ggml-quants.c 507 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391
  1. #define GGML_COMMON_IMPL_C
  2. #include "ggml-common.h"
  3. #include "ggml-quants.h"
  4. #include "ggml-impl.h"
  5. #define GGML_COMMON_IMPL_C
  6. #include "ggml-common.h"
  7. #include <math.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <float.h>
  11. #include <stdlib.h> // for qsort
  12. #include <stdio.h> // for GGML_ASSERT
  13. #undef MIN
  14. #undef MAX
  15. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  16. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  17. #define UNUSED GGML_UNUSED
  18. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  19. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  20. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  21. // multiply int8_t, add results pairwise twice
  22. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  23. // Get absolute values of x vectors
  24. const __m128i ax = _mm_sign_epi8(x, x);
  25. // Sign the values of the y vectors
  26. const __m128i sy = _mm_sign_epi8(y, x);
  27. // Perform multiplication and create 16-bit values
  28. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  29. const __m128i ones = _mm_set1_epi16(1);
  30. return _mm_madd_epi16(ones, dot);
  31. }
  32. #if __AVX__ || __AVX2__ || __AVX512F__
  33. // horizontally add 8 floats
  34. static inline float hsum_float_8(const __m256 x) {
  35. __m128 res = _mm256_extractf128_ps(x, 1);
  36. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  37. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  38. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  39. return _mm_cvtss_f32(res);
  40. }
  41. // horizontally add 8 int32_t
  42. static inline int hsum_i32_8(const __m256i a) {
  43. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  44. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  45. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  46. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  47. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  48. }
  49. // horizontally add 4 int32_t
  50. static inline int hsum_i32_4(const __m128i a) {
  51. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  52. const __m128i sum64 = _mm_add_epi32(hi64, a);
  53. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  54. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  55. }
  56. #if defined(__AVX2__) || defined(__AVX512F__)
  57. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  58. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  59. uint32_t x32;
  60. memcpy(&x32, x, sizeof(uint32_t));
  61. const __m256i shuf_mask = _mm256_set_epi64x(
  62. 0x0303030303030303, 0x0202020202020202,
  63. 0x0101010101010101, 0x0000000000000000);
  64. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  65. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  66. bytes = _mm256_or_si256(bytes, bit_mask);
  67. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  68. }
  69. // Unpack 32 4-bit fields into 32 bytes
  70. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  71. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  72. {
  73. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  74. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  75. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  76. return _mm256_and_si256(lowMask, bytes);
  77. }
  78. // add int16_t pairwise and return as float vector
  79. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  80. const __m256i ones = _mm256_set1_epi16(1);
  81. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  82. return _mm256_cvtepi32_ps(summed_pairs);
  83. }
  84. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  85. #if defined(__AVXVNNI__) || (defined(__AVX512VNNI__) && defined(__AVX512VL__))
  86. const __m256i zero = _mm256_setzero_si256();
  87. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  88. return _mm256_cvtepi32_ps(summed_pairs);
  89. #else
  90. // Perform multiplication and create 16-bit values
  91. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  92. return sum_i16_pairs_float(dot);
  93. #endif
  94. }
  95. // multiply int8_t, add results pairwise twice and return as float vector
  96. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  97. #if __AVXVNNIINT8__
  98. const __m256i zero = _mm256_setzero_si256();
  99. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  100. return _mm256_cvtepi32_ps(summed_pairs);
  101. #else
  102. // Get absolute values of x vectors
  103. const __m256i ax = _mm256_sign_epi8(x, x);
  104. // Sign the values of the y vectors
  105. const __m256i sy = _mm256_sign_epi8(y, x);
  106. return mul_sum_us8_pairs_float(ax, sy);
  107. #endif
  108. }
  109. static inline __m128i packNibbles( __m256i bytes )
  110. {
  111. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  112. #if __AVX512F__
  113. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  114. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  115. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  116. #else
  117. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  118. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  119. __m256i low = _mm256_and_si256( lowByte, bytes );
  120. high = _mm256_srli_epi16( high, 4 );
  121. bytes = _mm256_or_si256( low, high );
  122. // Compress uint16_t lanes into bytes
  123. __m128i r0 = _mm256_castsi256_si128( bytes );
  124. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  125. return _mm_packus_epi16( r0, r1 );
  126. #endif
  127. }
  128. #elif defined(__AVX__)
  129. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  130. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  131. uint32_t x32;
  132. memcpy(&x32, x, sizeof(uint32_t));
  133. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  134. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  135. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  136. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  137. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  138. bytesl = _mm_or_si128(bytesl, bit_mask);
  139. bytesh = _mm_or_si128(bytesh, bit_mask);
  140. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  141. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  142. return MM256_SET_M128I(bytesh, bytesl);
  143. }
  144. // Unpack 32 4-bit fields into 32 bytes
  145. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  146. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  147. {
  148. // Load 16 bytes from memory
  149. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  150. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  151. const __m128i lowMask = _mm_set1_epi8(0xF);
  152. tmpl = _mm_and_si128(lowMask, tmpl);
  153. tmph = _mm_and_si128(lowMask, tmph);
  154. return MM256_SET_M128I(tmph, tmpl);
  155. }
  156. // add int16_t pairwise and return as float vector
  157. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  158. const __m128i ones = _mm_set1_epi16(1);
  159. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  160. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  161. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  162. return _mm256_cvtepi32_ps(summed_pairs);
  163. }
  164. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  165. const __m128i axl = _mm256_castsi256_si128(ax);
  166. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  167. const __m128i syl = _mm256_castsi256_si128(sy);
  168. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  169. // Perform multiplication and create 16-bit values
  170. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  171. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  172. return sum_i16_pairs_float(doth, dotl);
  173. }
  174. // multiply int8_t, add results pairwise twice and return as float vector
  175. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  176. const __m128i xl = _mm256_castsi256_si128(x);
  177. const __m128i xh = _mm256_extractf128_si256(x, 1);
  178. const __m128i yl = _mm256_castsi256_si128(y);
  179. const __m128i yh = _mm256_extractf128_si256(y, 1);
  180. // Get absolute values of x vectors
  181. const __m128i axl = _mm_sign_epi8(xl, xl);
  182. const __m128i axh = _mm_sign_epi8(xh, xh);
  183. // Sign the values of the y vectors
  184. const __m128i syl = _mm_sign_epi8(yl, xl);
  185. const __m128i syh = _mm_sign_epi8(yh, xh);
  186. // Perform multiplication and create 16-bit values
  187. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  188. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  189. return sum_i16_pairs_float(doth, dotl);
  190. }
  191. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  192. {
  193. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  194. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  195. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  196. __m128i low = _mm_and_si128( lowByte, bytes1 );
  197. high = _mm_srli_epi16( high, 4 );
  198. bytes1 = _mm_or_si128( low, high );
  199. high = _mm_andnot_si128( lowByte, bytes2 );
  200. low = _mm_and_si128( lowByte, bytes2 );
  201. high = _mm_srli_epi16( high, 4 );
  202. bytes2 = _mm_or_si128( low, high );
  203. return _mm_packus_epi16( bytes1, bytes2);
  204. }
  205. #endif
  206. #elif defined(__SSSE3__)
  207. // horizontally add 4x4 floats
  208. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  209. __m128 res_0 =_mm_hadd_ps(a, b);
  210. __m128 res_1 =_mm_hadd_ps(c, d);
  211. __m128 res =_mm_hadd_ps(res_0, res_1);
  212. res =_mm_hadd_ps(res, res);
  213. res =_mm_hadd_ps(res, res);
  214. return _mm_cvtss_f32(res);
  215. }
  216. #endif // __AVX__ || __AVX2__ || __AVX512F__
  217. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  218. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  219. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  220. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  221. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  222. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  223. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  224. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  225. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  226. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  227. // precomputed tables for expanding 8bits to 8 bytes:
  228. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  229. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  230. #endif
  231. // reference implementation for deterministic creation of model files
  232. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int64_t k) {
  233. static const int qk = QK4_0;
  234. assert(k % qk == 0);
  235. const int nb = k / qk;
  236. for (int i = 0; i < nb; i++) {
  237. float amax = 0.0f; // absolute max
  238. float max = 0.0f;
  239. for (int j = 0; j < qk; j++) {
  240. const float v = x[i*qk + j];
  241. if (amax < fabsf(v)) {
  242. amax = fabsf(v);
  243. max = v;
  244. }
  245. }
  246. const float d = max / -8;
  247. const float id = d ? 1.0f/d : 0.0f;
  248. y[i].d = GGML_FP32_TO_FP16(d);
  249. for (int j = 0; j < qk/2; ++j) {
  250. const float x0 = x[i*qk + 0 + j]*id;
  251. const float x1 = x[i*qk + qk/2 + j]*id;
  252. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  253. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  254. y[i].qs[j] = xi0;
  255. y[i].qs[j] |= xi1 << 4;
  256. }
  257. }
  258. }
  259. void quantize_row_q4_0(const float * restrict x, void * restrict y, int64_t k) {
  260. quantize_row_q4_0_reference(x, y, k);
  261. }
  262. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int64_t k) {
  263. const int qk = QK4_1;
  264. assert(k % qk == 0);
  265. const int nb = k / qk;
  266. for (int i = 0; i < nb; i++) {
  267. float min = FLT_MAX;
  268. float max = -FLT_MAX;
  269. for (int j = 0; j < qk; j++) {
  270. const float v = x[i*qk + j];
  271. if (v < min) min = v;
  272. if (v > max) max = v;
  273. }
  274. const float d = (max - min) / ((1 << 4) - 1);
  275. const float id = d ? 1.0f/d : 0.0f;
  276. y[i].d = GGML_FP32_TO_FP16(d);
  277. y[i].m = GGML_FP32_TO_FP16(min);
  278. for (int j = 0; j < qk/2; ++j) {
  279. const float x0 = (x[i*qk + 0 + j] - min)*id;
  280. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  281. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  282. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  283. y[i].qs[j] = xi0;
  284. y[i].qs[j] |= xi1 << 4;
  285. }
  286. }
  287. }
  288. void quantize_row_q4_1(const float * restrict x, void * restrict y, int64_t k) {
  289. quantize_row_q4_1_reference(x, y, k);
  290. }
  291. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int64_t k) {
  292. static const int qk = QK5_0;
  293. assert(k % qk == 0);
  294. const int nb = k / qk;
  295. for (int i = 0; i < nb; i++) {
  296. float amax = 0.0f; // absolute max
  297. float max = 0.0f;
  298. for (int j = 0; j < qk; j++) {
  299. const float v = x[i*qk + j];
  300. if (amax < fabsf(v)) {
  301. amax = fabsf(v);
  302. max = v;
  303. }
  304. }
  305. const float d = max / -16;
  306. const float id = d ? 1.0f/d : 0.0f;
  307. y[i].d = GGML_FP32_TO_FP16(d);
  308. uint32_t qh = 0;
  309. for (int j = 0; j < qk/2; ++j) {
  310. const float x0 = x[i*qk + 0 + j]*id;
  311. const float x1 = x[i*qk + qk/2 + j]*id;
  312. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  313. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  314. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  315. // get the 5-th bit and store it in qh at the right position
  316. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  317. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  318. }
  319. memcpy(&y[i].qh, &qh, sizeof(qh));
  320. }
  321. }
  322. void quantize_row_q5_0(const float * restrict x, void * restrict y, int64_t k) {
  323. quantize_row_q5_0_reference(x, y, k);
  324. }
  325. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int64_t k) {
  326. const int qk = QK5_1;
  327. assert(k % qk == 0);
  328. const int nb = k / qk;
  329. for (int i = 0; i < nb; i++) {
  330. float min = FLT_MAX;
  331. float max = -FLT_MAX;
  332. for (int j = 0; j < qk; j++) {
  333. const float v = x[i*qk + j];
  334. if (v < min) min = v;
  335. if (v > max) max = v;
  336. }
  337. const float d = (max - min) / ((1 << 5) - 1);
  338. const float id = d ? 1.0f/d : 0.0f;
  339. y[i].d = GGML_FP32_TO_FP16(d);
  340. y[i].m = GGML_FP32_TO_FP16(min);
  341. uint32_t qh = 0;
  342. for (int j = 0; j < qk/2; ++j) {
  343. const float x0 = (x[i*qk + 0 + j] - min)*id;
  344. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  345. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  346. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  347. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  348. // get the 5-th bit and store it in qh at the right position
  349. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  350. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  351. }
  352. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  353. }
  354. }
  355. void quantize_row_q5_1(const float * restrict x, void * restrict y, int64_t k) {
  356. quantize_row_q5_1_reference(x, y, k);
  357. }
  358. // reference implementation for deterministic creation of model files
  359. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int64_t k) {
  360. assert(k % QK8_0 == 0);
  361. const int nb = k / QK8_0;
  362. for (int i = 0; i < nb; i++) {
  363. float amax = 0.0f; // absolute max
  364. for (int j = 0; j < QK8_0; j++) {
  365. const float v = x[i*QK8_0 + j];
  366. amax = MAX(amax, fabsf(v));
  367. }
  368. const float d = amax / ((1 << 7) - 1);
  369. const float id = d ? 1.0f/d : 0.0f;
  370. y[i].d = GGML_FP32_TO_FP16(d);
  371. for (int j = 0; j < QK8_0; ++j) {
  372. const float x0 = x[i*QK8_0 + j]*id;
  373. y[i].qs[j] = roundf(x0);
  374. }
  375. }
  376. }
  377. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int64_t k) {
  378. assert(QK8_0 == 32);
  379. assert(k % QK8_0 == 0);
  380. const int nb = k / QK8_0;
  381. block_q8_0 * restrict y = vy;
  382. #if defined(__ARM_NEON)
  383. for (int i = 0; i < nb; i++) {
  384. float32x4_t srcv [8];
  385. float32x4_t asrcv[8];
  386. float32x4_t amaxv[8];
  387. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  388. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  389. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  390. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  391. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  392. const float amax = vmaxvq_f32(amaxv[0]);
  393. const float d = amax / ((1 << 7) - 1);
  394. const float id = d ? 1.0f/d : 0.0f;
  395. y[i].d = GGML_FP32_TO_FP16(d);
  396. for (int j = 0; j < 8; j++) {
  397. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  398. const int32x4_t vi = vcvtnq_s32_f32(v);
  399. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  400. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  401. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  402. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  403. }
  404. }
  405. #elif defined(__wasm_simd128__)
  406. for (int i = 0; i < nb; i++) {
  407. v128_t srcv [8];
  408. v128_t asrcv[8];
  409. v128_t amaxv[8];
  410. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  411. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  412. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  413. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  414. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  415. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  416. wasm_f32x4_extract_lane(amaxv[0], 1)),
  417. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  418. wasm_f32x4_extract_lane(amaxv[0], 3)));
  419. const float d = amax / ((1 << 7) - 1);
  420. const float id = d ? 1.0f/d : 0.0f;
  421. y[i].d = GGML_FP32_TO_FP16(d);
  422. for (int j = 0; j < 8; j++) {
  423. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  424. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  425. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  426. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  427. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  428. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  429. }
  430. }
  431. #elif defined(__AVX2__) || defined(__AVX__)
  432. for (int i = 0; i < nb; i++) {
  433. // Load elements into 4 AVX vectors
  434. __m256 v0 = _mm256_loadu_ps( x );
  435. __m256 v1 = _mm256_loadu_ps( x + 8 );
  436. __m256 v2 = _mm256_loadu_ps( x + 16 );
  437. __m256 v3 = _mm256_loadu_ps( x + 24 );
  438. x += 32;
  439. // Compute max(abs(e)) for the block
  440. const __m256 signBit = _mm256_set1_ps( -0.0f );
  441. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  442. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  443. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  444. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  445. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  446. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  447. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  448. const float maxScalar = _mm_cvtss_f32( max4 );
  449. // Quantize these floats
  450. const float d = maxScalar / 127.f;
  451. y[i].d = GGML_FP32_TO_FP16(d);
  452. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  453. const __m256 mul = _mm256_set1_ps( id );
  454. // Apply the multiplier
  455. v0 = _mm256_mul_ps( v0, mul );
  456. v1 = _mm256_mul_ps( v1, mul );
  457. v2 = _mm256_mul_ps( v2, mul );
  458. v3 = _mm256_mul_ps( v3, mul );
  459. // Round to nearest integer
  460. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  461. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  462. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  463. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  464. // Convert floats to integers
  465. __m256i i0 = _mm256_cvtps_epi32( v0 );
  466. __m256i i1 = _mm256_cvtps_epi32( v1 );
  467. __m256i i2 = _mm256_cvtps_epi32( v2 );
  468. __m256i i3 = _mm256_cvtps_epi32( v3 );
  469. #if defined(__AVX2__)
  470. // Convert int32 to int16
  471. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  472. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  473. // Convert int16 to int8
  474. 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
  475. // We got our precious signed bytes, but the order is now wrong
  476. // These AVX2 pack instructions process 16-byte pieces independently
  477. // The following instruction is fixing the order
  478. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  479. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  480. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  481. #else
  482. // Since we don't have in AVX some necessary functions,
  483. // we split the registers in half and call AVX2 analogs from SSE
  484. __m128i ni0 = _mm256_castsi256_si128( i0 );
  485. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  486. __m128i ni2 = _mm256_castsi256_si128( i1 );
  487. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  488. __m128i ni4 = _mm256_castsi256_si128( i2 );
  489. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  490. __m128i ni6 = _mm256_castsi256_si128( i3 );
  491. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  492. // Convert int32 to int16
  493. ni0 = _mm_packs_epi32( ni0, ni1 );
  494. ni2 = _mm_packs_epi32( ni2, ni3 );
  495. ni4 = _mm_packs_epi32( ni4, ni5 );
  496. ni6 = _mm_packs_epi32( ni6, ni7 );
  497. // Convert int16 to int8
  498. ni0 = _mm_packs_epi16( ni0, ni2 );
  499. ni4 = _mm_packs_epi16( ni4, ni6 );
  500. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  501. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  502. #endif
  503. }
  504. #elif defined(__riscv_v_intrinsic)
  505. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  506. for (int i = 0; i < nb; i++) {
  507. // load elements
  508. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  509. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  510. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  511. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  512. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  513. const float d = amax / ((1 << 7) - 1);
  514. const float id = d ? 1.0f/d : 0.0f;
  515. y[i].d = GGML_FP32_TO_FP16(d);
  516. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  517. // convert to integer
  518. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  519. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  520. // store result
  521. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  522. }
  523. #else
  524. GGML_UNUSED(nb);
  525. // scalar
  526. quantize_row_q8_0_reference(x, y, k);
  527. #endif
  528. }
  529. // reference implementation for deterministic creation of model files
  530. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int64_t k) {
  531. assert(QK8_1 == 32);
  532. assert(k % QK8_1 == 0);
  533. const int nb = k / QK8_1;
  534. for (int i = 0; i < nb; i++) {
  535. float amax = 0.0f; // absolute max
  536. for (int j = 0; j < QK8_1; j++) {
  537. const float v = x[i*QK8_1 + j];
  538. amax = MAX(amax, fabsf(v));
  539. }
  540. const float d = amax / ((1 << 7) - 1);
  541. const float id = d ? 1.0f/d : 0.0f;
  542. y[i].d = GGML_FP32_TO_FP16(d);
  543. int sum = 0;
  544. for (int j = 0; j < QK8_1/2; ++j) {
  545. const float v0 = x[i*QK8_1 + j]*id;
  546. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  547. y[i].qs[ j] = roundf(v0);
  548. y[i].qs[QK8_1/2 + j] = roundf(v1);
  549. sum += y[i].qs[ j];
  550. sum += y[i].qs[QK8_1/2 + j];
  551. }
  552. y[i].s = GGML_FP32_TO_FP16(sum*d);
  553. }
  554. }
  555. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int64_t k) {
  556. assert(k % QK8_1 == 0);
  557. const int nb = k / QK8_1;
  558. block_q8_1 * restrict y = vy;
  559. #if defined(__ARM_NEON)
  560. for (int i = 0; i < nb; i++) {
  561. float32x4_t srcv [8];
  562. float32x4_t asrcv[8];
  563. float32x4_t amaxv[8];
  564. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  565. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  566. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  567. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  568. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  569. const float amax = vmaxvq_f32(amaxv[0]);
  570. const float d = amax / ((1 << 7) - 1);
  571. const float id = d ? 1.0f/d : 0.0f;
  572. y[i].d = GGML_FP32_TO_FP16(d);
  573. int32x4_t accv = vdupq_n_s32(0);
  574. for (int j = 0; j < 8; j++) {
  575. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  576. const int32x4_t vi = vcvtnq_s32_f32(v);
  577. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  578. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  579. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  580. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  581. accv = vaddq_s32(accv, vi);
  582. }
  583. y[i].s = GGML_FP32_TO_FP16(d * vaddvq_s32(accv));
  584. }
  585. #elif defined(__wasm_simd128__)
  586. for (int i = 0; i < nb; i++) {
  587. v128_t srcv [8];
  588. v128_t asrcv[8];
  589. v128_t amaxv[8];
  590. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  591. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  592. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  593. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  594. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  595. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  596. wasm_f32x4_extract_lane(amaxv[0], 1)),
  597. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  598. wasm_f32x4_extract_lane(amaxv[0], 3)));
  599. const float d = amax / ((1 << 7) - 1);
  600. const float id = d ? 1.0f/d : 0.0f;
  601. y[i].d = GGML_FP32_TO_FP16(d);
  602. v128_t accv = wasm_i32x4_splat(0);
  603. for (int j = 0; j < 8; j++) {
  604. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  605. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  606. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  607. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  608. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  609. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  610. accv = wasm_i32x4_add(accv, vi);
  611. }
  612. y[i].s = GGML_FP32_TO_FP16(
  613. d * (wasm_i32x4_extract_lane(accv, 0) +
  614. wasm_i32x4_extract_lane(accv, 1) +
  615. wasm_i32x4_extract_lane(accv, 2) +
  616. wasm_i32x4_extract_lane(accv, 3)));
  617. }
  618. #elif defined(__AVX2__) || defined(__AVX__)
  619. for (int i = 0; i < nb; i++) {
  620. // Load elements into 4 AVX vectors
  621. __m256 v0 = _mm256_loadu_ps( x );
  622. __m256 v1 = _mm256_loadu_ps( x + 8 );
  623. __m256 v2 = _mm256_loadu_ps( x + 16 );
  624. __m256 v3 = _mm256_loadu_ps( x + 24 );
  625. x += 32;
  626. // Compute max(abs(e)) for the block
  627. const __m256 signBit = _mm256_set1_ps( -0.0f );
  628. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  629. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  630. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  631. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  632. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  633. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  634. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  635. const float maxScalar = _mm_cvtss_f32( max4 );
  636. // Quantize these floats
  637. const float d = maxScalar / 127.f;
  638. y[i].d = GGML_FP32_TO_FP16(d);
  639. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  640. const __m256 mul = _mm256_set1_ps( id );
  641. // Apply the multiplier
  642. v0 = _mm256_mul_ps( v0, mul );
  643. v1 = _mm256_mul_ps( v1, mul );
  644. v2 = _mm256_mul_ps( v2, mul );
  645. v3 = _mm256_mul_ps( v3, mul );
  646. // Round to nearest integer
  647. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  648. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  649. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  650. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  651. // Convert floats to integers
  652. __m256i i0 = _mm256_cvtps_epi32( v0 );
  653. __m256i i1 = _mm256_cvtps_epi32( v1 );
  654. __m256i i2 = _mm256_cvtps_epi32( v2 );
  655. __m256i i3 = _mm256_cvtps_epi32( v3 );
  656. #if defined(__AVX2__)
  657. // Compute the sum of the quants and set y[i].s
  658. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3))));
  659. // Convert int32 to int16
  660. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  661. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  662. // Convert int16 to int8
  663. 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
  664. // We got our precious signed bytes, but the order is now wrong
  665. // These AVX2 pack instructions process 16-byte pieces independently
  666. // The following instruction is fixing the order
  667. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  668. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  669. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  670. #else
  671. // Since we don't have in AVX some necessary functions,
  672. // we split the registers in half and call AVX2 analogs from SSE
  673. __m128i ni0 = _mm256_castsi256_si128( i0 );
  674. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  675. __m128i ni2 = _mm256_castsi256_si128( i1 );
  676. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  677. __m128i ni4 = _mm256_castsi256_si128( i2 );
  678. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  679. __m128i ni6 = _mm256_castsi256_si128( i3 );
  680. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  681. // Compute the sum of the quants and set y[i].s
  682. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  683. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  684. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_4(_mm_add_epi32(s0, s1)));
  685. // Convert int32 to int16
  686. ni0 = _mm_packs_epi32( ni0, ni1 );
  687. ni2 = _mm_packs_epi32( ni2, ni3 );
  688. ni4 = _mm_packs_epi32( ni4, ni5 );
  689. ni6 = _mm_packs_epi32( ni6, ni7 );
  690. // Convert int16 to int8
  691. ni0 = _mm_packs_epi16( ni0, ni2 );
  692. ni4 = _mm_packs_epi16( ni4, ni6 );
  693. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  694. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  695. #endif
  696. }
  697. #elif defined(__riscv_v_intrinsic)
  698. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  699. for (int i = 0; i < nb; i++) {
  700. // load elements
  701. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  702. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  703. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  704. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  705. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  706. const float d = amax / ((1 << 7) - 1);
  707. const float id = d ? 1.0f/d : 0.0f;
  708. y[i].d = GGML_FP32_TO_FP16(d);
  709. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  710. // convert to integer
  711. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  712. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  713. // store result
  714. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  715. // compute sum for y[i].s
  716. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  717. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  718. // set y[i].s
  719. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  720. y[i].s = GGML_FP32_TO_FP16(sum*d);
  721. }
  722. #else
  723. GGML_UNUSED(nb);
  724. // scalar
  725. quantize_row_q8_1_reference(x, y, k);
  726. #endif
  727. }
  728. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int64_t k) {
  729. static const int qk = QK4_0;
  730. assert(k % qk == 0);
  731. const int nb = k / qk;
  732. for (int i = 0; i < nb; i++) {
  733. const float d = GGML_FP16_TO_FP32(x[i].d);
  734. for (int j = 0; j < qk/2; ++j) {
  735. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  736. const int x1 = (x[i].qs[j] >> 4) - 8;
  737. y[i*qk + j + 0 ] = x0*d;
  738. y[i*qk + j + qk/2] = x1*d;
  739. }
  740. }
  741. }
  742. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int64_t k) {
  743. static const int qk = QK4_1;
  744. assert(k % qk == 0);
  745. const int nb = k / qk;
  746. for (int i = 0; i < nb; i++) {
  747. const float d = GGML_FP16_TO_FP32(x[i].d);
  748. const float m = GGML_FP16_TO_FP32(x[i].m);
  749. for (int j = 0; j < qk/2; ++j) {
  750. const int x0 = (x[i].qs[j] & 0x0F);
  751. const int x1 = (x[i].qs[j] >> 4);
  752. y[i*qk + j + 0 ] = x0*d + m;
  753. y[i*qk + j + qk/2] = x1*d + m;
  754. }
  755. }
  756. }
  757. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int64_t k) {
  758. static const int qk = QK5_0;
  759. assert(k % qk == 0);
  760. const int nb = k / qk;
  761. for (int i = 0; i < nb; i++) {
  762. const float d = GGML_FP16_TO_FP32(x[i].d);
  763. uint32_t qh;
  764. memcpy(&qh, x[i].qh, sizeof(qh));
  765. for (int j = 0; j < qk/2; ++j) {
  766. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  767. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  768. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  769. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  770. y[i*qk + j + 0 ] = x0*d;
  771. y[i*qk + j + qk/2] = x1*d;
  772. }
  773. }
  774. }
  775. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int64_t k) {
  776. static const int qk = QK5_1;
  777. assert(k % qk == 0);
  778. const int nb = k / qk;
  779. for (int i = 0; i < nb; i++) {
  780. const float d = GGML_FP16_TO_FP32(x[i].d);
  781. const float m = GGML_FP16_TO_FP32(x[i].m);
  782. uint32_t qh;
  783. memcpy(&qh, x[i].qh, sizeof(qh));
  784. for (int j = 0; j < qk/2; ++j) {
  785. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  786. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  787. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  788. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  789. y[i*qk + j + 0 ] = x0*d + m;
  790. y[i*qk + j + qk/2] = x1*d + m;
  791. }
  792. }
  793. }
  794. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int64_t k) {
  795. static const int qk = QK8_0;
  796. assert(k % qk == 0);
  797. const int nb = k / qk;
  798. for (int i = 0; i < nb; i++) {
  799. const float d = GGML_FP16_TO_FP32(x[i].d);
  800. for (int j = 0; j < qk; ++j) {
  801. y[i*qk + j] = x[i].qs[j]*d;
  802. }
  803. }
  804. }
  805. //
  806. // 2-6 bit quantization in super-blocks
  807. //
  808. //
  809. // ===================== Helper functions
  810. //
  811. static inline int nearest_int(float fval) {
  812. assert(fval <= 4194303.f);
  813. float val = fval + 12582912.f;
  814. int i; memcpy(&i, &val, sizeof(int));
  815. return (i & 0x007fffff) - 0x00400000;
  816. }
  817. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  818. const float * restrict qw) {
  819. float max = 0;
  820. float amax = 0;
  821. for (int i = 0; i < n; ++i) {
  822. float ax = fabsf(x[i]);
  823. if (ax > amax) { amax = ax; max = x[i]; }
  824. }
  825. if (amax < 1e-30f) { // all zero
  826. for (int i = 0; i < n; ++i) {
  827. L[i] = 0;
  828. }
  829. return 0.f;
  830. }
  831. float iscale = -nmax / max;
  832. if (rmse_type == 0) {
  833. for (int i = 0; i < n; ++i) {
  834. int l = nearest_int(iscale * x[i]);
  835. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  836. }
  837. return 1/iscale;
  838. }
  839. bool return_early = false;
  840. if (rmse_type < 0) {
  841. rmse_type = -rmse_type;
  842. return_early = true;
  843. }
  844. float sumlx = 0;
  845. float suml2 = 0;
  846. #ifdef HAVE_BUGGY_APPLE_LINKER
  847. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  848. for (volatile int i = 0; i < n; ++i) {
  849. #else
  850. for (int i = 0; i < n; ++i) {
  851. #endif
  852. int l = nearest_int(iscale * x[i]);
  853. l = MAX(-nmax, MIN(nmax-1, l));
  854. L[i] = l + nmax;
  855. 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]));
  856. sumlx += w*x[i]*l;
  857. suml2 += w*l*l;
  858. }
  859. float scale = sumlx/suml2;
  860. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  861. float best = scale * sumlx;
  862. for (int is = -9; is <= 9; ++is) {
  863. if (is == 0) {
  864. continue;
  865. }
  866. iscale = -(nmax + 0.1f*is) / max;
  867. sumlx = suml2 = 0;
  868. for (int i = 0; i < n; ++i) {
  869. int l = nearest_int(iscale * x[i]);
  870. l = MAX(-nmax, MIN(nmax-1, l));
  871. 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]));
  872. sumlx += w*x[i]*l;
  873. suml2 += w*l*l;
  874. }
  875. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  876. for (int i = 0; i < n; ++i) {
  877. int l = nearest_int(iscale * x[i]);
  878. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  879. }
  880. scale = sumlx/suml2; best = scale*sumlx;
  881. }
  882. }
  883. return scale;
  884. }
  885. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  886. float max = 0;
  887. float amax = 0;
  888. for (int i = 0; i < n; ++i) {
  889. float ax = fabsf(x[i]);
  890. if (ax > amax) { amax = ax; max = x[i]; }
  891. }
  892. if (!amax) { // all zero
  893. for (int i = 0; i < n; ++i) { L[i] = 0; }
  894. return 0.f;
  895. }
  896. float iscale = -nmax / max;
  897. if (do_rmse) {
  898. float sumlx = 0;
  899. float suml2 = 0;
  900. for (int i = 0; i < n; ++i) {
  901. int l = nearest_int(iscale * x[i]);
  902. l = MAX(-nmax, MIN(nmax-1, l));
  903. L[i] = l;
  904. float w = x[i]*x[i];
  905. sumlx += w*x[i]*l;
  906. suml2 += w*l*l;
  907. }
  908. for (int itry = 0; itry < 5; ++itry) {
  909. int n_changed = 0;
  910. for (int i = 0; i < n; ++i) {
  911. float w = x[i]*x[i];
  912. float slx = sumlx - w*x[i]*L[i];
  913. if (slx > 0) {
  914. float sl2 = suml2 - w*L[i]*L[i];
  915. int new_l = nearest_int(x[i] * sl2 / slx);
  916. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  917. if (new_l != L[i]) {
  918. slx += w*x[i]*new_l;
  919. sl2 += w*new_l*new_l;
  920. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  921. L[i] = new_l; sumlx = slx; suml2 = sl2;
  922. ++n_changed;
  923. }
  924. }
  925. }
  926. }
  927. if (!n_changed) {
  928. break;
  929. }
  930. }
  931. for (int i = 0; i < n; ++i) {
  932. L[i] += nmax;
  933. }
  934. return sumlx / suml2;
  935. }
  936. for (int i = 0; i < n; ++i) {
  937. int l = nearest_int(iscale * x[i]);
  938. l = MAX(-nmax, MIN(nmax-1, l));
  939. L[i] = l + nmax;
  940. }
  941. return 1/iscale;
  942. }
  943. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  944. int ntry, float alpha) {
  945. float min = x[0];
  946. float max = x[0];
  947. for (int i = 1; i < n; ++i) {
  948. if (x[i] < min) min = x[i];
  949. if (x[i] > max) max = x[i];
  950. }
  951. if (max == min) {
  952. for (int i = 0; i < n; ++i) L[i] = 0;
  953. *the_min = 0;
  954. return 0.f;
  955. }
  956. if (min > 0) min = 0;
  957. float iscale = nmax/(max - min);
  958. float scale = 1/iscale;
  959. for (int itry = 0; itry < ntry; ++itry) {
  960. float sumlx = 0; int suml2 = 0;
  961. bool did_change = false;
  962. for (int i = 0; i < n; ++i) {
  963. int l = nearest_int(iscale*(x[i] - min));
  964. l = MAX(0, MIN(nmax, l));
  965. if (l != L[i]) {
  966. L[i] = l;
  967. did_change = true;
  968. }
  969. sumlx += (x[i] - min)*l;
  970. suml2 += l*l;
  971. }
  972. scale = sumlx/suml2;
  973. float sum = 0;
  974. for (int i = 0; i < n; ++i) {
  975. sum += x[i] - scale*L[i];
  976. }
  977. min = alpha*min + (1 - alpha)*sum/n;
  978. if (min > 0) min = 0;
  979. iscale = 1/scale;
  980. if (!did_change) break;
  981. }
  982. *the_min = -min;
  983. return scale;
  984. }
  985. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  986. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  987. float rmin, float rdelta, int nstep, bool use_mad) {
  988. float min = x[0];
  989. float max = x[0];
  990. float sum_w = weights[0];
  991. float sum_x = sum_w * x[0];
  992. #ifdef HAVE_BUGGY_APPLE_LINKER
  993. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  994. for (volatile int i = 1; i < n; ++i) {
  995. #else
  996. for (int i = 1; i < n; ++i) {
  997. #endif
  998. if (x[i] < min) min = x[i];
  999. if (x[i] > max) max = x[i];
  1000. float w = weights[i];
  1001. sum_w += w;
  1002. sum_x += w * x[i];
  1003. }
  1004. if (min > 0) min = 0;
  1005. if (max == min) {
  1006. for (int i = 0; i < n; ++i) L[i] = 0;
  1007. *the_min = -min;
  1008. return 0.f;
  1009. }
  1010. float iscale = nmax/(max - min);
  1011. float scale = 1/iscale;
  1012. float best_mad = 0;
  1013. for (int i = 0; i < n; ++i) {
  1014. int l = nearest_int(iscale*(x[i] - min));
  1015. L[i] = MAX(0, MIN(nmax, l));
  1016. float diff = scale * L[i] + min - x[i];
  1017. diff = use_mad ? fabsf(diff) : diff * diff;
  1018. float w = weights[i];
  1019. best_mad += w * diff;
  1020. }
  1021. if (nstep < 1) {
  1022. *the_min = -min;
  1023. return scale;
  1024. }
  1025. for (int is = 0; is <= nstep; ++is) {
  1026. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1027. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1028. for (int i = 0; i < n; ++i) {
  1029. int l = nearest_int(iscale*(x[i] - min));
  1030. l = MAX(0, MIN(nmax, l));
  1031. Laux[i] = l;
  1032. float w = weights[i];
  1033. sum_l += w*l;
  1034. sum_l2 += w*l*l;
  1035. sum_xl += w*l*x[i];
  1036. }
  1037. float D = sum_w * sum_l2 - sum_l * sum_l;
  1038. if (D > 0) {
  1039. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1040. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1041. if (this_min > 0) {
  1042. this_min = 0;
  1043. this_scale = sum_xl / sum_l2;
  1044. }
  1045. float mad = 0;
  1046. for (int i = 0; i < n; ++i) {
  1047. float diff = this_scale * Laux[i] + this_min - x[i];
  1048. diff = use_mad ? fabsf(diff) : diff * diff;
  1049. float w = weights[i];
  1050. mad += w * diff;
  1051. }
  1052. if (mad < best_mad) {
  1053. for (int i = 0; i < n; ++i) {
  1054. L[i] = Laux[i];
  1055. }
  1056. best_mad = mad;
  1057. scale = this_scale;
  1058. min = this_min;
  1059. }
  1060. }
  1061. }
  1062. *the_min = -min;
  1063. return scale;
  1064. }
  1065. #if QK_K == 256
  1066. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1067. if (j < 4) {
  1068. *d = q[j] & 63; *m = q[j + 4] & 63;
  1069. } else {
  1070. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1071. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1072. }
  1073. }
  1074. #endif
  1075. //========================- 2-bit (de)-quantization
  1076. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int64_t k) {
  1077. assert(k % QK_K == 0);
  1078. const int nb = k / QK_K;
  1079. uint8_t L[QK_K];
  1080. uint8_t Laux[16];
  1081. float weights[16];
  1082. float mins[QK_K/16];
  1083. float scales[QK_K/16];
  1084. const float q4scale = 15.f;
  1085. for (int i = 0; i < nb; i++) {
  1086. float max_scale = 0; // as we are deducting the min, scales are always positive
  1087. float max_min = 0;
  1088. for (int j = 0; j < QK_K/16; ++j) {
  1089. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1090. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1091. float scale = scales[j];
  1092. if (scale > max_scale) {
  1093. max_scale = scale;
  1094. }
  1095. float min = mins[j];
  1096. if (min > max_min) {
  1097. max_min = min;
  1098. }
  1099. }
  1100. if (max_scale > 0) {
  1101. float iscale = q4scale/max_scale;
  1102. for (int j = 0; j < QK_K/16; ++j) {
  1103. int l = nearest_int(iscale*scales[j]);
  1104. y[i].scales[j] = l;
  1105. }
  1106. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1107. } else {
  1108. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1109. y[i].d = GGML_FP32_TO_FP16(0.f);
  1110. }
  1111. if (max_min > 0) {
  1112. float iscale = q4scale/max_min;
  1113. for (int j = 0; j < QK_K/16; ++j) {
  1114. int l = nearest_int(iscale*mins[j]);
  1115. y[i].scales[j] |= (l << 4);
  1116. }
  1117. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1118. } else {
  1119. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1120. }
  1121. for (int j = 0; j < QK_K/16; ++j) {
  1122. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1123. if (!d) continue;
  1124. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1125. for (int ii = 0; ii < 16; ++ii) {
  1126. int l = nearest_int((x[16*j + ii] + dm)/d);
  1127. l = MAX(0, MIN(3, l));
  1128. L[16*j + ii] = l;
  1129. }
  1130. }
  1131. #if QK_K == 256
  1132. for (int j = 0; j < QK_K; j += 128) {
  1133. for (int l = 0; l < 32; ++l) {
  1134. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1135. }
  1136. }
  1137. #else
  1138. for (int l = 0; l < 16; ++l) {
  1139. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1140. }
  1141. #endif
  1142. x += QK_K;
  1143. }
  1144. }
  1145. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int64_t k) {
  1146. assert(k % QK_K == 0);
  1147. const int nb = k / QK_K;
  1148. for (int i = 0; i < nb; i++) {
  1149. const float d = GGML_FP16_TO_FP32(x[i].d);
  1150. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1151. const uint8_t * q = x[i].qs;
  1152. #if QK_K == 256
  1153. int is = 0;
  1154. float dl, ml;
  1155. for (int n = 0; n < QK_K; n += 128) {
  1156. int shift = 0;
  1157. for (int j = 0; j < 4; ++j) {
  1158. uint8_t sc = x[i].scales[is++];
  1159. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1160. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1161. sc = x[i].scales[is++];
  1162. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1163. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1164. shift += 2;
  1165. }
  1166. q += 32;
  1167. }
  1168. #else
  1169. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1170. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1171. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1172. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1173. for (int l = 0; l < 16; ++l) {
  1174. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1175. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1176. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1177. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1178. }
  1179. y += QK_K;
  1180. #endif
  1181. }
  1182. }
  1183. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int64_t k) {
  1184. quantize_row_q2_K_reference(x, vy, k);
  1185. }
  1186. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1187. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1188. float rmin, float rdelta, int nstep, bool use_mad) {
  1189. float min = x[0];
  1190. float max = x[0];
  1191. float sum_w = weights ? weights[0] : x[0]*x[0];
  1192. float sum_x = sum_w * x[0];
  1193. #ifdef HAVE_BUGGY_APPLE_LINKER
  1194. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1195. for (volatile int i = 1; i < n; ++i) {
  1196. #else
  1197. for (int i = 1; i < n; ++i) {
  1198. #endif
  1199. if (x[i] < min) min = x[i];
  1200. if (x[i] > max) max = x[i];
  1201. float w = weights ? weights[i] : x[i]*x[i];
  1202. sum_w += w;
  1203. sum_x += w * x[i];
  1204. }
  1205. if (min > 0) {
  1206. min = 0;
  1207. }
  1208. if (max <= min) {
  1209. memset(L, 0, n);
  1210. *the_min = -min;
  1211. return 0.f;
  1212. }
  1213. float iscale = nmax/(max - min);
  1214. float scale = 1/iscale;
  1215. float best_mad = 0;
  1216. for (int i = 0; i < n; ++i) {
  1217. int l = nearest_int(iscale*(x[i] - min));
  1218. L[i] = MAX(0, MIN(nmax, l));
  1219. float diff = scale * L[i] + min - x[i];
  1220. diff = use_mad ? fabsf(diff) : diff*diff;
  1221. float w = weights ? weights[i] : x[i]*x[i];
  1222. best_mad += w * diff;
  1223. }
  1224. if (nstep < 1) {
  1225. *the_min = -min;
  1226. return scale;
  1227. }
  1228. for (int is = 0; is <= nstep; ++is) {
  1229. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1230. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1231. for (int i = 0; i < n; ++i) {
  1232. int l = nearest_int(iscale*(x[i] - min));
  1233. l = MAX(0, MIN(nmax, l));
  1234. Laux[i] = l;
  1235. float w = weights ? weights[i] : x[i]*x[i];
  1236. sum_l += w*l;
  1237. sum_l2 += w*l*l;
  1238. sum_xl += w*l*x[i];
  1239. }
  1240. float D = sum_w * sum_l2 - sum_l * sum_l;
  1241. if (D > 0) {
  1242. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1243. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1244. if (this_min > 0) {
  1245. this_min = 0;
  1246. this_scale = sum_xl / sum_l2;
  1247. }
  1248. float mad = 0;
  1249. for (int i = 0; i < n; ++i) {
  1250. float diff = this_scale * Laux[i] + this_min - x[i];
  1251. diff = use_mad ? fabsf(diff) : diff*diff;
  1252. float w = weights ? weights[i] : x[i]*x[i];
  1253. mad += w * diff;
  1254. }
  1255. if (mad < best_mad) {
  1256. for (int i = 0; i < n; ++i) {
  1257. L[i] = Laux[i];
  1258. }
  1259. best_mad = mad;
  1260. scale = this_scale;
  1261. min = this_min;
  1262. }
  1263. }
  1264. }
  1265. *the_min = -min;
  1266. return scale;
  1267. }
  1268. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1269. float max = 0;
  1270. for (int i = 0; i < n; ++i) {
  1271. max = MAX(max, x[i]);
  1272. }
  1273. if (!max) { // all zero
  1274. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1275. return 0.f;
  1276. }
  1277. float iscale = nmax / max;
  1278. for (int i = 0; i < n; ++i) {
  1279. L[i] = nearest_int(iscale * x[i]);
  1280. }
  1281. float scale = 1/iscale;
  1282. float best_mse = 0;
  1283. for (int i = 0; i < n; ++i) {
  1284. float diff = x[i] - scale*L[i];
  1285. float w = quant_weights[i];
  1286. best_mse += w*diff*diff;
  1287. }
  1288. for (int is = -4; is <= 4; ++is) {
  1289. if (is == 0) continue;
  1290. float iscale_is = (0.1f*is + nmax)/max;
  1291. float scale_is = 1/iscale_is;
  1292. float mse = 0;
  1293. for (int i = 0; i < n; ++i) {
  1294. int l = nearest_int(iscale_is*x[i]);
  1295. l = MIN(nmax, l);
  1296. float diff = x[i] - scale_is*l;
  1297. float w = quant_weights[i];
  1298. mse += w*diff*diff;
  1299. }
  1300. if (mse < best_mse) {
  1301. best_mse = mse;
  1302. iscale = iscale_is;
  1303. }
  1304. }
  1305. float sumlx = 0;
  1306. float suml2 = 0;
  1307. for (int i = 0; i < n; ++i) {
  1308. int l = nearest_int(iscale * x[i]);
  1309. l = MIN(nmax, l);
  1310. L[i] = l;
  1311. float w = quant_weights[i];
  1312. sumlx += w*x[i]*l;
  1313. suml2 += w*l*l;
  1314. }
  1315. for (int itry = 0; itry < 5; ++itry) {
  1316. int n_changed = 0;
  1317. for (int i = 0; i < n; ++i) {
  1318. float w = quant_weights[i];
  1319. float slx = sumlx - w*x[i]*L[i];
  1320. float sl2 = suml2 - w*L[i]*L[i];
  1321. if (slx > 0 && sl2 > 0) {
  1322. int new_l = nearest_int(x[i] * sl2 / slx);
  1323. new_l = MIN(nmax, new_l);
  1324. if (new_l != L[i]) {
  1325. slx += w*x[i]*new_l;
  1326. sl2 += w*new_l*new_l;
  1327. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1328. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1329. ++n_changed;
  1330. }
  1331. }
  1332. }
  1333. }
  1334. if (!n_changed) {
  1335. break;
  1336. }
  1337. }
  1338. return sumlx / suml2;
  1339. }
  1340. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1341. GGML_ASSERT(quant_weights);
  1342. assert(k % QK_K == 0);
  1343. const int nb = k / QK_K;
  1344. const bool requantize = true;
  1345. uint8_t L[QK_K];
  1346. uint8_t Laux[16];
  1347. float mins[QK_K/16];
  1348. float scales[QK_K/16];
  1349. float sw[QK_K/16];
  1350. float weight[16];
  1351. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1352. for (int i = 0; i < nb; i++) {
  1353. memset(sw, 0, QK_K/16*sizeof(float));
  1354. float sumx2 = 0;
  1355. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1356. float sigma2 = sumx2/QK_K;
  1357. for (int j = 0; j < QK_K/16; ++j) {
  1358. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1359. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1360. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1361. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1362. }
  1363. float dm, mm;
  1364. #if QK_K == 64
  1365. float max_scale = 0, max_min = 0;
  1366. for (int j = 0; j < QK_K/16; ++j) {
  1367. max_scale = MAX(max_scale, scales[j]);
  1368. max_min = MAX(max_min, mins[j]);
  1369. }
  1370. dm = max_scale/15;
  1371. mm = max_min/15;
  1372. if (max_scale) {
  1373. float id = 1/dm;
  1374. for (int j = 0; j < QK_K/16; ++j) {
  1375. int l = nearest_int(id*scales[j]);
  1376. Ls[j] = MAX(0, MIN(15, l));
  1377. }
  1378. } else {
  1379. memset(Ls, 0, QK_K/16);
  1380. }
  1381. if (max_min) {
  1382. float id = 1/mm;
  1383. for (int j = 0; j < QK_K/16; ++j) {
  1384. int l = nearest_int(id*mins[j]);
  1385. Lm[j] = MAX(0, MIN(15, l));
  1386. }
  1387. } else {
  1388. memset(Lm, 0, QK_K/16);
  1389. }
  1390. #else
  1391. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1392. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1393. #endif
  1394. y[i].d = GGML_FP32_TO_FP16(dm);
  1395. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1396. dm = GGML_FP16_TO_FP32(y[i].d);
  1397. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1398. for (int j = 0; j < QK_K/16; ++j) {
  1399. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1400. }
  1401. if (requantize) {
  1402. for (int j = 0; j < QK_K/16; ++j) {
  1403. const float d = dm * (y[i].scales[j] & 0xF);
  1404. if (!d) continue;
  1405. const float m = mm * (y[i].scales[j] >> 4);
  1406. for (int ii = 0; ii < 16; ++ii) {
  1407. int l = nearest_int((x[16*j + ii] + m)/d);
  1408. l = MAX(0, MIN(3, l));
  1409. L[16*j + ii] = l;
  1410. }
  1411. }
  1412. }
  1413. #if QK_K == 256
  1414. for (int j = 0; j < QK_K; j += 128) {
  1415. for (int l = 0; l < 32; ++l) {
  1416. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1417. }
  1418. }
  1419. #else
  1420. for (int l = 0; l < 16; ++l) {
  1421. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1422. }
  1423. #endif
  1424. x += QK_K;
  1425. }
  1426. }
  1427. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1428. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1429. if (!quant_weights) {
  1430. quantize_row_q2_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1431. }
  1432. else {
  1433. char * qrow = (char *)dst;
  1434. for (int64_t row = 0; row < nrow; ++row) {
  1435. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1436. src += n_per_row;
  1437. qrow += row_size;
  1438. }
  1439. }
  1440. return nrow * row_size;
  1441. }
  1442. //========================= 3-bit (de)-quantization
  1443. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int64_t k) {
  1444. assert(k % QK_K == 0);
  1445. const int nb = k / QK_K;
  1446. int8_t L[QK_K];
  1447. float scales[QK_K / 16];
  1448. for (int i = 0; i < nb; i++) {
  1449. float max_scale = 0;
  1450. float amax = 0;
  1451. for (int j = 0; j < QK_K/16; ++j) {
  1452. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1453. float scale = fabsf(scales[j]);
  1454. if (scale > amax) {
  1455. amax = scale; max_scale = scales[j];
  1456. }
  1457. }
  1458. #if QK_K == 256
  1459. memset(y[i].scales, 0, 12);
  1460. if (max_scale) {
  1461. float iscale = -32.f/max_scale;
  1462. for (int j = 0; j < QK_K/16; ++j) {
  1463. int8_t l = nearest_int(iscale*scales[j]);
  1464. l = MAX(-32, MIN(31, l)) + 32;
  1465. if (j < 8) {
  1466. y[i].scales[j] = l & 0xF;
  1467. } else {
  1468. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1469. }
  1470. l >>= 4;
  1471. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1472. }
  1473. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1474. } else {
  1475. y[i].d = GGML_FP32_TO_FP16(0.f);
  1476. }
  1477. int8_t sc;
  1478. for (int j = 0; j < QK_K/16; ++j) {
  1479. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1480. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1481. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1482. if (!d) {
  1483. continue;
  1484. }
  1485. for (int ii = 0; ii < 16; ++ii) {
  1486. int l = nearest_int(x[16*j + ii]/d);
  1487. l = MAX(-4, MIN(3, l));
  1488. L[16*j + ii] = l + 4;
  1489. }
  1490. }
  1491. #else
  1492. if (max_scale) {
  1493. float iscale = -8.f/max_scale;
  1494. for (int j = 0; j < QK_K/16; j+=2) {
  1495. int l1 = nearest_int(iscale*scales[j]);
  1496. l1 = 8 + MAX(-8, MIN(7, l1));
  1497. int l2 = nearest_int(iscale*scales[j+1]);
  1498. l2 = 8 + MAX(-8, MIN(7, l2));
  1499. y[i].scales[j/2] = l1 | (l2 << 4);
  1500. }
  1501. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1502. } else {
  1503. for (int j = 0; j < QK_K/16; j+=2) {
  1504. y[i].scales[j/2] = 0;
  1505. }
  1506. y[i].d = GGML_FP32_TO_FP16(0.f);
  1507. }
  1508. for (int j = 0; j < QK_K/16; ++j) {
  1509. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1510. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1511. if (!d) {
  1512. continue;
  1513. }
  1514. for (int ii = 0; ii < 16; ++ii) {
  1515. int l = nearest_int(x[16*j + ii]/d);
  1516. l = MAX(-4, MIN(3, l));
  1517. L[16*j + ii] = l + 4;
  1518. }
  1519. }
  1520. #endif
  1521. memset(y[i].hmask, 0, QK_K/8);
  1522. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1523. int m = 0;
  1524. uint8_t hm = 1;
  1525. for (int j = 0; j < QK_K; ++j) {
  1526. if (L[j] > 3) {
  1527. y[i].hmask[m] |= hm;
  1528. L[j] -= 4;
  1529. }
  1530. if (++m == QK_K/8) {
  1531. m = 0; hm <<= 1;
  1532. }
  1533. }
  1534. #if QK_K == 256
  1535. for (int j = 0; j < QK_K; j += 128) {
  1536. for (int l = 0; l < 32; ++l) {
  1537. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1538. }
  1539. }
  1540. #else
  1541. for (int l = 0; l < 16; ++l) {
  1542. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1543. }
  1544. #endif
  1545. x += QK_K;
  1546. }
  1547. }
  1548. #if QK_K == 256
  1549. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1550. assert(k % QK_K == 0);
  1551. const int nb = k / QK_K;
  1552. const uint32_t kmask1 = 0x03030303;
  1553. const uint32_t kmask2 = 0x0f0f0f0f;
  1554. uint32_t aux[4];
  1555. const int8_t * scales = (const int8_t*)aux;
  1556. for (int i = 0; i < nb; i++) {
  1557. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1558. const uint8_t * restrict q = x[i].qs;
  1559. const uint8_t * restrict hm = x[i].hmask;
  1560. uint8_t m = 1;
  1561. memcpy(aux, x[i].scales, 12);
  1562. uint32_t tmp = aux[2];
  1563. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1564. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1565. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1566. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1567. int is = 0;
  1568. float dl;
  1569. for (int n = 0; n < QK_K; n += 128) {
  1570. int shift = 0;
  1571. for (int j = 0; j < 4; ++j) {
  1572. dl = d_all * (scales[is++] - 32);
  1573. for (int l = 0; l < 16; ++l) {
  1574. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1575. }
  1576. dl = d_all * (scales[is++] - 32);
  1577. for (int l = 0; l < 16; ++l) {
  1578. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1579. }
  1580. shift += 2;
  1581. m <<= 1;
  1582. }
  1583. q += 32;
  1584. }
  1585. }
  1586. }
  1587. #else
  1588. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1589. assert(k % QK_K == 0);
  1590. assert(QK_K == 64);
  1591. const int nb = k / QK_K;
  1592. for (int i = 0; i < nb; i++) {
  1593. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1594. const uint8_t * restrict q = x[i].qs;
  1595. const uint8_t * restrict hm = x[i].hmask;
  1596. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1597. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1598. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1599. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1600. for (int l=0; l<8; ++l) {
  1601. uint8_t h = hm[l];
  1602. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1603. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1604. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1605. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1606. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1607. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1608. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1609. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1610. }
  1611. y += QK_K;
  1612. }
  1613. }
  1614. #endif
  1615. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int64_t k) {
  1616. quantize_row_q3_K_reference(x, vy, k);
  1617. }
  1618. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int64_t n_per_row, const float * restrict quant_weights) {
  1619. #if QK_K != 256
  1620. (void)quant_weights;
  1621. quantize_row_q3_K_reference(x, y, n_per_row);
  1622. #else
  1623. assert(n_per_row % QK_K == 0);
  1624. const int nb = n_per_row / QK_K;
  1625. int8_t L[QK_K];
  1626. float scales[QK_K / 16];
  1627. float weight[16];
  1628. float sw[QK_K / 16];
  1629. int8_t Ls[QK_K / 16];
  1630. for (int i = 0; i < nb; i++) {
  1631. float sumx2 = 0;
  1632. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1633. float sigma2 = 2*sumx2/QK_K;
  1634. for (int j = 0; j < QK_K/16; ++j) {
  1635. if (quant_weights) {
  1636. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1637. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1638. } else {
  1639. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1640. }
  1641. float sumw = 0;
  1642. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1643. sw[j] = sumw;
  1644. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1645. }
  1646. memset(y[i].scales, 0, 12);
  1647. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1648. for (int j = 0; j < QK_K/16; ++j) {
  1649. int l = Ls[j];
  1650. if (j < 8) {
  1651. y[i].scales[j] = l & 0xF;
  1652. } else {
  1653. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1654. }
  1655. l >>= 4;
  1656. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1657. }
  1658. y[i].d = GGML_FP32_TO_FP16(d_block);
  1659. int8_t sc;
  1660. for (int j = 0; j < QK_K/16; ++j) {
  1661. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1662. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1663. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1664. if (!d) {
  1665. continue;
  1666. }
  1667. for (int ii = 0; ii < 16; ++ii) {
  1668. int l = nearest_int(x[16*j + ii]/d);
  1669. l = MAX(-4, MIN(3, l));
  1670. L[16*j + ii] = l + 4;
  1671. }
  1672. }
  1673. memset(y[i].hmask, 0, QK_K/8);
  1674. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1675. int m = 0;
  1676. uint8_t hm = 1;
  1677. for (int j = 0; j < QK_K; ++j) {
  1678. if (L[j] > 3) {
  1679. y[i].hmask[m] |= hm;
  1680. L[j] -= 4;
  1681. }
  1682. if (++m == QK_K/8) {
  1683. m = 0; hm <<= 1;
  1684. }
  1685. }
  1686. for (int j = 0; j < QK_K; j += 128) {
  1687. for (int l = 0; l < 32; ++l) {
  1688. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1689. }
  1690. }
  1691. x += QK_K;
  1692. }
  1693. #endif
  1694. }
  1695. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1696. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1697. if (!quant_weights) {
  1698. quantize_row_q3_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1699. }
  1700. else {
  1701. char * qrow = (char *)dst;
  1702. for (int64_t row = 0; row < nrow; ++row) {
  1703. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1704. src += n_per_row;
  1705. qrow += row_size;
  1706. }
  1707. }
  1708. return nrow * row_size;
  1709. }
  1710. // ====================== 4-bit (de)-quantization
  1711. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int64_t k) {
  1712. assert(k % QK_K == 0);
  1713. const int nb = k / QK_K;
  1714. uint8_t L[QK_K];
  1715. uint8_t Laux[32];
  1716. float weights[32];
  1717. float mins[QK_K/32];
  1718. float scales[QK_K/32];
  1719. for (int i = 0; i < nb; i++) {
  1720. float max_scale = 0; // as we are deducting the min, scales are always positive
  1721. float max_min = 0;
  1722. for (int j = 0; j < QK_K/32; ++j) {
  1723. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1724. float sum_x2 = 0;
  1725. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1726. float av_x = sqrtf(sum_x2/32);
  1727. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1728. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1729. float scale = scales[j];
  1730. if (scale > max_scale) {
  1731. max_scale = scale;
  1732. }
  1733. float min = mins[j];
  1734. if (min > max_min) {
  1735. max_min = min;
  1736. }
  1737. }
  1738. #if QK_K == 256
  1739. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1740. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1741. for (int j = 0; j < QK_K/32; ++j) {
  1742. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1743. uint8_t lm = nearest_int(inv_min*mins[j]);
  1744. ls = MIN(63, ls);
  1745. lm = MIN(63, lm);
  1746. if (j < 4) {
  1747. y[i].scales[j] = ls;
  1748. y[i].scales[j+4] = lm;
  1749. } else {
  1750. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1751. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1752. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1753. }
  1754. }
  1755. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1756. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1757. uint8_t sc, m;
  1758. for (int j = 0; j < QK_K/32; ++j) {
  1759. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1760. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1761. if (!d) continue;
  1762. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1763. for (int ii = 0; ii < 32; ++ii) {
  1764. int l = nearest_int((x[32*j + ii] + dm)/d);
  1765. l = MAX(0, MIN(15, l));
  1766. L[32*j + ii] = l;
  1767. }
  1768. }
  1769. #else
  1770. const float s_factor = 15.f;
  1771. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1772. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1773. int d1 = nearest_int(inv_scale*scales[0]);
  1774. int m1 = nearest_int(inv_min*mins[0]);
  1775. int d2 = nearest_int(inv_scale*scales[1]);
  1776. int m2 = nearest_int(inv_min*mins[1]);
  1777. y[i].scales[0] = d1 | (m1 << 4);
  1778. y[i].scales[1] = d2 | (m2 << 4);
  1779. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1780. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1781. float sumlx = 0;
  1782. int suml2 = 0;
  1783. for (int j = 0; j < QK_K/32; ++j) {
  1784. const uint8_t sd = y[i].scales[j] & 0xF;
  1785. const uint8_t sm = y[i].scales[j] >> 4;
  1786. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1787. if (!d) continue;
  1788. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1789. for (int ii = 0; ii < 32; ++ii) {
  1790. int l = nearest_int((x[32*j + ii] + m)/d);
  1791. l = MAX(0, MIN(15, l));
  1792. L[32*j + ii] = l;
  1793. sumlx += (x[32*j + ii] + m)*l*sd;
  1794. suml2 += l*l*sd*sd;
  1795. }
  1796. }
  1797. if (suml2) {
  1798. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1799. }
  1800. #endif
  1801. uint8_t * q = y[i].qs;
  1802. for (int j = 0; j < QK_K; j += 64) {
  1803. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1804. q += 32;
  1805. }
  1806. x += QK_K;
  1807. }
  1808. }
  1809. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int64_t k) {
  1810. assert(k % QK_K == 0);
  1811. const int nb = k / QK_K;
  1812. for (int i = 0; i < nb; i++) {
  1813. const uint8_t * q = x[i].qs;
  1814. #if QK_K == 256
  1815. const float d = GGML_FP16_TO_FP32(x[i].d);
  1816. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1817. int is = 0;
  1818. uint8_t sc, m;
  1819. for (int j = 0; j < QK_K; j += 64) {
  1820. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1821. const float d1 = d * sc; const float m1 = min * m;
  1822. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1823. const float d2 = d * sc; const float m2 = min * m;
  1824. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1825. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1826. q += 32; is += 2;
  1827. }
  1828. #else
  1829. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1830. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1831. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1832. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1833. for (int l = 0; l < 32; ++l) {
  1834. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1835. y[l+32] = d2 * (q[l] >> 4) - m2;
  1836. }
  1837. y += QK_K;
  1838. #endif
  1839. }
  1840. }
  1841. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int64_t k) {
  1842. assert(k % QK_K == 0);
  1843. block_q4_K * restrict y = vy;
  1844. quantize_row_q4_K_reference(x, y, k);
  1845. }
  1846. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  1847. #if QK_K != 256
  1848. (void)quant_weights;
  1849. quantize_row_q4_K_reference(x, y, n_per_row);
  1850. #else
  1851. assert(n_per_row % QK_K == 0);
  1852. const int64_t nb = n_per_row / QK_K;
  1853. uint8_t L[QK_K];
  1854. uint8_t Laux[32];
  1855. uint8_t Ls[QK_K/32];
  1856. uint8_t Lm[QK_K/32];
  1857. float weights[32];
  1858. float sw[QK_K/32];
  1859. float mins[QK_K/32];
  1860. float scales[QK_K/32];
  1861. for (int i = 0; i < nb; i++) {
  1862. float sum_x2 = 0;
  1863. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  1864. float sigma2 = 2*sum_x2/QK_K;
  1865. float av_x = sqrtf(sigma2);
  1866. for (int j = 0; j < QK_K/32; ++j) {
  1867. if (quant_weights) {
  1868. const float * qw = quant_weights + QK_K*i + 32*j;
  1869. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  1870. } else {
  1871. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1872. }
  1873. float sumw = 0;
  1874. for (int l = 0; l < 32; ++l) sumw += weights[l];
  1875. sw[j] = sumw;
  1876. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1877. }
  1878. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  1879. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  1880. for (int j = 0; j < QK_K/32; ++j) {
  1881. uint8_t ls = Ls[j];
  1882. uint8_t lm = Lm[j];
  1883. if (j < 4) {
  1884. y[i].scales[j] = ls;
  1885. y[i].scales[j+4] = lm;
  1886. } else {
  1887. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1888. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1889. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1890. }
  1891. }
  1892. y[i].d = GGML_FP32_TO_FP16(d_block);
  1893. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  1894. uint8_t sc, m;
  1895. for (int j = 0; j < QK_K/32; ++j) {
  1896. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1897. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1898. if (!d) continue;
  1899. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1900. for (int ii = 0; ii < 32; ++ii) {
  1901. int l = nearest_int((x[32*j + ii] + dm)/d);
  1902. l = MAX(0, MIN(15, l));
  1903. L[32*j + ii] = l;
  1904. }
  1905. }
  1906. uint8_t * q = y[i].qs;
  1907. for (int j = 0; j < QK_K; j += 64) {
  1908. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1909. q += 32;
  1910. }
  1911. x += QK_K;
  1912. }
  1913. #endif
  1914. }
  1915. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1916. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  1917. if (!quant_weights) {
  1918. quantize_row_q4_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1919. }
  1920. else {
  1921. char * qrow = (char *)dst;
  1922. for (int64_t row = 0; row < nrow; ++row) {
  1923. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  1924. src += n_per_row;
  1925. qrow += row_size;
  1926. }
  1927. }
  1928. return nrow * row_size;
  1929. }
  1930. // ====================== 5-bit (de)-quantization
  1931. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int64_t k) {
  1932. assert(k % QK_K == 0);
  1933. const int64_t nb = k / QK_K;
  1934. #if QK_K == 256
  1935. uint8_t L[QK_K];
  1936. float mins[QK_K/32];
  1937. float scales[QK_K/32];
  1938. float weights[32];
  1939. uint8_t Laux[32];
  1940. #else
  1941. int8_t L[QK_K];
  1942. float scales[QK_K/16];
  1943. #endif
  1944. for (int i = 0; i < nb; i++) {
  1945. #if QK_K == 256
  1946. float max_scale = 0; // as we are deducting the min, scales are always positive
  1947. float max_min = 0;
  1948. for (int j = 0; j < QK_K/32; ++j) {
  1949. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1950. float sum_x2 = 0;
  1951. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1952. float av_x = sqrtf(sum_x2/32);
  1953. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1954. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  1955. float scale = scales[j];
  1956. if (scale > max_scale) {
  1957. max_scale = scale;
  1958. }
  1959. float min = mins[j];
  1960. if (min > max_min) {
  1961. max_min = min;
  1962. }
  1963. }
  1964. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1965. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1966. for (int j = 0; j < QK_K/32; ++j) {
  1967. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1968. uint8_t lm = nearest_int(inv_min*mins[j]);
  1969. ls = MIN(63, ls);
  1970. lm = MIN(63, lm);
  1971. if (j < 4) {
  1972. y[i].scales[j] = ls;
  1973. y[i].scales[j+4] = lm;
  1974. } else {
  1975. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1976. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1977. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1978. }
  1979. }
  1980. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1981. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1982. uint8_t sc, m;
  1983. for (int j = 0; j < QK_K/32; ++j) {
  1984. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1985. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1986. if (!d) continue;
  1987. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1988. for (int ii = 0; ii < 32; ++ii) {
  1989. int l = nearest_int((x[32*j + ii] + dm)/d);
  1990. l = MAX(0, MIN(31, l));
  1991. L[32*j + ii] = l;
  1992. }
  1993. }
  1994. uint8_t * restrict qh = y[i].qh;
  1995. uint8_t * restrict ql = y[i].qs;
  1996. memset(qh, 0, QK_K/8);
  1997. uint8_t m1 = 1, m2 = 2;
  1998. for (int n = 0; n < QK_K; n += 64) {
  1999. for (int j = 0; j < 32; ++j) {
  2000. int l1 = L[n + j];
  2001. if (l1 > 15) {
  2002. l1 -= 16; qh[j] |= m1;
  2003. }
  2004. int l2 = L[n + j + 32];
  2005. if (l2 > 15) {
  2006. l2 -= 16; qh[j] |= m2;
  2007. }
  2008. ql[j] = l1 | (l2 << 4);
  2009. }
  2010. m1 <<= 2; m2 <<= 2;
  2011. ql += 32;
  2012. }
  2013. #else
  2014. float max_scale = 0, amax = 0;
  2015. for (int j = 0; j < QK_K/16; ++j) {
  2016. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2017. float abs_scale = fabsf(scales[j]);
  2018. if (abs_scale > amax) {
  2019. amax = abs_scale;
  2020. max_scale = scales[j];
  2021. }
  2022. }
  2023. float iscale = -128.f/max_scale;
  2024. for (int j = 0; j < QK_K/16; ++j) {
  2025. int l = nearest_int(iscale*scales[j]);
  2026. y[i].scales[j] = MAX(-128, MIN(127, l));
  2027. }
  2028. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2029. for (int j = 0; j < QK_K/16; ++j) {
  2030. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2031. if (!d) continue;
  2032. for (int ii = 0; ii < 16; ++ii) {
  2033. int l = nearest_int(x[16*j + ii]/d);
  2034. l = MAX(-16, MIN(15, l));
  2035. L[16*j + ii] = l + 16;
  2036. }
  2037. }
  2038. uint8_t * restrict qh = y[i].qh;
  2039. uint8_t * restrict ql = y[i].qs;
  2040. memset(qh, 0, QK_K/8);
  2041. for (int j = 0; j < 32; ++j) {
  2042. int jm = j%8;
  2043. int is = j/8;
  2044. int l1 = L[j];
  2045. if (l1 > 15) {
  2046. l1 -= 16; qh[jm] |= (1 << is);
  2047. }
  2048. int l2 = L[j + 32];
  2049. if (l2 > 15) {
  2050. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2051. }
  2052. ql[j] = l1 | (l2 << 4);
  2053. }
  2054. #endif
  2055. x += QK_K;
  2056. }
  2057. }
  2058. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int64_t k) {
  2059. assert(k % QK_K == 0);
  2060. const int64_t nb = k / QK_K;
  2061. for (int i = 0; i < nb; i++) {
  2062. const uint8_t * ql = x[i].qs;
  2063. const uint8_t * qh = x[i].qh;
  2064. #if QK_K == 256
  2065. const float d = GGML_FP16_TO_FP32(x[i].d);
  2066. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2067. int is = 0;
  2068. uint8_t sc, m;
  2069. uint8_t u1 = 1, u2 = 2;
  2070. for (int j = 0; j < QK_K; j += 64) {
  2071. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2072. const float d1 = d * sc; const float m1 = min * m;
  2073. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2074. const float d2 = d * sc; const float m2 = min * m;
  2075. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2076. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2077. ql += 32; is += 2;
  2078. u1 <<= 2; u2 <<= 2;
  2079. }
  2080. #else
  2081. float d = GGML_FP16_TO_FP32(x[i].d);
  2082. const int8_t * restrict s = x[i].scales;
  2083. for (int l = 0; l < 8; ++l) {
  2084. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2085. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2086. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2087. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2088. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2089. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2090. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2091. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2092. }
  2093. y += QK_K;
  2094. #endif
  2095. }
  2096. }
  2097. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int64_t k) {
  2098. assert(k % QK_K == 0);
  2099. block_q5_K * restrict y = vy;
  2100. quantize_row_q5_K_reference(x, y, k);
  2101. }
  2102. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2103. #if QK_K != 256
  2104. (void)quant_weights;
  2105. quantize_row_q5_K_reference(x, y, n_per_row);
  2106. #else
  2107. assert(n_per_row % QK_K == 0);
  2108. const int64_t nb = n_per_row / QK_K;
  2109. uint8_t L[QK_K];
  2110. uint8_t Laux[32];
  2111. uint8_t Ls[QK_K/32];
  2112. uint8_t Lm[QK_K/32];
  2113. float mins[QK_K/32];
  2114. float scales[QK_K/32];
  2115. float sw[QK_K/32];
  2116. float weights[32];
  2117. for (int i = 0; i < nb; i++) {
  2118. float sum_x2 = 0;
  2119. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2120. float sigma2 = 2*sum_x2/QK_K;
  2121. float av_x = sqrtf(sigma2);
  2122. for (int j = 0; j < QK_K/32; ++j) {
  2123. if (quant_weights) {
  2124. const float * qw = quant_weights + QK_K*i + 32*j;
  2125. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2126. } else {
  2127. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2128. }
  2129. float sumw = 0;
  2130. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2131. sw[j] = sumw;
  2132. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2133. }
  2134. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2135. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2136. for (int j = 0; j < QK_K/32; ++j) {
  2137. uint8_t ls = Ls[j];
  2138. uint8_t lm = Lm[j];
  2139. ls = MIN(63, ls);
  2140. lm = MIN(63, lm);
  2141. if (j < 4) {
  2142. y[i].scales[j] = ls;
  2143. y[i].scales[j+4] = lm;
  2144. } else {
  2145. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2146. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2147. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2148. }
  2149. }
  2150. y[i].d = GGML_FP32_TO_FP16(d_block);
  2151. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2152. uint8_t sc, m;
  2153. for (int j = 0; j < QK_K/32; ++j) {
  2154. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2155. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2156. if (!d) continue;
  2157. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2158. for (int ii = 0; ii < 32; ++ii) {
  2159. int l = nearest_int((x[32*j + ii] + dm)/d);
  2160. l = MAX(0, MIN(31, l));
  2161. L[32*j + ii] = l;
  2162. }
  2163. }
  2164. uint8_t * restrict qh = y[i].qh;
  2165. uint8_t * restrict ql = y[i].qs;
  2166. memset(qh, 0, QK_K/8);
  2167. uint8_t m1 = 1, m2 = 2;
  2168. for (int n = 0; n < QK_K; n += 64) {
  2169. for (int j = 0; j < 32; ++j) {
  2170. int l1 = L[n + j];
  2171. if (l1 > 15) {
  2172. l1 -= 16; qh[j] |= m1;
  2173. }
  2174. int l2 = L[n + j + 32];
  2175. if (l2 > 15) {
  2176. l2 -= 16; qh[j] |= m2;
  2177. }
  2178. ql[j] = l1 | (l2 << 4);
  2179. }
  2180. m1 <<= 2; m2 <<= 2;
  2181. ql += 32;
  2182. }
  2183. x += QK_K;
  2184. }
  2185. #endif
  2186. }
  2187. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2188. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2189. if (!quant_weights) {
  2190. quantize_row_q5_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2191. }
  2192. else {
  2193. char * qrow = (char *)dst;
  2194. for (int64_t row = 0; row < nrow; ++row) {
  2195. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2196. src += n_per_row;
  2197. qrow += row_size;
  2198. }
  2199. }
  2200. return nrow * row_size;
  2201. }
  2202. // ====================== 6-bit (de)-quantization
  2203. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int64_t k) {
  2204. assert(k % QK_K == 0);
  2205. const int64_t nb = k / QK_K;
  2206. int8_t L[QK_K];
  2207. float scales[QK_K/16];
  2208. for (int i = 0; i < nb; i++) {
  2209. float max_scale = 0;
  2210. float max_abs_scale = 0;
  2211. for (int ib = 0; ib < QK_K/16; ++ib) {
  2212. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2213. scales[ib] = scale;
  2214. const float abs_scale = fabsf(scale);
  2215. if (abs_scale > max_abs_scale) {
  2216. max_abs_scale = abs_scale;
  2217. max_scale = scale;
  2218. }
  2219. }
  2220. if (!max_abs_scale) {
  2221. memset(&y[i], 0, sizeof(block_q6_K));
  2222. y[i].d = GGML_FP32_TO_FP16(0.f);
  2223. x += QK_K;
  2224. continue;
  2225. }
  2226. float iscale = -128.f/max_scale;
  2227. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2228. for (int ib = 0; ib < QK_K/16; ++ib) {
  2229. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2230. }
  2231. for (int j = 0; j < QK_K/16; ++j) {
  2232. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2233. if (!d) {
  2234. continue;
  2235. }
  2236. for (int ii = 0; ii < 16; ++ii) {
  2237. int l = nearest_int(x[16*j + ii]/d);
  2238. l = MAX(-32, MIN(31, l));
  2239. L[16*j + ii] = l + 32;
  2240. }
  2241. }
  2242. uint8_t * restrict ql = y[i].ql;
  2243. uint8_t * restrict qh = y[i].qh;
  2244. #if QK_K == 256
  2245. for (int j = 0; j < QK_K; j += 128) {
  2246. for (int l = 0; l < 32; ++l) {
  2247. const uint8_t q1 = L[j + l + 0] & 0xF;
  2248. const uint8_t q2 = L[j + l + 32] & 0xF;
  2249. const uint8_t q3 = L[j + l + 64] & 0xF;
  2250. const uint8_t q4 = L[j + l + 96] & 0xF;
  2251. ql[l+ 0] = q1 | (q3 << 4);
  2252. ql[l+32] = q2 | (q4 << 4);
  2253. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2254. }
  2255. ql += 64;
  2256. qh += 32;
  2257. }
  2258. #else
  2259. for (int l = 0; l < 32; ++l) {
  2260. const uint8_t q1 = L[l + 0] & 0xF;
  2261. const uint8_t q2 = L[l + 32] & 0xF;
  2262. ql[l] = q1 | (q2 << 4);
  2263. }
  2264. for (int l = 0; l < 16; ++l) {
  2265. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2266. }
  2267. #endif
  2268. x += QK_K;
  2269. }
  2270. }
  2271. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int64_t k) {
  2272. assert(k % QK_K == 0);
  2273. const int64_t nb = k / QK_K;
  2274. for (int i = 0; i < nb; i++) {
  2275. const float d = GGML_FP16_TO_FP32(x[i].d);
  2276. const uint8_t * restrict ql = x[i].ql;
  2277. const uint8_t * restrict qh = x[i].qh;
  2278. const int8_t * restrict sc = x[i].scales;
  2279. #if QK_K == 256
  2280. for (int n = 0; n < QK_K; n += 128) {
  2281. for (int l = 0; l < 32; ++l) {
  2282. int is = l/16;
  2283. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2284. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2285. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2286. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2287. y[l + 0] = d * sc[is + 0] * q1;
  2288. y[l + 32] = d * sc[is + 2] * q2;
  2289. y[l + 64] = d * sc[is + 4] * q3;
  2290. y[l + 96] = d * sc[is + 6] * q4;
  2291. }
  2292. y += 128;
  2293. ql += 64;
  2294. qh += 32;
  2295. sc += 8;
  2296. }
  2297. #else
  2298. for (int l = 0; l < 16; ++l) {
  2299. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2300. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2301. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2302. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2303. y[l+ 0] = d * sc[0] * q1;
  2304. y[l+16] = d * sc[1] * q2;
  2305. y[l+32] = d * sc[2] * q3;
  2306. y[l+48] = d * sc[3] * q4;
  2307. }
  2308. y += 64;
  2309. #endif
  2310. }
  2311. }
  2312. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int64_t k) {
  2313. assert(k % QK_K == 0);
  2314. block_q6_K * restrict y = vy;
  2315. quantize_row_q6_K_reference(x, y, k);
  2316. }
  2317. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2318. #if QK_K != 256
  2319. (void)quant_weights;
  2320. quantize_row_q6_K_reference(x, y, n_per_row);
  2321. #else
  2322. assert(n_per_row % QK_K == 0);
  2323. const int64_t nb = n_per_row / QK_K;
  2324. int8_t L[QK_K];
  2325. float scales[QK_K/16];
  2326. //float weights[16];
  2327. for (int i = 0; i < nb; i++) {
  2328. //float sum_x2 = 0;
  2329. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2330. //float sigma2 = sum_x2/QK_K;
  2331. float max_scale = 0;
  2332. float max_abs_scale = 0;
  2333. for (int ib = 0; ib < QK_K/16; ++ib) {
  2334. float scale;
  2335. if (quant_weights) {
  2336. const float * qw = quant_weights + QK_K*i + 16*ib;
  2337. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2338. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2339. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2340. } else {
  2341. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2342. }
  2343. scales[ib] = scale;
  2344. const float abs_scale = fabsf(scale);
  2345. if (abs_scale > max_abs_scale) {
  2346. max_abs_scale = abs_scale;
  2347. max_scale = scale;
  2348. }
  2349. }
  2350. if (!max_abs_scale) {
  2351. memset(&y[i], 0, sizeof(block_q6_K));
  2352. y[i].d = GGML_FP32_TO_FP16(0.f);
  2353. x += QK_K;
  2354. continue;
  2355. }
  2356. float iscale = -128.f/max_scale;
  2357. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2358. for (int ib = 0; ib < QK_K/16; ++ib) {
  2359. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2360. }
  2361. for (int j = 0; j < QK_K/16; ++j) {
  2362. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2363. if (!d) {
  2364. continue;
  2365. }
  2366. for (int ii = 0; ii < 16; ++ii) {
  2367. int l = nearest_int(x[16*j + ii]/d);
  2368. l = MAX(-32, MIN(31, l));
  2369. L[16*j + ii] = l + 32;
  2370. }
  2371. }
  2372. uint8_t * restrict ql = y[i].ql;
  2373. uint8_t * restrict qh = y[i].qh;
  2374. for (int j = 0; j < QK_K; j += 128) {
  2375. for (int l = 0; l < 32; ++l) {
  2376. const uint8_t q1 = L[j + l + 0] & 0xF;
  2377. const uint8_t q2 = L[j + l + 32] & 0xF;
  2378. const uint8_t q3 = L[j + l + 64] & 0xF;
  2379. const uint8_t q4 = L[j + l + 96] & 0xF;
  2380. ql[l+ 0] = q1 | (q3 << 4);
  2381. ql[l+32] = q2 | (q4 << 4);
  2382. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2383. }
  2384. ql += 64;
  2385. qh += 32;
  2386. }
  2387. x += QK_K;
  2388. }
  2389. #endif
  2390. }
  2391. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2392. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2393. if (!quant_weights) {
  2394. quantize_row_q6_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2395. }
  2396. else {
  2397. char * qrow = (char *)dst;
  2398. for (int64_t row = 0; row < nrow; ++row) {
  2399. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2400. src += n_per_row;
  2401. qrow += row_size;
  2402. }
  2403. }
  2404. return nrow * row_size;
  2405. }
  2406. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2407. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2408. if (!quant_weights) {
  2409. quantize_row_q4_0_reference(x, y, n_per_row);
  2410. return;
  2411. }
  2412. float weight[QK4_0];
  2413. int8_t L[QK4_0];
  2414. float sum_x2 = 0;
  2415. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2416. float sigma2 = sum_x2/n_per_row;
  2417. const int64_t nb = n_per_row/QK4_0;
  2418. for (int ib = 0; ib < nb; ++ib) {
  2419. const float * xb = x + QK4_0 * ib;
  2420. const float * qw = quant_weights + QK4_0 * ib;
  2421. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2422. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2423. y[ib].d = GGML_FP32_TO_FP16(d);
  2424. for (int j = 0; j < 16; ++j) {
  2425. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2426. }
  2427. }
  2428. }
  2429. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2430. if (!quant_weights) {
  2431. quantize_row_q4_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2432. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2433. }
  2434. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2435. char * qrow = (char *)dst;
  2436. for (int64_t row = 0; row < nrow; ++row) {
  2437. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2438. src += n_per_row;
  2439. qrow += row_size;
  2440. }
  2441. return nrow * row_size;
  2442. }
  2443. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2444. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2445. if (!quant_weights) {
  2446. quantize_row_q4_1_reference(x, y, n_per_row);
  2447. return;
  2448. }
  2449. float weight[QK4_1];
  2450. uint8_t L[QK4_1], Laux[QK4_1];
  2451. float sum_x2 = 0;
  2452. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2453. float sigma2 = sum_x2/n_per_row;
  2454. const int64_t nb = n_per_row/QK4_1;
  2455. for (int ib = 0; ib < nb; ++ib) {
  2456. const float * xb = x + QK4_1 * ib;
  2457. const float * qw = quant_weights + QK4_1 * ib;
  2458. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2459. float min;
  2460. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2461. y[ib].d = GGML_FP32_TO_FP16(d);
  2462. y[ib].m = GGML_FP32_TO_FP16(-min);
  2463. for (int j = 0; j < 16; ++j) {
  2464. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2465. }
  2466. }
  2467. }
  2468. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2469. if (!quant_weights) {
  2470. quantize_row_q4_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2471. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2472. }
  2473. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2474. char * qrow = (char *)dst;
  2475. for (int64_t row = 0; row < nrow; ++row) {
  2476. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2477. src += n_per_row;
  2478. qrow += row_size;
  2479. }
  2480. return nrow * row_size;
  2481. }
  2482. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2483. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2484. if (!quant_weights) {
  2485. quantize_row_q5_0_reference(x, y, n_per_row);
  2486. return;
  2487. }
  2488. float weight[QK5_0];
  2489. int8_t L[QK5_0];
  2490. float sum_x2 = 0;
  2491. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2492. float sigma2 = sum_x2/n_per_row;
  2493. const int64_t nb = n_per_row/QK5_0;
  2494. for (int ib = 0; ib < nb; ++ib) {
  2495. const float * xb = x + QK5_0 * ib;
  2496. const float * qw = quant_weights + QK5_0 * ib;
  2497. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2498. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2499. y[ib].d = GGML_FP32_TO_FP16(d);
  2500. uint32_t qh = 0;
  2501. for (int j = 0; j < 16; ++j) {
  2502. const uint8_t xi0 = L[j];
  2503. const uint8_t xi1 = L[j+16];
  2504. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2505. // get the 5-th bit and store it in qh at the right position
  2506. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2507. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2508. }
  2509. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2510. }
  2511. }
  2512. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2513. if (!quant_weights) {
  2514. quantize_row_q5_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2515. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2516. }
  2517. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2518. char * qrow = (char *)dst;
  2519. for (int64_t row = 0; row < nrow; ++row) {
  2520. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2521. src += n_per_row;
  2522. qrow += row_size;
  2523. }
  2524. return nrow * row_size;
  2525. }
  2526. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2527. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2528. if (!quant_weights) {
  2529. quantize_row_q5_1_reference(x, y, n_per_row);
  2530. return;
  2531. }
  2532. float weight[QK5_1];
  2533. uint8_t L[QK5_1], Laux[QK5_1];
  2534. float sum_x2 = 0;
  2535. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2536. float sigma2 = sum_x2/n_per_row;
  2537. const int64_t nb = n_per_row/QK5_1;
  2538. for (int ib = 0; ib < nb; ++ib) {
  2539. const float * xb = x + QK5_1 * ib;
  2540. const float * qw = quant_weights + QK5_1 * ib;
  2541. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2542. float min;
  2543. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2544. y[ib].d = GGML_FP32_TO_FP16(d);
  2545. y[ib].m = GGML_FP32_TO_FP16(-min);
  2546. uint32_t qh = 0;
  2547. for (int j = 0; j < 16; ++j) {
  2548. const uint8_t xi0 = L[j];
  2549. const uint8_t xi1 = L[j+16];
  2550. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2551. // get the 5-th bit and store it in qh at the right position
  2552. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2553. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2554. }
  2555. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2556. }
  2557. }
  2558. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2559. if (!quant_weights) {
  2560. quantize_row_q5_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2561. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2562. }
  2563. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2564. char * qrow = (char *)dst;
  2565. for (int64_t row = 0; row < nrow; ++row) {
  2566. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2567. src += n_per_row;
  2568. qrow += row_size;
  2569. }
  2570. return nrow * row_size;
  2571. }
  2572. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2573. (void)quant_weights; // not used
  2574. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2575. quantize_row_q8_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2576. return nrow * row_size;
  2577. }
  2578. // ====================== "True" 2-bit (de)-quantization
  2579. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int64_t k) {
  2580. assert(k % QK_K == 0);
  2581. const int64_t nb = k / QK_K;
  2582. uint32_t aux32[2];
  2583. const uint8_t * aux8 = (const uint8_t *)aux32;
  2584. for (int i = 0; i < nb; i++) {
  2585. const float d = GGML_FP16_TO_FP32(x[i].d);
  2586. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2587. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2588. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2589. for (int l = 0; l < 4; ++l) {
  2590. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2591. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2592. for (int j = 0; j < 8; ++j) {
  2593. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2594. }
  2595. y += 8;
  2596. }
  2597. }
  2598. }
  2599. }
  2600. // ====================== 2.3125 bpw (de)-quantization
  2601. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int64_t k) {
  2602. assert(k % QK_K == 0);
  2603. const int64_t nb = k / QK_K;
  2604. float db[2];
  2605. for (int i = 0; i < nb; i++) {
  2606. const float d = GGML_FP16_TO_FP32(x[i].d);
  2607. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2608. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2609. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2610. for (int l = 0; l < 4; ++l) {
  2611. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2612. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2613. for (int j = 0; j < 8; ++j) {
  2614. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2615. }
  2616. y += 8;
  2617. }
  2618. }
  2619. }
  2620. }
  2621. // ====================== 2.5625 bpw (de)-quantization
  2622. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int64_t k) {
  2623. assert(k % QK_K == 0);
  2624. const int64_t nb = k / QK_K;
  2625. float db[2];
  2626. for (int i = 0; i < nb; i++) {
  2627. const float d = GGML_FP16_TO_FP32(x[i].d);
  2628. const uint8_t * qs = x[i].qs;
  2629. const uint8_t * qh = x[i].qh;
  2630. const uint8_t * signs = qs + QK_K/8;
  2631. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2632. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2633. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2634. for (int l = 0; l < 4; ++l) {
  2635. const float dl = db[l/2];
  2636. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2637. for (int j = 0; j < 8; ++j) {
  2638. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2639. }
  2640. y += 8;
  2641. }
  2642. qs += 4;
  2643. signs += 4;
  2644. }
  2645. }
  2646. }
  2647. // ====================== 3.0625 bpw (de)-quantization
  2648. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int64_t k) {
  2649. assert(k % QK_K == 0);
  2650. const int64_t nb = k / QK_K;
  2651. uint32_t aux32;
  2652. for (int i = 0; i < nb; i++) {
  2653. const float d = GGML_FP16_TO_FP32(x[i].d);
  2654. const uint8_t * qs = x[i].qs;
  2655. const uint8_t * scales_and_signs = qs + QK_K/4;
  2656. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2657. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2658. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2659. for (int l = 0; l < 4; ++l) {
  2660. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2661. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2662. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2663. for (int j = 0; j < 4; ++j) {
  2664. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2665. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2666. }
  2667. y += 8;
  2668. }
  2669. qs += 8;
  2670. }
  2671. }
  2672. }
  2673. // ====================== 3.3125 bpw (de)-quantization
  2674. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int64_t k) {
  2675. assert(k % QK_K == 0);
  2676. const int64_t nb = k / QK_K;
  2677. for (int i = 0; i < nb; i++) {
  2678. const float d = GGML_FP16_TO_FP32(x[i].d);
  2679. const uint8_t * qs = x[i].qs;
  2680. const uint8_t * qh = x[i].qh;
  2681. const uint8_t * signs = x[i].signs;
  2682. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2683. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2684. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2685. for (int l = 0; l < 4; ++l) {
  2686. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2687. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2688. for (int j = 0; j < 4; ++j) {
  2689. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2690. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2691. }
  2692. y += 8;
  2693. }
  2694. qs += 8;
  2695. signs += 4;
  2696. for (int l = 0; l < 4; ++l) {
  2697. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2698. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2699. for (int j = 0; j < 4; ++j) {
  2700. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2701. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2702. }
  2703. y += 8;
  2704. }
  2705. qh += 2;
  2706. qs += 8;
  2707. signs += 4;
  2708. }
  2709. }
  2710. }
  2711. // ====================== 1.5625 bpw (de)-quantization
  2712. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int64_t k) {
  2713. assert(k % QK_K == 0);
  2714. const int64_t nb = k / QK_K;
  2715. for (int i = 0; i < nb; i++) {
  2716. const float d = GGML_FP16_TO_FP32(x[i].d);
  2717. const uint8_t * qs = x[i].qs;
  2718. const uint16_t * qh = x[i].qh;
  2719. for (int ib = 0; ib < QK_K/32; ++ib) {
  2720. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2721. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2722. for (int l = 0; l < 4; ++l) {
  2723. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2724. for (int j = 0; j < 8; ++j) {
  2725. y[j] = dl * (grid[j] + delta);
  2726. }
  2727. y += 8;
  2728. }
  2729. qs += 4;
  2730. }
  2731. }
  2732. }
  2733. void dequantize_row_iq1_m(const block_iq1_m * restrict x, float * restrict y, int64_t k) {
  2734. assert(k % QK_K == 0);
  2735. const int64_t nb = k / QK_K;
  2736. float delta[4];
  2737. uint16_t idx[4];
  2738. #if QK_K != 64
  2739. iq1m_scale_t scale;
  2740. #endif
  2741. for (int i = 0; i < nb; i++) {
  2742. const uint16_t * sc = (const uint16_t *)x[i].scales;
  2743. #if QK_K == 64
  2744. const float d = GGML_FP16_TO_FP32(x[i].d);
  2745. #else
  2746. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2747. const float d = GGML_FP16_TO_FP32(scale.f16);
  2748. #endif
  2749. const uint8_t * qs = x[i].qs;
  2750. const uint8_t * qh = x[i].qh;
  2751. for (int ib = 0; ib < QK_K/32; ++ib) {
  2752. #if QK_K == 64
  2753. const float dl1 = d * (2*((sc[ib/2] >> (8*(ib%2)+0)) & 0xf) + 1);
  2754. const float dl2 = d * (2*((sc[ib/2] >> (8*(ib%2)+4)) & 0xf) + 1);
  2755. #else
  2756. const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
  2757. const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
  2758. #endif
  2759. idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
  2760. idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
  2761. idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
  2762. idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
  2763. delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2764. delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2765. delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2766. delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2767. for (int l = 0; l < 2; ++l) {
  2768. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2769. for (int j = 0; j < 8; ++j) {
  2770. y[j] = dl1 * (grid[j] + delta[l]);
  2771. }
  2772. y += 8;
  2773. }
  2774. for (int l = 2; l < 4; ++l) {
  2775. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2776. for (int j = 0; j < 8; ++j) {
  2777. y[j] = dl2 * (grid[j] + delta[l]);
  2778. }
  2779. y += 8;
  2780. }
  2781. qs += 4;
  2782. qh += 2;
  2783. }
  2784. }
  2785. }
  2786. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2787. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int64_t k) {
  2788. assert(k % QK4_NL == 0);
  2789. const int64_t nb = k / QK4_NL;
  2790. for (int i = 0; i < nb; i++) {
  2791. const uint8_t * qs = x[i].qs;
  2792. const float d = GGML_FP16_TO_FP32(x[i].d);
  2793. for (int j = 0; j < QK4_NL/2; ++j) {
  2794. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  2795. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  2796. }
  2797. y += QK4_NL;
  2798. qs += QK4_NL/2;
  2799. }
  2800. }
  2801. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int64_t k) {
  2802. assert(k % QK_K == 0);
  2803. #if QK_K == 64
  2804. dequantize_row_iq4_nl((const block_iq4_nl *)x, y, k);
  2805. #else
  2806. const int64_t nb = k / QK_K;
  2807. for (int i = 0; i < nb; i++) {
  2808. const uint8_t * qs = x[i].qs;
  2809. const float d = GGML_FP16_TO_FP32(x[i].d);
  2810. for (int ib = 0; ib < QK_K/32; ++ib) {
  2811. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  2812. const float dl = d * (ls - 32);
  2813. for (int j = 0; j < 16; ++j) {
  2814. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  2815. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  2816. }
  2817. y += 32;
  2818. qs += 16;
  2819. }
  2820. }
  2821. #endif
  2822. }
  2823. //===================================== Q8_K ==============================================
  2824. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int64_t k) {
  2825. assert(k % QK_K == 0);
  2826. const int64_t nb = k / QK_K;
  2827. for (int i = 0; i < nb; i++) {
  2828. float max = 0;
  2829. float amax = 0;
  2830. for (int j = 0; j < QK_K; ++j) {
  2831. float ax = fabsf(x[j]);
  2832. if (ax > amax) {
  2833. amax = ax; max = x[j];
  2834. }
  2835. }
  2836. if (!amax) {
  2837. y[i].d = 0;
  2838. memset(y[i].qs, 0, QK_K);
  2839. x += QK_K;
  2840. continue;
  2841. }
  2842. //const float iscale = -128.f/max;
  2843. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  2844. const float iscale = -127.f/max;
  2845. for (int j = 0; j < QK_K; ++j) {
  2846. int v = nearest_int(iscale*x[j]);
  2847. y[i].qs[j] = MIN(127, v);
  2848. }
  2849. for (int j = 0; j < QK_K/16; ++j) {
  2850. int sum = 0;
  2851. for (int ii = 0; ii < 16; ++ii) {
  2852. sum += y[i].qs[j*16 + ii];
  2853. }
  2854. y[i].bsums[j] = sum;
  2855. }
  2856. y[i].d = 1/iscale;
  2857. x += QK_K;
  2858. }
  2859. }
  2860. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int64_t k) {
  2861. assert(k % QK_K == 0);
  2862. const int64_t nb = k / QK_K;
  2863. for (int i = 0; i < nb; i++) {
  2864. for (int j = 0; j < QK_K; ++j) {
  2865. *y++ = x[i].d * x[i].qs[j];
  2866. }
  2867. }
  2868. }
  2869. void quantize_row_q8_K(const float * restrict x, void * restrict y, int64_t k) {
  2870. quantize_row_q8_K_reference(x, y, k);
  2871. }
  2872. //===================================== Dot ptoducts =================================
  2873. //
  2874. // Helper functions
  2875. //
  2876. #if __AVX__ || __AVX2__ || __AVX512F__
  2877. // shuffles to pick the required scales in dot products
  2878. static inline __m256i get_scale_shuffle_q3k(int i) {
  2879. static const uint8_t k_shuffle[128] = {
  2880. 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,
  2881. 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,
  2882. 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,
  2883. 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,
  2884. };
  2885. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2886. }
  2887. static inline __m256i get_scale_shuffle_k4(int i) {
  2888. static const uint8_t k_shuffle[256] = {
  2889. 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,
  2890. 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,
  2891. 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,
  2892. 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,
  2893. 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,
  2894. 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,
  2895. 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,
  2896. 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
  2897. };
  2898. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2899. }
  2900. static inline __m128i get_scale_shuffle(int i) {
  2901. static const uint8_t k_shuffle[128] = {
  2902. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2903. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  2904. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  2905. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  2906. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  2907. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  2908. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  2909. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  2910. };
  2911. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  2912. }
  2913. #endif
  2914. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  2915. const int qk = QK8_0;
  2916. const int nb = n / qk;
  2917. assert(n % qk == 0);
  2918. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2919. assert((nrc == 2) || (nrc == 1));
  2920. #else
  2921. assert(nrc == 1);
  2922. #endif
  2923. UNUSED(nrc);
  2924. UNUSED(bx);
  2925. UNUSED(by);
  2926. UNUSED(bs);
  2927. const block_q4_0 * restrict x = vx;
  2928. const block_q8_0 * restrict y = vy;
  2929. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2930. if (nrc == 2) {
  2931. const block_q4_0 * restrict vx0 = vx;
  2932. const block_q4_0 * restrict vx1 = vx + bx;
  2933. const block_q8_0 * restrict vy0 = vy;
  2934. const block_q8_0 * restrict vy1 = vy + by;
  2935. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2936. for (int i = 0; i < nb; i++) {
  2937. const block_q4_0 * restrict b_x0 = &vx0[i];
  2938. const block_q4_0 * restrict b_x1 = &vx1[i];
  2939. const block_q8_0 * restrict b_y0 = &vy0[i];
  2940. const block_q8_0 * restrict b_y1 = &vy1[i];
  2941. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2942. const int8x16_t s8b = vdupq_n_s8(0x8);
  2943. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  2944. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  2945. // 4-bit -> 8-bit
  2946. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2947. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2948. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2949. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2950. // sub 8
  2951. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  2952. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  2953. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  2954. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  2955. // load y
  2956. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  2957. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  2958. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  2959. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  2960. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  2961. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  2962. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  2963. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  2964. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  2965. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  2966. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  2967. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  2968. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  2969. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  2970. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  2971. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  2972. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  2973. l1, r1)), l2, r2)), l3, r3))), scale);
  2974. }
  2975. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  2976. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  2977. vst1_f32(s, vget_low_f32(sumv2));
  2978. vst1_f32(s + bs, vget_high_f32(sumv2));
  2979. return;
  2980. }
  2981. #endif
  2982. #if defined(__ARM_NEON)
  2983. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2984. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2985. assert(nb % 2 == 0); // TODO: handle odd nb
  2986. for (int i = 0; i < nb; i += 2) {
  2987. const block_q4_0 * restrict x0 = &x[i + 0];
  2988. const block_q4_0 * restrict x1 = &x[i + 1];
  2989. const block_q8_0 * restrict y0 = &y[i + 0];
  2990. const block_q8_0 * restrict y1 = &y[i + 1];
  2991. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2992. const int8x16_t s8b = vdupq_n_s8(0x8);
  2993. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2994. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2995. // 4-bit -> 8-bit
  2996. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2997. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2998. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2999. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3000. // sub 8
  3001. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3002. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3003. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3004. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3005. // load y
  3006. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3007. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3008. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3009. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3010. // dot product into int32x4_t
  3011. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3012. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3013. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3014. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3015. }
  3016. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3017. #elif defined(__AVX2__)
  3018. // Initialize accumulator with zeros
  3019. __m256 acc = _mm256_setzero_ps();
  3020. // Main loop
  3021. for (int i = 0; i < nb; ++i) {
  3022. /* Compute combined scale for the block */
  3023. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3024. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3025. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3026. const __m256i off = _mm256_set1_epi8( 8 );
  3027. qx = _mm256_sub_epi8( qx, off );
  3028. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3029. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3030. /* Multiply q with scale and accumulate */
  3031. acc = _mm256_fmadd_ps( d, q, acc );
  3032. }
  3033. *s = hsum_float_8(acc);
  3034. #elif defined(__AVX__)
  3035. // Initialize accumulator with zeros
  3036. __m256 acc = _mm256_setzero_ps();
  3037. // Main loop
  3038. for (int i = 0; i < nb; ++i) {
  3039. // Compute combined scale for the block
  3040. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3041. const __m128i lowMask = _mm_set1_epi8(0xF);
  3042. const __m128i off = _mm_set1_epi8(8);
  3043. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3044. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3045. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3046. bx_0 = _mm_sub_epi8(bx_0, off);
  3047. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3048. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3049. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3050. bx_0 = _mm_sub_epi8(bx_0, off);
  3051. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3052. // Convert int32_t to float
  3053. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3054. // Apply the scale, and accumulate
  3055. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3056. }
  3057. *s = hsum_float_8(acc);
  3058. #elif defined(__SSSE3__)
  3059. // set constants
  3060. const __m128i lowMask = _mm_set1_epi8(0xF);
  3061. const __m128i off = _mm_set1_epi8(8);
  3062. // Initialize accumulator with zeros
  3063. __m128 acc_0 = _mm_setzero_ps();
  3064. __m128 acc_1 = _mm_setzero_ps();
  3065. __m128 acc_2 = _mm_setzero_ps();
  3066. __m128 acc_3 = _mm_setzero_ps();
  3067. // First round without accumulation
  3068. {
  3069. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3070. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3071. // Compute combined scale for the block 0 and 1
  3072. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3073. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3074. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3075. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3076. bx_0 = _mm_sub_epi8(bx_0, off);
  3077. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3078. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3079. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3080. bx_1 = _mm_sub_epi8(bx_1, off);
  3081. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3082. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3083. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3084. // Compute combined scale for the block 2 and 3
  3085. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3086. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3087. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3088. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3089. bx_2 = _mm_sub_epi8(bx_2, off);
  3090. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3091. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3092. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3093. bx_3 = _mm_sub_epi8(bx_3, off);
  3094. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3095. // Convert int32_t to float
  3096. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3097. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3098. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3099. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3100. // Apply the scale
  3101. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3102. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3103. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3104. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3105. }
  3106. assert(nb % 2 == 0); // TODO: handle odd nb
  3107. // Main loop
  3108. for (int i = 2; i < nb; i+=2) {
  3109. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3110. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3111. // Compute combined scale for the block 0 and 1
  3112. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3113. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3114. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3115. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3116. bx_0 = _mm_sub_epi8(bx_0, off);
  3117. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3118. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3119. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3120. bx_1 = _mm_sub_epi8(bx_1, off);
  3121. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3122. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3123. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3124. // Compute combined scale for the block 2 and 3
  3125. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3126. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3127. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3128. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3129. bx_2 = _mm_sub_epi8(bx_2, off);
  3130. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3131. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3132. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3133. bx_3 = _mm_sub_epi8(bx_3, off);
  3134. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3135. // Convert int32_t to float
  3136. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3137. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3138. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3139. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3140. // Apply the scale
  3141. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3142. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3143. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3144. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3145. // Acummulate
  3146. acc_0 = _mm_add_ps(p0_d, acc_0);
  3147. acc_1 = _mm_add_ps(p1_d, acc_1);
  3148. acc_2 = _mm_add_ps(p2_d, acc_2);
  3149. acc_3 = _mm_add_ps(p3_d, acc_3);
  3150. }
  3151. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3152. #elif defined(__riscv_v_intrinsic)
  3153. float sumf = 0.0;
  3154. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3155. for (int i = 0; i < nb; i++) {
  3156. // load elements
  3157. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3158. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3159. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3160. // mask and store lower part of x, and then upper part
  3161. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3162. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3163. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3164. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3165. // subtract offset
  3166. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3167. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3168. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3169. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3170. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3171. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3172. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3173. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3174. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3175. }
  3176. *s = sumf;
  3177. #else
  3178. // scalar
  3179. float sumf = 0.0;
  3180. for (int i = 0; i < nb; i++) {
  3181. int sumi = 0;
  3182. for (int j = 0; j < qk/2; ++j) {
  3183. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3184. const int v1 = (x[i].qs[j] >> 4) - 8;
  3185. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3186. }
  3187. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3188. }
  3189. *s = sumf;
  3190. #endif
  3191. }
  3192. void ggml_vec_dot_q4_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3193. const int qk = QK8_1;
  3194. const int nb = n / qk;
  3195. assert(n % qk == 0);
  3196. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3197. assert((nrc == 2) || (nrc == 1));
  3198. #else
  3199. assert(nrc == 1);
  3200. #endif
  3201. UNUSED(nrc);
  3202. UNUSED(bx);
  3203. UNUSED(by);
  3204. UNUSED(bs);
  3205. const block_q4_1 * restrict x = vx;
  3206. const block_q8_1 * restrict y = vy;
  3207. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3208. if (nrc == 2) {
  3209. const block_q4_1 * restrict vx0 = vx;
  3210. const block_q4_1 * restrict vx1 = vx + bx;
  3211. const block_q8_1 * restrict vy0 = vy;
  3212. const block_q8_1 * restrict vy1 = vy + by;
  3213. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3214. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3215. for (int i = 0; i < nb; i++) {
  3216. const block_q4_1 * restrict b_x0 = &vx0[i];
  3217. const block_q4_1 * restrict b_x1 = &vx1[i];
  3218. const block_q8_1 * restrict b_y0 = &vy0[i];
  3219. const block_q8_1 * restrict b_y1 = &vy1[i];
  3220. float32x4_t summs_t = {GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y0->s),
  3221. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y0->s),
  3222. GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y1->s),
  3223. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y1->s)};
  3224. summs0 += summs_t;
  3225. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3226. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3227. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3228. // 4-bit -> 8-bit
  3229. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3230. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3231. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3232. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3233. // load y
  3234. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3235. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3236. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3237. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3238. // mmla into int32x4_t
  3239. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3240. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3241. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3242. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3243. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3244. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3245. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3246. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3247. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3248. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3249. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3250. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3251. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3252. l1, r1)), l2, r2)), l3, r3))), scale);
  3253. }
  3254. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3255. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3256. sumv2 = sumv2 + summs0;
  3257. vst1_f32(s, vget_low_f32(sumv2));
  3258. vst1_f32(s + bs, vget_high_f32(sumv2));
  3259. return;
  3260. }
  3261. #endif
  3262. // TODO: add WASM SIMD
  3263. #if defined(__ARM_NEON)
  3264. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3265. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3266. float summs = 0;
  3267. assert(nb % 2 == 0); // TODO: handle odd nb
  3268. for (int i = 0; i < nb; i += 2) {
  3269. const block_q4_1 * restrict x0 = &x[i + 0];
  3270. const block_q4_1 * restrict x1 = &x[i + 1];
  3271. const block_q8_1 * restrict y0 = &y[i + 0];
  3272. const block_q8_1 * restrict y1 = &y[i + 1];
  3273. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s) + GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3274. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3275. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3276. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3277. // 4-bit -> 8-bit
  3278. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3279. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3280. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3281. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3282. // load y
  3283. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3284. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3285. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3286. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3287. // dot product into int32x4_t
  3288. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3289. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3290. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3291. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3292. }
  3293. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3294. #elif defined(__AVX2__) || defined(__AVX__)
  3295. // Initialize accumulator with zeros
  3296. __m256 acc = _mm256_setzero_ps();
  3297. float summs = 0;
  3298. // Main loop
  3299. for (int i = 0; i < nb; ++i) {
  3300. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3301. const float d1 = GGML_FP16_TO_FP32(y[i].d);
  3302. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3303. const __m256 d0v = _mm256_set1_ps( d0 );
  3304. const __m256 d1v = _mm256_set1_ps( d1 );
  3305. // Compute combined scales
  3306. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3307. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3308. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3309. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3310. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3311. // Accumulate d0*d1*x*y
  3312. #if defined(__AVX2__)
  3313. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3314. #else
  3315. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3316. #endif
  3317. }
  3318. *s = hsum_float_8(acc) + summs;
  3319. #elif defined(__riscv_v_intrinsic)
  3320. float sumf = 0.0;
  3321. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3322. for (int i = 0; i < nb; i++) {
  3323. // load elements
  3324. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3325. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3326. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3327. // mask and store lower part of x, and then upper part
  3328. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3329. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3330. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3331. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3332. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3333. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3334. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3335. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3336. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3337. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3338. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3339. }
  3340. *s = sumf;
  3341. #else
  3342. // scalar
  3343. float sumf = 0.0;
  3344. for (int i = 0; i < nb; i++) {
  3345. int sumi = 0;
  3346. for (int j = 0; j < qk/2; ++j) {
  3347. const int v0 = (x[i].qs[j] & 0x0F);
  3348. const int v1 = (x[i].qs[j] >> 4);
  3349. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3350. }
  3351. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3352. }
  3353. *s = sumf;
  3354. #endif
  3355. }
  3356. void ggml_vec_dot_q5_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3357. const int qk = QK8_0;
  3358. const int nb = n / qk;
  3359. assert(n % qk == 0);
  3360. assert(qk == QK5_0);
  3361. assert(nrc == 1);
  3362. UNUSED(nrc);
  3363. UNUSED(bx);
  3364. UNUSED(by);
  3365. UNUSED(bs);
  3366. const block_q5_0 * restrict x = vx;
  3367. const block_q8_0 * restrict y = vy;
  3368. #if defined(__ARM_NEON)
  3369. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3370. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3371. uint32_t qh0;
  3372. uint32_t qh1;
  3373. uint64_t tmp0[4];
  3374. uint64_t tmp1[4];
  3375. assert(nb % 2 == 0); // TODO: handle odd nb
  3376. for (int i = 0; i < nb; i += 2) {
  3377. const block_q5_0 * restrict x0 = &x[i];
  3378. const block_q5_0 * restrict x1 = &x[i + 1];
  3379. const block_q8_0 * restrict y0 = &y[i];
  3380. const block_q8_0 * restrict y1 = &y[i + 1];
  3381. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3382. // extract the 5th bit via lookup table ((!b) << 4)
  3383. memcpy(&qh0, x0->qh, sizeof(qh0));
  3384. memcpy(&qh1, x1->qh, sizeof(qh1));
  3385. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3386. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3387. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3388. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3389. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3390. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3391. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3392. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3393. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3394. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3395. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3396. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3397. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3398. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3399. // 4-bit -> 8-bit
  3400. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3401. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3402. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3403. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3404. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3405. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3406. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3407. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3408. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3409. // load y
  3410. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3411. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3412. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3413. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3414. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3415. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3416. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3417. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3418. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3419. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3420. }
  3421. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3422. #elif defined(__wasm_simd128__)
  3423. v128_t sumv = wasm_f32x4_splat(0.0f);
  3424. uint32_t qh;
  3425. uint64_t tmp[4];
  3426. // TODO: check if unrolling this is better
  3427. for (int i = 0; i < nb; ++i) {
  3428. const block_q5_0 * restrict x0 = &x[i];
  3429. const block_q8_0 * restrict y0 = &y[i];
  3430. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3431. // extract the 5th bit
  3432. memcpy(&qh, x0->qh, sizeof(qh));
  3433. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3434. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3435. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3436. tmp[3] = table_b2b_1[(qh >> 24) ];
  3437. const v128_t qhl = wasm_v128_load(tmp + 0);
  3438. const v128_t qhh = wasm_v128_load(tmp + 2);
  3439. const v128_t v0 = wasm_v128_load(x0->qs);
  3440. // 4-bit -> 8-bit
  3441. const v128_t v0l = wasm_v128_and (v0, m4b);
  3442. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3443. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3444. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3445. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3446. // load y
  3447. const v128_t v1l = wasm_v128_load(y0->qs);
  3448. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3449. // int8x16 -> int16x8
  3450. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3451. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3452. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3453. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3454. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3455. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3456. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3457. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3458. // dot product
  3459. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3460. wasm_i32x4_add(
  3461. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3462. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3463. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3464. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3465. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3466. }
  3467. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3468. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3469. #elif defined(__AVX2__)
  3470. // Initialize accumulator with zeros
  3471. __m256 acc = _mm256_setzero_ps();
  3472. // Main loop
  3473. for (int i = 0; i < nb; i++) {
  3474. /* Compute combined scale for the block */
  3475. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3476. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3477. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3478. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3479. qx = _mm256_or_si256(qx, bxhi);
  3480. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3481. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3482. /* Multiply q with scale and accumulate */
  3483. acc = _mm256_fmadd_ps(d, q, acc);
  3484. }
  3485. *s = hsum_float_8(acc);
  3486. #elif defined(__AVX__)
  3487. // Initialize accumulator with zeros
  3488. __m256 acc = _mm256_setzero_ps();
  3489. __m128i mask = _mm_set1_epi8((char)0xF0);
  3490. // Main loop
  3491. for (int i = 0; i < nb; i++) {
  3492. /* Compute combined scale for the block */
  3493. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3494. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3495. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3496. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3497. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3498. bxhil = _mm_andnot_si128(bxhil, mask);
  3499. bxhih = _mm_andnot_si128(bxhih, mask);
  3500. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3501. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3502. bxl = _mm_or_si128(bxl, bxhil);
  3503. bxh = _mm_or_si128(bxh, bxhih);
  3504. bx_0 = MM256_SET_M128I(bxh, bxl);
  3505. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3506. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3507. /* Multiply q with scale and accumulate */
  3508. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3509. }
  3510. *s = hsum_float_8(acc);
  3511. #elif defined(__riscv_v_intrinsic)
  3512. float sumf = 0.0;
  3513. uint32_t qh;
  3514. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3515. // These temporary registers are for masking and shift operations
  3516. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3517. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3518. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3519. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3520. for (int i = 0; i < nb; i++) {
  3521. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3522. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3523. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3524. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3525. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3526. // ((qh & (1u << (j + 16))) >> (j + 12));
  3527. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3528. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3529. // narrowing
  3530. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3531. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3532. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3533. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3534. // load
  3535. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3536. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3537. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3538. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3539. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3540. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3541. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3542. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3543. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3544. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3545. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3546. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3547. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3548. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3549. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3550. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3551. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3552. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3553. }
  3554. *s = sumf;
  3555. #else
  3556. // scalar
  3557. float sumf = 0.0;
  3558. for (int i = 0; i < nb; i++) {
  3559. uint32_t qh;
  3560. memcpy(&qh, x[i].qh, sizeof(qh));
  3561. int sumi = 0;
  3562. for (int j = 0; j < qk/2; ++j) {
  3563. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3564. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3565. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3566. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3567. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3568. }
  3569. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3570. }
  3571. *s = sumf;
  3572. #endif
  3573. }
  3574. void ggml_vec_dot_q5_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3575. const int qk = QK8_1;
  3576. const int nb = n / qk;
  3577. assert(n % qk == 0);
  3578. assert(qk == QK5_1);
  3579. assert(nrc == 1);
  3580. UNUSED(nrc);
  3581. UNUSED(bx);
  3582. UNUSED(by);
  3583. UNUSED(bs);
  3584. const block_q5_1 * restrict x = vx;
  3585. const block_q8_1 * restrict y = vy;
  3586. #if defined(__ARM_NEON)
  3587. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3588. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3589. float summs0 = 0.0f;
  3590. float summs1 = 0.0f;
  3591. uint32_t qh0;
  3592. uint32_t qh1;
  3593. uint64_t tmp0[4];
  3594. uint64_t tmp1[4];
  3595. assert(nb % 2 == 0); // TODO: handle odd nb
  3596. for (int i = 0; i < nb; i += 2) {
  3597. const block_q5_1 * restrict x0 = &x[i];
  3598. const block_q5_1 * restrict x1 = &x[i + 1];
  3599. const block_q8_1 * restrict y0 = &y[i];
  3600. const block_q8_1 * restrict y1 = &y[i + 1];
  3601. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3602. summs0 += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3603. summs1 += GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3604. // extract the 5th bit via lookup table ((b) << 4)
  3605. memcpy(&qh0, x0->qh, sizeof(qh0));
  3606. memcpy(&qh1, x1->qh, sizeof(qh1));
  3607. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3608. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3609. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3610. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3611. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3612. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3613. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3614. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3615. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3616. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3617. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3618. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3619. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3620. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3621. // 4-bit -> 8-bit
  3622. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3623. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3624. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3625. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3626. // add high bit
  3627. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3628. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3629. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3630. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3631. // load y
  3632. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3633. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3634. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3635. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3636. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3637. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3638. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3639. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3640. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3641. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3642. }
  3643. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3644. #elif defined(__wasm_simd128__)
  3645. v128_t sumv = wasm_f32x4_splat(0.0f);
  3646. float summs = 0.0f;
  3647. uint32_t qh;
  3648. uint64_t tmp[4];
  3649. // TODO: check if unrolling this is better
  3650. for (int i = 0; i < nb; ++i) {
  3651. const block_q5_1 * restrict x0 = &x[i];
  3652. const block_q8_1 * restrict y0 = &y[i];
  3653. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3654. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3655. // extract the 5th bit
  3656. memcpy(&qh, x0->qh, sizeof(qh));
  3657. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3658. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3659. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3660. tmp[3] = table_b2b_0[(qh >> 24) ];
  3661. const v128_t qhl = wasm_v128_load(tmp + 0);
  3662. const v128_t qhh = wasm_v128_load(tmp + 2);
  3663. const v128_t v0 = wasm_v128_load(x0->qs);
  3664. // 4-bit -> 8-bit
  3665. const v128_t v0l = wasm_v128_and (v0, m4b);
  3666. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3667. // add high bit
  3668. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3669. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3670. // load y
  3671. const v128_t v1l = wasm_v128_load(y0->qs);
  3672. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3673. // int8x16 -> int16x8
  3674. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3675. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3676. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3677. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3678. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3679. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3680. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3681. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3682. // dot product
  3683. sumv = wasm_f32x4_add(sumv,
  3684. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3685. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3686. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3687. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3688. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3689. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3690. }
  3691. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3692. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3693. #elif defined(__AVX2__)
  3694. // Initialize accumulator with zeros
  3695. __m256 acc = _mm256_setzero_ps();
  3696. float summs = 0.0f;
  3697. // Main loop
  3698. for (int i = 0; i < nb; i++) {
  3699. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3700. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3701. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3702. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3703. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3704. qx = _mm256_or_si256(qx, bxhi);
  3705. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3706. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3707. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  3708. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3709. }
  3710. *s = hsum_float_8(acc) + summs;
  3711. #elif defined(__AVX__)
  3712. // Initialize accumulator with zeros
  3713. __m256 acc = _mm256_setzero_ps();
  3714. __m128i mask = _mm_set1_epi8(0x10);
  3715. float summs = 0.0f;
  3716. // Main loop
  3717. for (int i = 0; i < nb; i++) {
  3718. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3719. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3720. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3721. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3722. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3723. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3724. bxhil = _mm_and_si128(bxhil, mask);
  3725. bxhih = _mm_and_si128(bxhih, mask);
  3726. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3727. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3728. bxl = _mm_or_si128(bxl, bxhil);
  3729. bxh = _mm_or_si128(bxh, bxhih);
  3730. bx_0 = MM256_SET_M128I(bxh, bxl);
  3731. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3732. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3733. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  3734. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3735. }
  3736. *s = hsum_float_8(acc) + summs;
  3737. #elif defined(__riscv_v_intrinsic)
  3738. float sumf = 0.0;
  3739. uint32_t qh;
  3740. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3741. // temporary registers for shift operations
  3742. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3743. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3744. for (int i = 0; i < nb; i++) {
  3745. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3746. // load qh
  3747. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3748. // ((qh >> (j + 0)) << 4) & 0x10;
  3749. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3750. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3751. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3752. // ((qh >> (j + 12)) ) & 0x10;
  3753. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3754. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3755. // narrowing
  3756. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3757. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3758. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3759. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3760. // load
  3761. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3762. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3763. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3764. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3765. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3766. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3767. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3768. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3769. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3770. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3771. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3772. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3773. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3774. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3775. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3776. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3777. }
  3778. *s = sumf;
  3779. #else
  3780. // scalar
  3781. float sumf = 0.0;
  3782. for (int i = 0; i < nb; i++) {
  3783. uint32_t qh;
  3784. memcpy(&qh, x[i].qh, sizeof(qh));
  3785. int sumi = 0;
  3786. for (int j = 0; j < qk/2; ++j) {
  3787. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3788. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3789. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3790. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3791. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3792. }
  3793. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3794. }
  3795. *s = sumf;
  3796. #endif
  3797. }
  3798. void ggml_vec_dot_q8_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3799. const int qk = QK8_0;
  3800. const int nb = n / qk;
  3801. assert(n % qk == 0);
  3802. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3803. assert((nrc == 2) || (nrc == 1));
  3804. #else
  3805. assert(nrc == 1);
  3806. #endif
  3807. UNUSED(nrc);
  3808. UNUSED(bx);
  3809. UNUSED(by);
  3810. UNUSED(bs);
  3811. const block_q8_0 * restrict x = vx;
  3812. const block_q8_0 * restrict y = vy;
  3813. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3814. if (nrc == 2) {
  3815. const block_q8_0 * restrict vx0 = vx;
  3816. const block_q8_0 * restrict vx1 = vx + bx;
  3817. const block_q8_0 * restrict vy0 = vy;
  3818. const block_q8_0 * restrict vy1 = vy + by;
  3819. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3820. for (int i = 0; i < nb; i++) {
  3821. const block_q8_0 * restrict b_x0 = &vx0[i];
  3822. const block_q8_0 * restrict b_y0 = &vy0[i];
  3823. const block_q8_0 * restrict b_x1 = &vx1[i];
  3824. const block_q8_0 * restrict b_y1 = &vy1[i];
  3825. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  3826. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  3827. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  3828. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  3829. // load y
  3830. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3831. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3832. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3833. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3834. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3835. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3836. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3837. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3838. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3839. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3840. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3841. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3842. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3843. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3844. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3845. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3846. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3847. l1, r1)), l2, r2)), l3, r3))), scale);
  3848. }
  3849. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3850. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3851. vst1_f32(s, vget_low_f32(sumv2));
  3852. vst1_f32(s + bs, vget_high_f32(sumv2));
  3853. return;
  3854. }
  3855. #endif
  3856. #if defined(__ARM_NEON)
  3857. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3858. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3859. assert(nb % 2 == 0); // TODO: handle odd nb
  3860. for (int i = 0; i < nb; i += 2) {
  3861. const block_q8_0 * restrict x0 = &x[i + 0];
  3862. const block_q8_0 * restrict x1 = &x[i + 1];
  3863. const block_q8_0 * restrict y0 = &y[i + 0];
  3864. const block_q8_0 * restrict y1 = &y[i + 1];
  3865. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3866. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3867. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3868. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3869. // load y
  3870. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3871. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3872. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3873. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3874. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3875. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3876. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3877. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3878. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3879. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3880. }
  3881. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3882. #elif defined(__AVX2__) || defined(__AVX__)
  3883. // Initialize accumulator with zeros
  3884. __m256 acc = _mm256_setzero_ps();
  3885. // Main loop
  3886. for (int i = 0; i < nb; ++i) {
  3887. // Compute combined scale for the block
  3888. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3889. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3890. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3891. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3892. // Multiply q with scale and accumulate
  3893. #if defined(__AVX2__)
  3894. acc = _mm256_fmadd_ps( d, q, acc );
  3895. #else
  3896. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3897. #endif
  3898. }
  3899. *s = hsum_float_8(acc);
  3900. #elif defined(__riscv_v_intrinsic)
  3901. float sumf = 0.0;
  3902. size_t vl = __riscv_vsetvl_e8m1(qk);
  3903. for (int i = 0; i < nb; i++) {
  3904. // load elements
  3905. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3906. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3907. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  3908. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3909. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3910. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3911. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3912. }
  3913. *s = sumf;
  3914. #else
  3915. // scalar
  3916. float sumf = 0.0;
  3917. for (int i = 0; i < nb; i++) {
  3918. int sumi = 0;
  3919. for (int j = 0; j < qk; j++) {
  3920. sumi += x[i].qs[j]*y[i].qs[j];
  3921. }
  3922. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3923. }
  3924. *s = sumf;
  3925. #endif
  3926. }
  3927. #if QK_K == 256
  3928. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3929. assert(nrc == 1);
  3930. UNUSED(nrc);
  3931. UNUSED(bx);
  3932. UNUSED(by);
  3933. UNUSED(bs);
  3934. const block_q2_K * restrict x = vx;
  3935. const block_q8_K * restrict y = vy;
  3936. const int nb = n / QK_K;
  3937. #ifdef __ARM_NEON
  3938. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3939. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3940. const int32x4_t vzero = vdupq_n_s32(0);
  3941. ggml_int8x16x2_t q2bytes;
  3942. uint8_t aux[16];
  3943. float sum = 0;
  3944. for (int i = 0; i < nb; ++i) {
  3945. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3946. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3947. const uint8_t * restrict q2 = x[i].qs;
  3948. const int8_t * restrict q8 = y[i].qs;
  3949. const uint8_t * restrict sc = x[i].scales;
  3950. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3951. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3952. vst1q_u8(aux, scales);
  3953. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3954. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3955. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3956. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3957. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3958. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3959. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3960. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3961. int isum = 0;
  3962. int is = 0;
  3963. // We use this macro instead of a function call because for some reason
  3964. // the code runs 2-3% slower, even if the function is declared inline
  3965. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3966. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3967. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3968. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3969. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3970. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3971. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3972. MULTIPLY_ACCUM_WITH_SCALE((index));
  3973. for (int j = 0; j < QK_K/128; ++j) {
  3974. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3975. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3976. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3977. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3978. MULTIPLY_ACCUM_WITH_SCALE(0);
  3979. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3980. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3981. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3982. is += 8;
  3983. }
  3984. sum += d * isum;
  3985. }
  3986. *s = sum;
  3987. #elif defined __AVX2__
  3988. const __m256i m3 = _mm256_set1_epi8(3);
  3989. const __m128i m4 = _mm_set1_epi8(0xF);
  3990. __m256 acc = _mm256_setzero_ps();
  3991. for (int i = 0; i < nb; ++i) {
  3992. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3993. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3994. const uint8_t * restrict q2 = x[i].qs;
  3995. const int8_t * restrict q8 = y[i].qs;
  3996. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3997. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3998. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3999. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4000. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4001. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4002. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4003. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4004. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4005. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4006. __m256i sumi = _mm256_setzero_si256();
  4007. for (int j = 0; j < QK_K/128; ++j) {
  4008. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4009. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4010. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4011. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4012. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4013. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4014. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4015. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4016. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4017. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4018. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4019. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4020. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4021. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4022. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4023. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4024. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4025. p0 = _mm256_add_epi32(p0, p1);
  4026. p2 = _mm256_add_epi32(p2, p3);
  4027. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4028. }
  4029. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4030. }
  4031. *s = hsum_float_8(acc);
  4032. #elif defined __AVX__
  4033. const __m128i m3 = _mm_set1_epi8(0x3);
  4034. const __m128i m4 = _mm_set1_epi8(0xF);
  4035. const __m128i m2 = _mm_set1_epi8(0x2);
  4036. __m256 acc = _mm256_setzero_ps();
  4037. for (int i = 0; i < nb; ++i) {
  4038. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4039. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4040. const uint8_t * restrict q2 = x[i].qs;
  4041. const int8_t * restrict q8 = y[i].qs;
  4042. // load mins and scales from block_q2_K.scales[QK_K/16]
  4043. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4044. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4045. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4046. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4047. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4048. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4049. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4050. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4051. // sumf += -dmin * summs in 32bits*8
  4052. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4053. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4054. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4055. const __m128i scales[2] = { scales_0, scales_1 };
  4056. __m128i sumi_0 = _mm_setzero_si128();
  4057. __m128i sumi_1 = _mm_setzero_si128();
  4058. for (int j = 0; j < QK_K/128; ++j) {
  4059. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4060. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4061. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4062. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4063. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4064. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4065. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4066. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4067. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4068. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4069. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4070. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4071. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4072. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4073. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4074. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4075. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4076. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4077. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4078. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4079. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4080. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4081. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4082. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4083. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4084. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4085. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4086. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4087. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4088. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4089. __m128i shuffle = _mm_set1_epi16(0x0100);
  4090. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4091. shuffle = _mm_add_epi16(shuffle, m2);
  4092. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4093. shuffle = _mm_add_epi16(shuffle, m2);
  4094. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4095. shuffle = _mm_add_epi16(shuffle, m2);
  4096. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4097. shuffle = _mm_add_epi16(shuffle, m2);
  4098. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4099. shuffle = _mm_add_epi16(shuffle, m2);
  4100. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4101. shuffle = _mm_add_epi16(shuffle, m2);
  4102. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4103. shuffle = _mm_add_epi16(shuffle, m2);
  4104. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4105. p0 = _mm_add_epi32(p0, p1);
  4106. p2 = _mm_add_epi32(p2, p3);
  4107. p4 = _mm_add_epi32(p4, p5);
  4108. p6 = _mm_add_epi32(p6, p7);
  4109. // isum in 32bits*4*2
  4110. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4111. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4112. }
  4113. // sumf += dall * isum - dmin * summs in 32bits
  4114. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4115. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4116. }
  4117. *s = hsum_float_8(acc);
  4118. #elif defined __riscv_v_intrinsic
  4119. float sumf = 0;
  4120. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4121. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4122. for (int i = 0; i < nb; ++i) {
  4123. const uint8_t * q2 = x[i].qs;
  4124. const int8_t * q8 = y[i].qs;
  4125. const uint8_t * sc = x[i].scales;
  4126. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4127. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4128. size_t vl = 16;
  4129. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4130. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4131. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4132. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4133. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4134. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4135. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4136. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4137. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4138. vl = 32;
  4139. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4140. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4141. uint8_t is=0;
  4142. int isum=0;
  4143. for (int j = 0; j < QK_K/128; ++j) {
  4144. // load Q2
  4145. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4146. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4147. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4148. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4149. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4150. // duplicate scale elements for product
  4151. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4152. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4153. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4154. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4155. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4156. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4157. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4158. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4159. // load Q8
  4160. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4161. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4162. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4163. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4164. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4165. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4166. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4167. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4168. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4169. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4170. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4171. q2+=32; q8+=128; is=8;
  4172. }
  4173. sumf += dall * isum;
  4174. }
  4175. *s = sumf;
  4176. #else
  4177. float sumf = 0;
  4178. for (int i = 0; i < nb; ++i) {
  4179. const uint8_t * q2 = x[i].qs;
  4180. const int8_t * q8 = y[i].qs;
  4181. const uint8_t * sc = x[i].scales;
  4182. int summs = 0;
  4183. for (int j = 0; j < 16; ++j) {
  4184. summs += y[i].bsums[j] * (sc[j] >> 4);
  4185. }
  4186. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4187. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4188. int isum = 0;
  4189. int is = 0;
  4190. int d;
  4191. for (int k = 0; k < QK_K/128; ++k) {
  4192. int shift = 0;
  4193. for (int j = 0; j < 4; ++j) {
  4194. d = sc[is++] & 0xF;
  4195. int isuml = 0;
  4196. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4197. isum += d * isuml;
  4198. d = sc[is++] & 0xF;
  4199. isuml = 0;
  4200. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4201. isum += d * isuml;
  4202. shift += 2;
  4203. q8 += 32;
  4204. }
  4205. q2 += 32;
  4206. }
  4207. sumf += dall * isum - dmin * summs;
  4208. }
  4209. *s = sumf;
  4210. #endif
  4211. }
  4212. #else
  4213. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4214. assert(nrc == 1);
  4215. UNUSED(nrc);
  4216. UNUSED(bx);
  4217. UNUSED(by);
  4218. UNUSED(bs);
  4219. const block_q2_K * restrict x = vx;
  4220. const block_q8_K * restrict y = vy;
  4221. const int nb = n / QK_K;
  4222. #ifdef __ARM_NEON
  4223. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4224. const int32x4_t vzero = vdupq_n_s32(0);
  4225. ggml_int8x16x4_t q2bytes;
  4226. uint32_t aux32[2];
  4227. const uint8_t * scales = (const uint8_t *)aux32;
  4228. float sum = 0;
  4229. for (int i = 0; i < nb; ++i) {
  4230. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4231. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4232. const uint8_t * restrict q2 = x[i].qs;
  4233. const int8_t * restrict q8 = y[i].qs;
  4234. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4235. aux32[0] = sc[0] & 0x0f0f0f0f;
  4236. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4237. 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]);
  4238. int isum1 = 0, isum2 = 0;
  4239. const uint8x16_t q2bits = vld1q_u8(q2);
  4240. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4241. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4242. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4243. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4244. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4245. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4246. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4247. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4248. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4249. sum += d * (isum1 + isum2);
  4250. }
  4251. *s = sum;
  4252. #elif defined __AVX2__
  4253. const __m256i m3 = _mm256_set1_epi8(3);
  4254. __m256 acc = _mm256_setzero_ps();
  4255. uint32_t ud, um;
  4256. const uint8_t * restrict db = (const uint8_t *)&ud;
  4257. const uint8_t * restrict mb = (const uint8_t *)&um;
  4258. float summs = 0;
  4259. // TODO: optimize this
  4260. for (int i = 0; i < nb; ++i) {
  4261. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4262. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4263. const uint8_t * restrict q2 = x[i].qs;
  4264. const int8_t * restrict q8 = y[i].qs;
  4265. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4266. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4267. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4268. 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];
  4269. summs += dmin * smin;
  4270. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4271. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4272. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4273. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4274. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4275. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4276. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4277. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4278. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4279. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4280. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4281. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4282. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4283. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4284. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4285. }
  4286. *s = hsum_float_8(acc) + summs;
  4287. #elif defined __AVX__
  4288. const __m128i m3 = _mm_set1_epi8(3);
  4289. __m256 acc = _mm256_setzero_ps();
  4290. uint32_t ud, um;
  4291. const uint8_t * restrict db = (const uint8_t *)&ud;
  4292. const uint8_t * restrict mb = (const uint8_t *)&um;
  4293. float summs = 0;
  4294. // TODO: optimize this
  4295. for (int i = 0; i < nb; ++i) {
  4296. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4297. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4298. const uint8_t * restrict q2 = x[i].qs;
  4299. const int8_t * restrict q8 = y[i].qs;
  4300. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4301. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4302. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4303. 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];
  4304. summs += dmin * smin;
  4305. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4306. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4307. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4308. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4309. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4310. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4311. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4312. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4313. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4314. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4315. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4316. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4317. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4318. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4319. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4320. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4321. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4322. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4323. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4324. }
  4325. *s = hsum_float_8(acc) + summs;
  4326. #elif defined __riscv_v_intrinsic
  4327. uint32_t aux32[2];
  4328. const uint8_t * scales = (const uint8_t *)aux32;
  4329. float sumf = 0;
  4330. for (int i = 0; i < nb; ++i) {
  4331. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4332. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4333. const uint8_t * restrict q2 = x[i].qs;
  4334. const int8_t * restrict q8 = y[i].qs;
  4335. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4336. aux32[0] = sc[0] & 0x0f0f0f0f;
  4337. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4338. 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]);
  4339. int isum1 = 0;
  4340. int isum2 = 0;
  4341. size_t vl = 16;
  4342. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4343. // load Q2
  4344. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4345. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4346. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4347. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4348. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4349. // load Q8, and take product with Q2
  4350. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4351. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4352. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4353. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4354. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4355. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4356. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4357. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4358. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4359. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4360. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4361. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4362. sumf += d * (isum1 + isum2);
  4363. }
  4364. *s = sumf;
  4365. #else
  4366. float sumf = 0;
  4367. int isum[QK_K/16];
  4368. for (int i = 0; i < nb; ++i) {
  4369. const uint8_t * q2 = x[i].qs;
  4370. const int8_t * q8 = y[i].qs;
  4371. const uint8_t * sc = x[i].scales;
  4372. int summs = 0;
  4373. for (int j = 0; j < QK_K/16; ++j) {
  4374. summs += y[i].bsums[j] * (sc[j] >> 4);
  4375. }
  4376. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4377. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4378. memset(isum, 0, (QK_K/16)*sizeof(int));
  4379. for (int l = 0; l < 16; ++l) {
  4380. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4381. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4382. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4383. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4384. }
  4385. for (int l = 0; l < QK_K/16; ++l) {
  4386. isum[l] *= (sc[l] & 0xF);
  4387. }
  4388. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4389. }
  4390. *s = sumf;
  4391. #endif
  4392. }
  4393. #endif
  4394. #if QK_K == 256
  4395. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4396. assert(n % QK_K == 0);
  4397. assert(nrc == 1);
  4398. UNUSED(nrc);
  4399. UNUSED(bx);
  4400. UNUSED(by);
  4401. UNUSED(bs);
  4402. const uint32_t kmask1 = 0x03030303;
  4403. const uint32_t kmask2 = 0x0f0f0f0f;
  4404. const block_q3_K * restrict x = vx;
  4405. const block_q8_K * restrict y = vy;
  4406. const int nb = n / QK_K;
  4407. #ifdef __ARM_NEON
  4408. uint32_t aux[3];
  4409. uint32_t utmp[4];
  4410. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4411. const int32x4_t vzero = vdupq_n_s32(0);
  4412. const uint8x16_t m0 = vdupq_n_u8(1);
  4413. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4414. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4415. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4416. const int8_t m32 = 32;
  4417. ggml_int8x16x4_t q3bytes;
  4418. float sum = 0;
  4419. for (int i = 0; i < nb; ++i) {
  4420. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4421. const uint8_t * restrict q3 = x[i].qs;
  4422. const uint8_t * restrict qh = x[i].hmask;
  4423. const int8_t * restrict q8 = y[i].qs;
  4424. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4425. ggml_uint8x16x4_t q3h;
  4426. int32_t isum = 0;
  4427. // Set up scales
  4428. memcpy(aux, x[i].scales, 12);
  4429. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4430. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4431. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4432. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4433. int8_t * scale = (int8_t *)utmp;
  4434. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4435. for (int j = 0; j < QK_K/128; ++j) {
  4436. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4437. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4438. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4439. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4440. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4441. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4442. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4443. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4444. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4445. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4446. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4447. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4448. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4449. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4450. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4451. scale += 4;
  4452. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4453. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4454. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4455. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4456. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4457. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4458. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4459. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4460. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4461. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4462. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4463. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4464. scale += 4;
  4465. if (j == 0) {
  4466. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4467. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4468. }
  4469. }
  4470. sum += d * isum;
  4471. }
  4472. *s = sum;
  4473. #elif defined __AVX2__
  4474. const __m256i m3 = _mm256_set1_epi8(3);
  4475. const __m256i mone = _mm256_set1_epi8(1);
  4476. const __m128i m32 = _mm_set1_epi8(32);
  4477. __m256 acc = _mm256_setzero_ps();
  4478. uint32_t aux[3];
  4479. for (int i = 0; i < nb; ++i) {
  4480. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4481. const uint8_t * restrict q3 = x[i].qs;
  4482. const int8_t * restrict q8 = y[i].qs;
  4483. // Set up scales
  4484. memcpy(aux, x[i].scales, 12);
  4485. __m128i scales128 = _mm_set_epi32(
  4486. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4487. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4488. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4489. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4490. scales128 = _mm_sub_epi8(scales128, m32);
  4491. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4492. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4493. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4494. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4495. // high bit
  4496. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4497. // integer accumulator
  4498. __m256i sumi = _mm256_setzero_si256();
  4499. int bit = 0;
  4500. int is = 0;
  4501. for (int j = 0; j < QK_K/128; ++j) {
  4502. // load low 2 bits
  4503. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4504. // prepare low and high bits
  4505. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4506. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4507. ++bit;
  4508. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4509. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4510. ++bit;
  4511. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4512. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4513. ++bit;
  4514. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4515. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4516. ++bit;
  4517. // load Q8 quants
  4518. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4519. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4520. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4521. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4522. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4523. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4524. // and 2 if the high bit was set)
  4525. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4526. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4527. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4528. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4529. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4530. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4531. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4532. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4533. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4534. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4535. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4536. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4537. // multiply with scales
  4538. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4539. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4540. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4541. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4542. // accumulate
  4543. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4544. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4545. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4546. }
  4547. // multiply with block scale and accumulate
  4548. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4549. }
  4550. *s = hsum_float_8(acc);
  4551. #elif defined __AVX__
  4552. const __m128i m3 = _mm_set1_epi8(3);
  4553. const __m128i mone = _mm_set1_epi8(1);
  4554. const __m128i m32 = _mm_set1_epi8(32);
  4555. const __m128i m2 = _mm_set1_epi8(2);
  4556. __m256 acc = _mm256_setzero_ps();
  4557. const uint32_t *aux;
  4558. for (int i = 0; i < nb; ++i) {
  4559. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4560. const uint8_t * restrict q3 = x[i].qs;
  4561. const int8_t * restrict q8 = y[i].qs;
  4562. // Set up scales
  4563. aux = (const uint32_t *)x[i].scales;
  4564. __m128i scales128 = _mm_set_epi32(
  4565. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4566. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4567. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4568. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4569. scales128 = _mm_sub_epi8(scales128, m32);
  4570. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4571. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4572. const __m128i scales[2] = { scales_0, scales_1 };
  4573. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4574. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4575. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4576. // integer accumulator
  4577. __m128i sumi_0 = _mm_setzero_si128();
  4578. __m128i sumi_1 = _mm_setzero_si128();
  4579. for (int j = 0; j < QK_K/128; ++j) {
  4580. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4581. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4582. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4583. // prepare low and high bits
  4584. const int bit = j << 2;
  4585. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4586. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4587. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4588. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4589. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4590. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4591. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4592. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4593. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4594. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4595. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4596. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4597. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4598. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4599. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4600. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4601. // load Q8 quants from block_q8_K.qs[QK_K]
  4602. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4603. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4604. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4605. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4606. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4607. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4608. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4609. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4610. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4611. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4612. // and 2 if the high bit was set)
  4613. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4614. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4615. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4616. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4617. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4618. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4619. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4620. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4621. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4622. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4623. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4624. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4625. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4626. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4627. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4628. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4629. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4630. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4631. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4632. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4633. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4634. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4635. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4636. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4637. // multiply with scales
  4638. __m128i shuffle = _mm_set1_epi16(0x0100);
  4639. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4640. shuffle = _mm_add_epi16(shuffle, m2);
  4641. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4642. shuffle = _mm_add_epi16(shuffle, m2);
  4643. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4644. shuffle = _mm_add_epi16(shuffle, m2);
  4645. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4646. shuffle = _mm_add_epi16(shuffle, m2);
  4647. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4648. shuffle = _mm_add_epi16(shuffle, m2);
  4649. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4650. shuffle = _mm_add_epi16(shuffle, m2);
  4651. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4652. shuffle = _mm_add_epi16(shuffle, m2);
  4653. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4654. // accumulate
  4655. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4656. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4657. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4658. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4659. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4660. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4661. }
  4662. // multiply with block scale and accumulate
  4663. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4664. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4665. }
  4666. *s = hsum_float_8(acc);
  4667. #elif defined __riscv_v_intrinsic
  4668. uint32_t aux[3];
  4669. uint32_t utmp[4];
  4670. float sumf = 0;
  4671. for (int i = 0; i < nb; ++i) {
  4672. const uint8_t * restrict q3 = x[i].qs;
  4673. const uint8_t * restrict qh = x[i].hmask;
  4674. const int8_t * restrict q8 = y[i].qs;
  4675. memcpy(aux, x[i].scales, 12);
  4676. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4677. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4678. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4679. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4680. int8_t * scale = (int8_t *)utmp;
  4681. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4682. size_t vl = 32;
  4683. uint8_t m = 1;
  4684. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4685. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4686. int sum_t = 0;
  4687. for (int j = 0; j < QK_K; j += 128) {
  4688. vl = 32;
  4689. // load Q3
  4690. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4691. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4692. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4693. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4694. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4695. // compute mask for subtraction
  4696. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4697. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4698. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4699. m <<= 1;
  4700. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4701. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4702. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4703. m <<= 1;
  4704. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4705. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4706. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4707. m <<= 1;
  4708. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4709. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4710. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4711. m <<= 1;
  4712. // load Q8 and take product with Q3
  4713. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4714. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4715. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4716. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4717. vl = 16;
  4718. // retrieve lane to multiply with scale
  4719. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4720. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4721. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4722. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4723. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4724. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4725. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4726. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4727. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4728. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4729. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4730. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4731. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4732. q3 += 32; q8 += 128; scale += 8;
  4733. }
  4734. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4735. sumf += d*sum_t;
  4736. }
  4737. *s = sumf;
  4738. #else
  4739. // scalar version
  4740. // This function is written like this so the compiler can manage to vectorize most of it
  4741. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4742. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4743. // The ideal situation would be if we could just write the code once, and the compiler would
  4744. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4745. // write vectorized versions for AVX, ARM_NEON, etc.
  4746. int8_t aux8[QK_K];
  4747. int16_t aux16[8];
  4748. float sums [8];
  4749. int32_t aux32[8];
  4750. memset(sums, 0, 8*sizeof(float));
  4751. uint32_t auxs[4];
  4752. const int8_t * scales = (const int8_t*)auxs;
  4753. float sumf = 0;
  4754. for (int i = 0; i < nb; ++i) {
  4755. const uint8_t * restrict q3 = x[i].qs;
  4756. const uint8_t * restrict hm = x[i].hmask;
  4757. const int8_t * restrict q8 = y[i].qs;
  4758. memset(aux32, 0, 8*sizeof(int32_t));
  4759. int8_t * restrict a = aux8;
  4760. uint8_t m = 1;
  4761. for (int j = 0; j < QK_K; j += 128) {
  4762. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4763. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4764. a += 32; m <<= 1;
  4765. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4766. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4767. a += 32; m <<= 1;
  4768. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4769. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4770. a += 32; m <<= 1;
  4771. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4772. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4773. a += 32; m <<= 1;
  4774. q3 += 32;
  4775. }
  4776. a = aux8;
  4777. memcpy(auxs, x[i].scales, 12);
  4778. uint32_t tmp = auxs[2];
  4779. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4780. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4781. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4782. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4783. for (int j = 0; j < QK_K/16; ++j) {
  4784. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4785. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4786. q8 += 8; a += 8;
  4787. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4788. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4789. q8 += 8; a += 8;
  4790. }
  4791. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4792. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4793. }
  4794. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4795. *s = sumf;
  4796. #endif
  4797. }
  4798. #else
  4799. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4800. assert(n % QK_K == 0);
  4801. assert(nrc == 1);
  4802. UNUSED(nrc);
  4803. UNUSED(bx);
  4804. UNUSED(by);
  4805. UNUSED(bs);
  4806. const block_q3_K * restrict x = vx;
  4807. const block_q8_K * restrict y = vy;
  4808. const int nb = n / QK_K;
  4809. #ifdef __ARM_NEON
  4810. const int32x4_t vzero = vdupq_n_s32(0);
  4811. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4812. const uint8x16_t mh = vdupq_n_u8(4);
  4813. ggml_int8x16x4_t q3bytes;
  4814. uint16_t aux16[2];
  4815. int8_t * scales = (int8_t *)aux16;
  4816. float sum = 0;
  4817. for (int i = 0; i < nb; ++i) {
  4818. ggml_uint8x16x4_t q3h;
  4819. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4820. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4821. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4822. const uint16_t a = *(const uint16_t *)x[i].scales;
  4823. aux16[0] = a & 0x0f0f;
  4824. aux16[1] = (a >> 4) & 0x0f0f;
  4825. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4826. 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]);
  4827. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4828. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4829. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4830. q3h.val[1] = vandq_u8(mh, htmp);
  4831. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4832. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4833. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4834. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4835. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4836. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4837. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4838. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4839. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4840. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4841. sum += d * isum;
  4842. }
  4843. *s = sum;
  4844. #elif defined __AVX2__
  4845. const __m256i m3 = _mm256_set1_epi8(3);
  4846. const __m256i m1 = _mm256_set1_epi8(1);
  4847. __m256 acc = _mm256_setzero_ps();
  4848. uint64_t aux64;
  4849. uint16_t aux16[2];
  4850. const int8_t * aux8 = (const int8_t *)aux16;
  4851. for (int i = 0; i < nb; ++i) {
  4852. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4853. const uint8_t * restrict q3 = x[i].qs;
  4854. const int8_t * restrict q8 = y[i].qs;
  4855. const uint16_t a = *(const uint16_t *)x[i].scales;
  4856. aux16[0] = a & 0x0f0f;
  4857. aux16[1] = (a >> 4) & 0x0f0f;
  4858. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4859. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4860. memcpy(&aux64, x[i].hmask, 8);
  4861. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4862. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4863. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4864. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4865. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4866. // load low 2 bits
  4867. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4868. // prepare low and high bits
  4869. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4870. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4871. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4872. // load Q8 quants
  4873. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4874. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4875. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4876. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4877. // and 2 if the high bit was set)
  4878. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4879. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4880. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4881. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4882. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4883. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4884. // multiply with scales
  4885. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4886. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4887. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4888. // multiply with block scale and accumulate
  4889. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4890. }
  4891. *s = hsum_float_8(acc);
  4892. #elif defined __AVX__
  4893. const __m128i m3 = _mm_set1_epi8(3);
  4894. const __m128i m1 = _mm_set1_epi8(1);
  4895. __m256 acc = _mm256_setzero_ps();
  4896. uint64_t aux64;
  4897. uint16_t aux16[2];
  4898. const int8_t * aux8 = (const int8_t *)aux16;
  4899. for (int i = 0; i < nb; ++i) {
  4900. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4901. const uint8_t * restrict q3 = x[i].qs;
  4902. const int8_t * restrict q8 = y[i].qs;
  4903. const uint16_t a = *(const uint16_t *)x[i].scales;
  4904. aux16[0] = a & 0x0f0f;
  4905. aux16[1] = (a >> 4) & 0x0f0f;
  4906. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4907. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4908. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4909. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4910. memcpy(&aux64, x[i].hmask, 8);
  4911. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4912. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4913. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4914. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4915. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4916. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4917. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4918. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4919. // load low 2 bits
  4920. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4921. // prepare low and high bits
  4922. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4923. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4924. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4925. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4926. // load Q8 quants
  4927. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4928. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4929. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4930. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4931. // and 2 if the high bit was set)
  4932. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4933. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4934. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4935. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4936. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4937. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4938. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4939. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4940. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4941. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4942. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4943. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4944. // multiply with scales
  4945. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4946. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4947. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4948. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4949. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4950. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4951. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4952. // multiply with block scale and accumulate
  4953. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4954. }
  4955. *s = hsum_float_8(acc);
  4956. #elif defined __riscv_v_intrinsic
  4957. uint16_t aux16[2];
  4958. int8_t * scales = (int8_t *)aux16;
  4959. float sumf = 0;
  4960. for (int i = 0; i < nb; ++i) {
  4961. const uint8_t * restrict q3 = x[i].qs;
  4962. const int8_t * restrict q8 = y[i].qs;
  4963. const uint16_t a = *(const uint16_t *)x[i].scales;
  4964. aux16[0] = a & 0x0f0f;
  4965. aux16[1] = (a >> 4) & 0x0f0f;
  4966. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4967. 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]);
  4968. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4969. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4970. // load qh
  4971. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4972. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4973. size_t vl = 16;
  4974. // extend and combine both qh_x1 and qh_x2
  4975. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4976. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4977. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4978. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4979. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4980. // load Q3
  4981. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4982. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4983. 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);
  4984. 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);
  4985. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4986. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4987. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4988. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4989. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4990. // load Q8 and take product with Q3
  4991. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4992. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4993. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4994. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4995. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4996. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4997. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4998. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4999. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  5000. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  5001. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  5002. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  5003. sumf += d * isum;
  5004. }
  5005. *s = sumf;
  5006. #else
  5007. int8_t aux8[QK_K];
  5008. int16_t aux16[8];
  5009. float sums [8];
  5010. int32_t aux32[8];
  5011. int32_t scales[4];
  5012. memset(sums, 0, 8*sizeof(float));
  5013. float sumf = 0;
  5014. for (int i = 0; i < nb; ++i) {
  5015. const uint8_t * restrict q3 = x[i].qs;
  5016. const uint8_t * restrict hm = x[i].hmask;
  5017. const int8_t * restrict q8 = y[i].qs;
  5018. int8_t * restrict a = aux8;
  5019. for (int l = 0; l < 8; ++l) {
  5020. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5021. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5022. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5023. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5024. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5025. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5026. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5027. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5028. }
  5029. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5030. scales[1] = (x[i].scales[0] >> 4) - 8;
  5031. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5032. scales[3] = (x[i].scales[1] >> 4) - 8;
  5033. memset(aux32, 0, 8*sizeof(int32_t));
  5034. for (int j = 0; j < QK_K/16; ++j) {
  5035. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5036. q8 += 8; a += 8;
  5037. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5038. q8 += 8; a += 8;
  5039. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5040. }
  5041. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5042. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5043. }
  5044. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5045. *s = sumf;
  5046. #endif
  5047. }
  5048. #endif
  5049. #if QK_K == 256
  5050. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5051. assert(n % QK_K == 0);
  5052. assert(nrc == 1);
  5053. UNUSED(nrc);
  5054. UNUSED(bx);
  5055. UNUSED(by);
  5056. UNUSED(bs);
  5057. const block_q4_K * restrict x = vx;
  5058. const block_q8_K * restrict y = vy;
  5059. const int nb = n / QK_K;
  5060. static const uint32_t kmask1 = 0x3f3f3f3f;
  5061. static const uint32_t kmask2 = 0x0f0f0f0f;
  5062. static const uint32_t kmask3 = 0x03030303;
  5063. uint32_t utmp[4];
  5064. #ifdef __ARM_NEON
  5065. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5066. const int32x4_t mzero = vdupq_n_s32(0);
  5067. ggml_int8x16x2_t q4bytes;
  5068. ggml_int8x16x2_t q8bytes;
  5069. float sumf = 0;
  5070. for (int i = 0; i < nb; ++i) {
  5071. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5072. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5073. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5074. memcpy(utmp, x[i].scales, 12);
  5075. uint32x2_t mins8 = { 0 };
  5076. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5077. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5078. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5079. utmp[0] &= kmask1;
  5080. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5081. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5082. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5083. sumf -= dmin * vaddvq_s32(prod);
  5084. const uint8_t * scales = (const uint8_t *)utmp;
  5085. const uint8_t * restrict q4 = x[i].qs;
  5086. const int8_t * restrict q8 = y[i].qs;
  5087. int32_t sumi1 = 0;
  5088. int32_t sumi2 = 0;
  5089. for (int j = 0; j < QK_K/64; ++j) {
  5090. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5091. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5092. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5093. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5094. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5095. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5096. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5097. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5098. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5099. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5100. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5101. }
  5102. sumf += d * (sumi1 + sumi2);
  5103. }
  5104. *s = sumf;
  5105. #elif defined __AVX2__
  5106. const __m256i m4 = _mm256_set1_epi8(0xF);
  5107. __m256 acc = _mm256_setzero_ps();
  5108. __m128 acc_m = _mm_setzero_ps();
  5109. for (int i = 0; i < nb; ++i) {
  5110. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5111. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5112. memcpy(utmp, x[i].scales, 12);
  5113. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5114. const uint32_t uaux = utmp[1] & kmask1;
  5115. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5116. utmp[2] = uaux;
  5117. utmp[0] &= kmask1;
  5118. const uint8_t * restrict q4 = x[i].qs;
  5119. const int8_t * restrict q8 = y[i].qs;
  5120. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5121. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5122. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5123. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5124. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5125. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5126. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5127. __m256i sumi = _mm256_setzero_si256();
  5128. for (int j = 0; j < QK_K/64; ++j) {
  5129. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5130. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5131. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5132. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5133. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5134. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5135. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5136. p16l = _mm256_madd_epi16(scale_l, p16l);
  5137. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5138. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5139. p16h = _mm256_madd_epi16(scale_h, p16h);
  5140. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5141. sumi = _mm256_add_epi32(sumi, sumj);
  5142. }
  5143. __m256 vd = _mm256_set1_ps(d);
  5144. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5145. }
  5146. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5147. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5148. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5149. #elif defined __AVX__
  5150. const __m128i m4 = _mm_set1_epi8(0xF);
  5151. const __m128i m2 = _mm_set1_epi8(0x2);
  5152. __m256 acc = _mm256_setzero_ps();
  5153. __m128 acc_m = _mm_setzero_ps();
  5154. for (int i = 0; i < nb; ++i) {
  5155. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5156. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5157. const uint8_t * restrict q4 = x[i].qs;
  5158. const int8_t * restrict q8 = y[i].qs;
  5159. memcpy(utmp, x[i].scales, 12);
  5160. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5161. const uint32_t uaux = utmp[1] & kmask1;
  5162. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5163. utmp[2] = uaux;
  5164. utmp[0] &= kmask1;
  5165. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5166. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5167. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5168. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5169. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5170. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5171. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5172. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5173. __m128i sumi_0 = _mm_setzero_si128();
  5174. __m128i sumi_1 = _mm_setzero_si128();
  5175. __m128i shuffle = _mm_set1_epi16(0x0100);
  5176. for (int j = 0; j < QK_K/64; ++j) {
  5177. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5178. shuffle = _mm_add_epi16(shuffle, m2);
  5179. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5180. shuffle = _mm_add_epi16(shuffle, m2);
  5181. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5182. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5183. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5184. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5185. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5186. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5187. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5188. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5189. p16l = _mm_madd_epi16(scale_l, p16l);
  5190. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5191. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5192. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5193. p16l = _mm_madd_epi16(scale_l, p16l);
  5194. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5195. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5196. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5197. p16h = _mm_madd_epi16(scale_h, p16h);
  5198. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5199. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5200. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5201. p16h = _mm_madd_epi16(scale_h, p16h);
  5202. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5203. }
  5204. __m256 vd = _mm256_set1_ps(d);
  5205. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5206. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5207. }
  5208. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5209. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5210. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5211. #elif defined __riscv_v_intrinsic
  5212. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5213. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5214. float sumf = 0;
  5215. for (int i = 0; i < nb; ++i) {
  5216. size_t vl = 8;
  5217. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5218. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5219. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5220. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5221. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5222. memcpy(utmp, x[i].scales, 12);
  5223. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5224. const uint32_t uaux = utmp[1] & kmask1;
  5225. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5226. utmp[2] = uaux;
  5227. utmp[0] &= kmask1;
  5228. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5229. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5230. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5231. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5232. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5233. const uint8_t * restrict q4 = x[i].qs;
  5234. const int8_t * restrict q8 = y[i].qs;
  5235. vl = 32;
  5236. int32_t sum_1 = 0;
  5237. int32_t sum_2 = 0;
  5238. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5239. for (int j = 0; j < QK_K/64; ++j) {
  5240. // load Q4
  5241. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5242. // load Q8 and multiply it with lower Q4 nibble
  5243. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5244. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5245. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5246. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5247. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5248. // load Q8 and multiply it with upper Q4 nibble
  5249. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5250. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5251. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5252. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5253. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5254. q4 += 32; q8 += 64;
  5255. }
  5256. sumf += d*(sum_1 + sum_2);
  5257. }
  5258. *s = sumf;
  5259. #else
  5260. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5261. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5262. int8_t aux8[QK_K];
  5263. int16_t aux16[8];
  5264. float sums [8];
  5265. int32_t aux32[8];
  5266. memset(sums, 0, 8*sizeof(float));
  5267. float sumf = 0;
  5268. for (int i = 0; i < nb; ++i) {
  5269. const uint8_t * restrict q4 = x[i].qs;
  5270. const int8_t * restrict q8 = y[i].qs;
  5271. memset(aux32, 0, 8*sizeof(int32_t));
  5272. int8_t * restrict a = aux8;
  5273. for (int j = 0; j < QK_K/64; ++j) {
  5274. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5275. a += 32;
  5276. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5277. a += 32; q4 += 32;
  5278. }
  5279. memcpy(utmp, x[i].scales, 12);
  5280. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5281. const uint32_t uaux = utmp[1] & kmask1;
  5282. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5283. utmp[2] = uaux;
  5284. utmp[0] &= kmask1;
  5285. int sumi = 0;
  5286. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5287. a = aux8;
  5288. int is = 0;
  5289. for (int j = 0; j < QK_K/32; ++j) {
  5290. int32_t scale = scales[is++];
  5291. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5292. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5293. q8 += 8; a += 8;
  5294. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5295. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5296. q8 += 8; a += 8;
  5297. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5298. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5299. q8 += 8; a += 8;
  5300. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5301. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5302. q8 += 8; a += 8;
  5303. }
  5304. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5305. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5306. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5307. sumf -= dmin * sumi;
  5308. }
  5309. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5310. *s = sumf;
  5311. #endif
  5312. }
  5313. #else
  5314. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5315. assert(n % QK_K == 0);
  5316. assert(nrc == 1);
  5317. UNUSED(nrc);
  5318. UNUSED(bx);
  5319. UNUSED(by);
  5320. UNUSED(bs);
  5321. const block_q4_K * restrict x = vx;
  5322. const block_q8_K * restrict y = vy;
  5323. const int nb = n / QK_K;
  5324. #ifdef __ARM_NEON
  5325. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5326. const int32x4_t mzero = vdupq_n_s32(0);
  5327. float sumf = 0;
  5328. ggml_int8x16x2_t q4bytes;
  5329. ggml_int8x16x4_t q8bytes;
  5330. float sum_mins = 0.f;
  5331. uint16_t aux16[2];
  5332. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5333. for (int i = 0; i < nb; ++i) {
  5334. const uint8_t * restrict q4 = x[i].qs;
  5335. const int8_t * restrict q8 = y[i].qs;
  5336. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5337. aux16[0] = a[0] & 0x0f0f;
  5338. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5339. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5340. sum_mins += y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * summi;
  5341. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5342. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5343. q8bytes = ggml_vld1q_s8_x4(q8);
  5344. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5345. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5346. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5347. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5348. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5349. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5350. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5351. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5352. sumf += d * (sumi1 + sumi2);
  5353. }
  5354. *s = sumf - sum_mins;
  5355. #elif defined __AVX2__
  5356. const __m256i m4 = _mm256_set1_epi8(0xF);
  5357. __m256 acc = _mm256_setzero_ps();
  5358. float summs = 0;
  5359. uint16_t aux16[2];
  5360. const uint8_t * scales = (const uint8_t *)aux16;
  5361. for (int i = 0; i < nb; ++i) {
  5362. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5363. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5364. const __m256 vd = _mm256_set1_ps(d);
  5365. const uint16_t * a = (const uint16_t *)x[i].scales;
  5366. aux16[0] = a[0] & 0x0f0f;
  5367. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5368. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5369. const uint8_t * restrict q4 = x[i].qs;
  5370. const int8_t * restrict q8 = y[i].qs;
  5371. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5372. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5373. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5374. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5375. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5376. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5377. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5378. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5379. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5380. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5381. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5382. }
  5383. *s = hsum_float_8(acc) - summs;
  5384. #elif defined __AVX__
  5385. const __m128i m4 = _mm_set1_epi8(0xF);
  5386. __m256 acc = _mm256_setzero_ps();
  5387. float summs = 0;
  5388. uint16_t aux16[2];
  5389. const uint8_t * scales = (const uint8_t *)aux16;
  5390. for (int i = 0; i < nb; ++i) {
  5391. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5392. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5393. const __m256 vd = _mm256_set1_ps(d);
  5394. const uint16_t * a = (const uint16_t *)x[i].scales;
  5395. aux16[0] = a[0] & 0x0f0f;
  5396. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5397. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5398. const uint8_t * restrict q4 = x[i].qs;
  5399. const int8_t * restrict q8 = y[i].qs;
  5400. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5401. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5402. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5403. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5404. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5405. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5406. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5407. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5408. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5409. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5410. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5411. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5412. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5413. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5414. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5415. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5416. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5417. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5418. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5419. }
  5420. *s = hsum_float_8(acc) - summs;
  5421. #elif defined __riscv_v_intrinsic
  5422. uint16_t s16[2];
  5423. const uint8_t * restrict scales = (const uint8_t *)s16;
  5424. float sumf = 0;
  5425. for (int i = 0; i < nb; ++i) {
  5426. const uint8_t * restrict q4 = x[i].qs;
  5427. const int8_t * restrict q8 = y[i].qs;
  5428. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5429. s16[0] = b[0] & 0x0f0f;
  5430. s16[1] = (b[0] >> 4) & 0x0f0f;
  5431. 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]));
  5432. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5433. size_t vl = 32;
  5434. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5435. // load Q4
  5436. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5437. // load Q8 and multiply it with lower Q4 nibble
  5438. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5439. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5440. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5441. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5442. // load Q8 and multiply it with upper Q4 nibble
  5443. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5444. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5445. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5446. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5447. }
  5448. *s = sumf;
  5449. #else
  5450. uint8_t aux8[QK_K];
  5451. int16_t aux16[16];
  5452. float sums [8];
  5453. memset(sums, 0, 8*sizeof(float));
  5454. uint16_t s16[2];
  5455. const uint8_t * restrict scales = (const uint8_t *)s16;
  5456. float sumf = 0;
  5457. for (int i = 0; i < nb; ++i) {
  5458. const uint8_t * restrict q4 = x[i].qs;
  5459. const int8_t * restrict q8 = y[i].qs;
  5460. uint8_t * restrict a = aux8;
  5461. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5462. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5463. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5464. s16[0] = b[0] & 0x0f0f;
  5465. s16[1] = (b[0] >> 4) & 0x0f0f;
  5466. 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]));
  5467. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5468. for (int j = 0; j < QK_K/32; ++j) {
  5469. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5470. q8 += 16; a += 16;
  5471. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5472. q8 += 16; a += 16;
  5473. const float dl = d * scales[j];
  5474. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5475. }
  5476. }
  5477. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5478. *s = sumf;
  5479. #endif
  5480. }
  5481. #endif
  5482. #if QK_K == 256
  5483. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5484. assert(n % QK_K == 0);
  5485. assert(nrc == 1);
  5486. UNUSED(nrc);
  5487. UNUSED(bx);
  5488. UNUSED(by);
  5489. UNUSED(bs);
  5490. const block_q5_K * restrict x = vx;
  5491. const block_q8_K * restrict y = vy;
  5492. const int nb = n / QK_K;
  5493. static const uint32_t kmask1 = 0x3f3f3f3f;
  5494. static const uint32_t kmask2 = 0x0f0f0f0f;
  5495. static const uint32_t kmask3 = 0x03030303;
  5496. uint32_t utmp[4];
  5497. #ifdef __ARM_NEON
  5498. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5499. const uint8x16_t mone = vdupq_n_u8(1);
  5500. const uint8x16_t mtwo = vdupq_n_u8(2);
  5501. const int32x4_t mzero = vdupq_n_s32(0);
  5502. ggml_int8x16x4_t q5bytes;
  5503. float sumf = 0;
  5504. for (int i = 0; i < nb; ++i) {
  5505. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5506. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5507. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5508. memcpy(utmp, x[i].scales, 12);
  5509. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5510. const uint32_t uaux = utmp[1] & kmask1;
  5511. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5512. utmp[2] = uaux;
  5513. utmp[0] &= kmask1;
  5514. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5515. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5516. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5517. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5518. int32_t sumi_mins = vaddvq_s32(prod);
  5519. const uint8_t * scales = (const uint8_t *)utmp;
  5520. const uint8_t * restrict q5 = x[i].qs;
  5521. const uint8_t * restrict qh = x[i].qh;
  5522. const int8_t * restrict q8 = y[i].qs;
  5523. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5524. ggml_uint8x16x4_t q5h;
  5525. int32_t sumi = 0;
  5526. for (int j = 0; j < QK_K/64; ++j) {
  5527. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5528. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5529. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5530. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5531. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5532. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5533. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5534. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5535. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5536. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5537. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5538. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5539. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5540. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5541. }
  5542. sumf += d * sumi - dmin * sumi_mins;
  5543. }
  5544. *s = sumf;
  5545. #elif defined __AVX2__
  5546. const __m256i m4 = _mm256_set1_epi8(0xF);
  5547. const __m128i mzero = _mm_setzero_si128();
  5548. const __m256i mone = _mm256_set1_epi8(1);
  5549. __m256 acc = _mm256_setzero_ps();
  5550. float summs = 0.f;
  5551. for (int i = 0; i < nb; ++i) {
  5552. const uint8_t * restrict q5 = x[i].qs;
  5553. const int8_t * restrict q8 = y[i].qs;
  5554. #if QK_K == 256
  5555. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5556. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5557. memcpy(utmp, x[i].scales, 12);
  5558. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5559. const uint32_t uaux = utmp[1] & kmask1;
  5560. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5561. utmp[2] = uaux;
  5562. utmp[0] &= kmask1;
  5563. #else
  5564. // TODO
  5565. const float d = 0, dmin = 0;
  5566. #endif
  5567. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5568. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5569. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5570. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5571. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5572. summs += dmin * _mm_extract_epi32(hsum, 0);
  5573. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5574. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5575. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5576. __m256i hmask = mone;
  5577. __m256i sumi = _mm256_setzero_si256();
  5578. int bit = 0;
  5579. for (int j = 0; j < QK_K/64; ++j) {
  5580. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5581. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5582. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5583. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5584. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5585. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5586. hmask = _mm256_slli_epi16(hmask, 1);
  5587. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5588. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5589. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5590. hmask = _mm256_slli_epi16(hmask, 1);
  5591. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5592. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5593. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5594. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5595. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5596. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5597. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5598. }
  5599. __m256 vd = _mm256_set1_ps(d);
  5600. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5601. }
  5602. *s = hsum_float_8(acc) + summs;
  5603. #elif defined __AVX__
  5604. const __m128i m4 = _mm_set1_epi8(0xF);
  5605. const __m128i mzero = _mm_setzero_si128();
  5606. const __m128i mone = _mm_set1_epi8(1);
  5607. const __m128i m2 = _mm_set1_epi8(2);
  5608. __m256 acc = _mm256_setzero_ps();
  5609. float summs = 0.f;
  5610. for (int i = 0; i < nb; ++i) {
  5611. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5612. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5613. const uint8_t * restrict q5 = x[i].qs;
  5614. const int8_t * restrict q8 = y[i].qs;
  5615. memcpy(utmp, x[i].scales, 12);
  5616. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5617. const uint32_t uaux = utmp[1] & kmask1;
  5618. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5619. utmp[2] = uaux;
  5620. utmp[0] &= kmask1;
  5621. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5622. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5623. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5624. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5625. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5626. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5627. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5628. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5629. summs += dmin * _mm_extract_epi32(hsum, 0);
  5630. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5631. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5632. __m128i hmask = mone;
  5633. __m128i sumi_0 = _mm_setzero_si128();
  5634. __m128i sumi_1 = _mm_setzero_si128();
  5635. int bit = 0;
  5636. __m128i shuffle = _mm_set1_epi16(0x0100);
  5637. for (int j = 0; j < QK_K/64; ++j) {
  5638. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5639. shuffle = _mm_add_epi16(shuffle, m2);
  5640. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5641. shuffle = _mm_add_epi16(shuffle, m2);
  5642. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5643. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5644. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5645. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5646. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5647. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5648. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5649. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5650. hmask = _mm_slli_epi16(hmask, 1);
  5651. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5652. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5653. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5654. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5655. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5656. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5657. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5658. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5659. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5660. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5661. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5662. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5663. hmask = _mm_slli_epi16(hmask, 1);
  5664. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5665. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5666. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5667. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5668. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5669. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5670. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5671. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5672. }
  5673. __m256 vd = _mm256_set1_ps(d);
  5674. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5675. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5676. }
  5677. *s = hsum_float_8(acc) + summs;
  5678. #elif defined __riscv_v_intrinsic
  5679. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5680. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5681. float sumf = 0;
  5682. float sums = 0.0;
  5683. size_t vl;
  5684. for (int i = 0; i < nb; ++i) {
  5685. vl = 8;
  5686. const uint8_t * restrict q5 = x[i].qs;
  5687. const uint8_t * restrict hm = x[i].qh;
  5688. const int8_t * restrict q8 = y[i].qs;
  5689. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5690. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5691. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5692. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5693. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5694. memcpy(utmp, x[i].scales, 12);
  5695. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5696. const uint32_t uaux = utmp[1] & kmask1;
  5697. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5698. utmp[2] = uaux;
  5699. utmp[0] &= kmask1;
  5700. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5701. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5702. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5703. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5704. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5705. vl = 32;
  5706. int32_t aux32 = 0;
  5707. int is = 0;
  5708. uint8_t m = 1;
  5709. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5710. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5711. for (int j = 0; j < QK_K/64; ++j) {
  5712. // load Q5 and Q8
  5713. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5714. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5715. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5716. // compute mask for addition
  5717. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5718. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5719. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5720. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5721. m <<= 1;
  5722. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5723. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5724. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5725. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5726. m <<= 1;
  5727. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5728. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5729. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5730. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5731. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5732. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5733. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5734. q5 += 32; q8 += 64;
  5735. }
  5736. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5737. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5738. }
  5739. *s = sumf+sums;
  5740. #else
  5741. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5742. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5743. int8_t aux8[QK_K];
  5744. int16_t aux16[8];
  5745. float sums [8];
  5746. int32_t aux32[8];
  5747. memset(sums, 0, 8*sizeof(float));
  5748. float sumf = 0;
  5749. for (int i = 0; i < nb; ++i) {
  5750. const uint8_t * restrict q4 = x[i].qs;
  5751. const uint8_t * restrict hm = x[i].qh;
  5752. const int8_t * restrict q8 = y[i].qs;
  5753. memset(aux32, 0, 8*sizeof(int32_t));
  5754. int8_t * restrict a = aux8;
  5755. uint8_t m = 1;
  5756. for (int j = 0; j < QK_K/64; ++j) {
  5757. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5758. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5759. a += 32; m <<= 1;
  5760. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5761. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5762. a += 32; m <<= 1;
  5763. q4 += 32;
  5764. }
  5765. memcpy(utmp, x[i].scales, 12);
  5766. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5767. const uint32_t uaux = utmp[1] & kmask1;
  5768. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5769. utmp[2] = uaux;
  5770. utmp[0] &= kmask1;
  5771. int sumi = 0;
  5772. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5773. a = aux8;
  5774. int is = 0;
  5775. for (int j = 0; j < QK_K/32; ++j) {
  5776. int32_t scale = scales[is++];
  5777. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5778. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5779. q8 += 8; a += 8;
  5780. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5781. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5782. q8 += 8; a += 8;
  5783. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5784. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5785. q8 += 8; a += 8;
  5786. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5787. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5788. q8 += 8; a += 8;
  5789. }
  5790. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5791. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5792. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5793. sumf -= dmin * sumi;
  5794. }
  5795. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5796. *s = sumf;
  5797. #endif
  5798. }
  5799. #else
  5800. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5801. assert(n % QK_K == 0);
  5802. assert(nrc == 1);
  5803. UNUSED(nrc);
  5804. UNUSED(bx);
  5805. UNUSED(by);
  5806. UNUSED(bs);
  5807. const block_q5_K * restrict x = vx;
  5808. const block_q8_K * restrict y = vy;
  5809. const int nb = n / QK_K;
  5810. #ifdef __ARM_NEON
  5811. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5812. const uint8x16_t mh = vdupq_n_u8(16);
  5813. const int32x4_t mzero = vdupq_n_s32(0);
  5814. ggml_int8x16x4_t q5bytes;
  5815. ggml_uint8x16x4_t q5h;
  5816. float sumf = 0;
  5817. for (int i = 0; i < nb; ++i) {
  5818. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5819. const int8_t * sc = x[i].scales;
  5820. const uint8_t * restrict q5 = x[i].qs;
  5821. const uint8_t * restrict qh = x[i].qh;
  5822. const int8_t * restrict q8 = y[i].qs;
  5823. const uint8x8_t qhbits = vld1_u8(qh);
  5824. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5825. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5826. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5827. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5828. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5829. q5h.val[2] = vbicq_u8(mh, htmp);
  5830. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5831. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5832. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5833. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5834. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5835. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5836. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5837. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5838. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5839. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5840. }
  5841. *s = sumf;
  5842. #elif defined __AVX2__
  5843. const __m256i m4 = _mm256_set1_epi8(0xF);
  5844. const __m256i mone = _mm256_set1_epi8(1);
  5845. __m256 acc = _mm256_setzero_ps();
  5846. for (int i = 0; i < nb; ++i) {
  5847. const uint8_t * restrict q5 = x[i].qs;
  5848. const int8_t * restrict q8 = y[i].qs;
  5849. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5850. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5851. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5852. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5853. int64_t aux64;
  5854. memcpy(&aux64, x[i].qh, 8);
  5855. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5856. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5857. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5858. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5859. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5860. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5861. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5862. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5863. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5864. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5865. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5866. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5867. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5868. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5869. }
  5870. *s = hsum_float_8(acc);
  5871. #elif defined __AVX__
  5872. const __m128i m4 = _mm_set1_epi8(0xF);
  5873. const __m128i mone = _mm_set1_epi8(1);
  5874. __m256 acc = _mm256_setzero_ps();
  5875. for (int i = 0; i < nb; ++i) {
  5876. const uint8_t * restrict q5 = x[i].qs;
  5877. const int8_t * restrict q8 = y[i].qs;
  5878. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5879. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5880. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5881. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5882. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5883. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5884. int64_t aux64;
  5885. memcpy(&aux64, x[i].qh, 8);
  5886. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5887. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5888. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5889. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5890. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5891. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5892. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5893. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5894. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5895. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5896. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5897. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5898. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5899. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5900. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5901. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5902. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5903. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5904. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5905. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5906. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5907. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5908. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5909. }
  5910. *s = hsum_float_8(acc);
  5911. #elif defined __riscv_v_intrinsic
  5912. float sumf = 0;
  5913. for (int i = 0; i < nb; ++i) {
  5914. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5915. const int8_t * sc = x[i].scales;
  5916. const uint8_t * restrict q5 = x[i].qs;
  5917. const uint8_t * restrict qh = x[i].qh;
  5918. const int8_t * restrict q8 = y[i].qs;
  5919. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5920. // load qh
  5921. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5922. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5923. size_t vl = 16;
  5924. // combine both qh_1 and qh_2
  5925. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5926. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5927. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5928. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5929. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5930. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5931. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5932. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5933. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5934. // load q5
  5935. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5936. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5937. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5938. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5939. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5940. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5941. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5942. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5943. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5944. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5945. // load Q8 and multiply it with Q5
  5946. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5947. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5948. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5949. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5950. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5951. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5952. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5953. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5954. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5955. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5956. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5957. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5958. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5959. }
  5960. *s = sumf;
  5961. #else
  5962. int8_t aux8[QK_K];
  5963. int16_t aux16[16];
  5964. float sums [8];
  5965. memset(sums, 0, 8*sizeof(float));
  5966. float sumf = 0;
  5967. for (int i = 0; i < nb; ++i) {
  5968. const uint8_t * restrict q4 = x[i].qs;
  5969. const uint8_t * restrict hm = x[i].qh;
  5970. const int8_t * restrict q8 = y[i].qs;
  5971. int8_t * restrict a = aux8;
  5972. for (int l = 0; l < 32; ++l) {
  5973. a[l+ 0] = q4[l] & 0xF;
  5974. a[l+32] = q4[l] >> 4;
  5975. }
  5976. for (int is = 0; is < 8; ++is) {
  5977. uint8_t m = 1 << is;
  5978. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5979. }
  5980. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5981. const int8_t * restrict sc = x[i].scales;
  5982. for (int j = 0; j < QK_K/16; ++j) {
  5983. const float dl = d * sc[j];
  5984. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5985. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5986. q8 += 16; a += 16;
  5987. }
  5988. }
  5989. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5990. *s = sumf;
  5991. #endif
  5992. }
  5993. #endif
  5994. #if QK_K == 256
  5995. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5996. assert(n % QK_K == 0);
  5997. assert(nrc == 1);
  5998. UNUSED(nrc);
  5999. UNUSED(bx);
  6000. UNUSED(by);
  6001. UNUSED(bs);
  6002. const block_q6_K * restrict x = vx;
  6003. const block_q8_K * restrict y = vy;
  6004. const int nb = n / QK_K;
  6005. #ifdef __ARM_NEON
  6006. float sum = 0;
  6007. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6008. const int32x4_t vzero = vdupq_n_s32(0);
  6009. //const int8x16_t m32s = vdupq_n_s8(32);
  6010. const uint8x16_t mone = vdupq_n_u8(3);
  6011. ggml_int8x16x4_t q6bytes;
  6012. ggml_uint8x16x4_t q6h;
  6013. for (int i = 0; i < nb; ++i) {
  6014. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6015. const uint8_t * restrict q6 = x[i].ql;
  6016. const uint8_t * restrict qh = x[i].qh;
  6017. const int8_t * restrict q8 = y[i].qs;
  6018. const int8_t * restrict scale = x[i].scales;
  6019. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6020. const int8x16_t scales = vld1q_s8(scale);
  6021. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6022. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6023. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6024. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6025. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6026. int32_t isum_mins = vaddvq_s32(prod);
  6027. int32_t isum = 0;
  6028. for (int j = 0; j < QK_K/128; ++j) {
  6029. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6030. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6031. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6032. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6033. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6034. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6035. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6036. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6037. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6038. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6039. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6040. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6041. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6042. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6043. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6044. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6045. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6046. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6047. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6048. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6049. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6050. scale += 4;
  6051. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6052. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6053. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6054. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6055. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6056. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6057. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6058. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6059. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6060. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6061. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6062. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6063. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6064. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6065. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6066. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6067. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6068. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6069. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6070. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6071. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6072. scale += 4;
  6073. }
  6074. //sum += isum * d_all * y[i].d;
  6075. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6076. }
  6077. *s = sum;
  6078. #elif defined __AVX2__
  6079. const __m256i m4 = _mm256_set1_epi8(0xF);
  6080. const __m256i m2 = _mm256_set1_epi8(3);
  6081. const __m256i m32s = _mm256_set1_epi8(32);
  6082. __m256 acc = _mm256_setzero_ps();
  6083. for (int i = 0; i < nb; ++i) {
  6084. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6085. const uint8_t * restrict q4 = x[i].ql;
  6086. const uint8_t * restrict qh = x[i].qh;
  6087. const int8_t * restrict q8 = y[i].qs;
  6088. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6089. __m256i sumi = _mm256_setzero_si256();
  6090. int is = 0;
  6091. for (int j = 0; j < QK_K/128; ++j) {
  6092. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6093. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6094. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6095. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6096. is += 4;
  6097. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6098. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6099. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6100. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6101. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6102. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6103. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6104. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6105. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6106. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6107. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6108. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6109. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6110. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6111. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6112. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6113. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6114. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6115. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6116. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6117. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6118. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6119. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6120. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6121. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6122. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6123. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6124. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6125. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6126. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6127. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6128. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6129. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6130. }
  6131. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6132. }
  6133. *s = hsum_float_8(acc);
  6134. #elif defined __AVX__
  6135. const __m128i m4 = _mm_set1_epi8(0xF);
  6136. const __m128i m3 = _mm_set1_epi8(3);
  6137. const __m128i m32s = _mm_set1_epi8(32);
  6138. const __m128i m2 = _mm_set1_epi8(2);
  6139. __m256 acc = _mm256_setzero_ps();
  6140. for (int i = 0; i < nb; ++i) {
  6141. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6142. const uint8_t * restrict q4 = x[i].ql;
  6143. const uint8_t * restrict qh = x[i].qh;
  6144. const int8_t * restrict q8 = y[i].qs;
  6145. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6146. __m128i sumi_0 = _mm_setzero_si128();
  6147. __m128i sumi_1 = _mm_setzero_si128();
  6148. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6149. for (int j = 0; j < QK_K/128; ++j) {
  6150. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6151. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6152. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6153. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6154. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6155. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6156. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6157. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6158. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6159. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6160. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6161. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6162. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6163. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6164. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6165. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6166. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6167. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6168. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6169. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6170. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6171. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6172. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6173. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6174. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6175. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6176. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6177. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6178. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6179. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6180. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6181. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6182. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6183. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6184. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6185. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6186. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6187. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6188. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6189. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6190. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6191. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6192. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6193. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6194. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6195. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6196. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6197. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6198. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6199. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6200. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6201. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6202. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6203. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6204. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6205. shuffle = _mm_add_epi8(shuffle, m2);
  6206. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6207. shuffle = _mm_add_epi8(shuffle, m2);
  6208. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6209. shuffle = _mm_add_epi8(shuffle, m2);
  6210. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6211. shuffle = _mm_add_epi8(shuffle, m2);
  6212. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6213. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6214. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6215. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6216. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6217. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6218. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6219. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6220. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6221. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6222. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6223. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6224. }
  6225. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6226. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6227. }
  6228. *s = hsum_float_8(acc);
  6229. #elif defined __riscv_v_intrinsic
  6230. float sumf = 0;
  6231. for (int i = 0; i < nb; ++i) {
  6232. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6233. const uint8_t * restrict q6 = x[i].ql;
  6234. const uint8_t * restrict qh = x[i].qh;
  6235. const int8_t * restrict q8 = y[i].qs;
  6236. const int8_t * restrict scale = x[i].scales;
  6237. size_t vl;
  6238. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6239. int sum_t = 0;
  6240. int is = 0;
  6241. for (int j = 0; j < QK_K/128; ++j) {
  6242. vl = 32;
  6243. // load qh
  6244. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6245. // load Q6
  6246. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6247. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6248. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6249. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6250. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6251. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6252. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6253. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6254. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6255. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6256. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6257. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6258. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6259. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6260. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6261. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6262. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6263. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6264. // load Q8 and take product
  6265. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6266. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6267. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6268. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6269. vl = 16;
  6270. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6271. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6272. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6273. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6274. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6275. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6276. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6277. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6278. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6279. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6280. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6281. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6282. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6283. q6 += 64; qh += 32; q8 += 128; is=8;
  6284. }
  6285. sumf += d * sum_t;
  6286. }
  6287. *s = sumf;
  6288. #else
  6289. int8_t aux8[QK_K];
  6290. int16_t aux16[8];
  6291. float sums [8];
  6292. int32_t aux32[8];
  6293. memset(sums, 0, 8*sizeof(float));
  6294. float sumf = 0;
  6295. for (int i = 0; i < nb; ++i) {
  6296. const uint8_t * restrict q4 = x[i].ql;
  6297. const uint8_t * restrict qh = x[i].qh;
  6298. const int8_t * restrict q8 = y[i].qs;
  6299. memset(aux32, 0, 8*sizeof(int32_t));
  6300. int8_t * restrict a = aux8;
  6301. for (int j = 0; j < QK_K; j += 128) {
  6302. for (int l = 0; l < 32; ++l) {
  6303. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6304. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6305. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6306. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6307. }
  6308. a += 128;
  6309. q4 += 64;
  6310. qh += 32;
  6311. }
  6312. a = aux8;
  6313. int is = 0;
  6314. for (int j = 0; j < QK_K/16; ++j) {
  6315. int scale = x[i].scales[is++];
  6316. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6317. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6318. q8 += 8; a += 8;
  6319. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6320. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6321. q8 += 8; a += 8;
  6322. }
  6323. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6324. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6325. }
  6326. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6327. *s = sumf;
  6328. #endif
  6329. }
  6330. #else
  6331. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6332. assert(n % QK_K == 0);
  6333. assert(nrc == 1);
  6334. UNUSED(nrc);
  6335. UNUSED(bx);
  6336. UNUSED(by);
  6337. UNUSED(bs);
  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 = GGML_FP16_TO_FP32(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 = GGML_FP16_TO_FP32(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. #if defined (__AVX2__) || defined (__ARM_NEON)
  6550. static const int8_t keven_signs_q2xs[1024] = {
  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. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6583. };
  6584. #endif
  6585. void ggml_vec_dot_iq2_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6586. assert(n % QK_K == 0);
  6587. assert(nrc == 1);
  6588. UNUSED(nrc);
  6589. UNUSED(bx);
  6590. UNUSED(by);
  6591. UNUSED(bs);
  6592. const block_iq2_xxs * restrict x = vx;
  6593. const block_q8_K * restrict y = vy;
  6594. const int nb = n / QK_K;
  6595. #if defined(__ARM_NEON)
  6596. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6597. uint32_t aux32[4];
  6598. const uint8_t * aux8 = (const uint8_t *)aux32;
  6599. ggml_int8x16x4_t q2u;
  6600. ggml_int8x16x4_t q2s;
  6601. ggml_int8x16x4_t q8b;
  6602. float sumf = 0;
  6603. for (int i = 0; i < nb; ++i) {
  6604. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6605. const uint16_t * restrict q2 = x[i].qs;
  6606. const int8_t * restrict q8 = y[i].qs;
  6607. float sumf1 = 0, sumf2 = 0;
  6608. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6609. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6610. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6611. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6612. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6613. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6614. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6615. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6616. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6617. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6618. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6619. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6620. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6621. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6622. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6623. 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]);
  6624. 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]);
  6625. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6626. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6627. }
  6628. sumf += d*(sumf1 + sumf2);
  6629. }
  6630. *s = 0.25f * sumf;
  6631. #elif defined(__AVX2__)
  6632. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6633. uint32_t aux32[4];
  6634. const uint8_t * aux8 = (const uint8_t *)aux32;
  6635. __m256 accumf = _mm256_setzero_ps();
  6636. for (int i = 0; i < nb; ++i) {
  6637. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6638. const uint16_t * restrict q2 = x[i].qs;
  6639. const int8_t * restrict q8 = y[i].qs;
  6640. __m256i sumi1 = _mm256_setzero_si256();
  6641. __m256i sumi2 = _mm256_setzero_si256();
  6642. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6643. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6644. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6645. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6646. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6647. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6648. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6649. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6650. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6651. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6652. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6653. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6654. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6655. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6656. const uint16_t ls1 = aux32[1] >> 28;
  6657. const uint16_t ls2 = aux32[3] >> 28;
  6658. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6659. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6660. sumi1 = _mm256_add_epi32(sumi1, p1);
  6661. sumi2 = _mm256_add_epi32(sumi2, p2);
  6662. }
  6663. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6664. }
  6665. *s = 0.125f * hsum_float_8(accumf);
  6666. #else
  6667. uint32_t aux32[2];
  6668. const uint8_t * aux8 = (const uint8_t *)aux32;
  6669. float sumf = 0.f;
  6670. for (int i = 0; i < nb; ++i) {
  6671. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6672. const uint16_t * restrict q2 = x[i].qs;
  6673. const int8_t * restrict q8 = y[i].qs;
  6674. int32_t bsum = 0;
  6675. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6676. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6677. q2 += 4;
  6678. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6679. int32_t sumi = 0;
  6680. for (int l = 0; l < 4; ++l) {
  6681. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6682. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6683. for (int j = 0; j < 8; ++j) {
  6684. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6685. }
  6686. q8 += 8;
  6687. }
  6688. bsum += sumi * ls;
  6689. }
  6690. sumf += d * bsum;
  6691. }
  6692. *s = 0.125f * sumf;
  6693. #endif
  6694. }
  6695. void ggml_vec_dot_iq2_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6696. assert(n % QK_K == 0);
  6697. assert(nrc == 1);
  6698. UNUSED(nrc);
  6699. UNUSED(bx);
  6700. UNUSED(by);
  6701. UNUSED(bs);
  6702. const block_iq2_xs * restrict x = vx;
  6703. const block_q8_K * restrict y = vy;
  6704. const int nb = n / QK_K;
  6705. #if defined(__ARM_NEON)
  6706. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6707. ggml_int8x16x4_t q2u;
  6708. ggml_int8x16x4_t q2s;
  6709. ggml_int8x16x4_t q8b;
  6710. int32x4x4_t scales32;
  6711. float sumf = 0;
  6712. for (int i = 0; i < nb; ++i) {
  6713. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6714. const uint16_t * restrict q2 = x[i].qs;
  6715. const int8_t * restrict q8 = y[i].qs;
  6716. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6717. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6718. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6719. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6720. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6721. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6722. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6723. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6724. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6725. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6726. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6727. int32x4_t sumi = vdupq_n_s32(0);
  6728. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6729. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6730. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6731. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6732. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6733. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6734. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6735. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6736. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6737. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6738. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6739. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6740. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6741. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6742. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6743. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6744. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6745. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6746. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6747. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6748. q2 += 8;
  6749. }
  6750. sumf += d*vaddvq_s32(sumi);
  6751. }
  6752. *s = 0.125f * sumf;
  6753. #elif defined(__AVX2__)
  6754. const __m256i mone = _mm256_set1_epi8(1);
  6755. static const char block_sign_shuffle_mask_1[32] = {
  6756. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6757. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6758. };
  6759. static const char block_sign_shuffle_mask_2[32] = {
  6760. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6761. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6762. };
  6763. static const uint8_t bit_selector_mask_bytes[32] = {
  6764. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6765. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6766. };
  6767. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6768. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6769. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6770. #if QK_K == 64
  6771. static const uint8_t k_bit_helper[16] = {
  6772. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6773. };
  6774. const __m128i bit_helper = _mm_loadu_si128((const __m128i*)k_bit_helper);
  6775. const __m128i m511 = _mm_set1_epi16(511);
  6776. typedef union {
  6777. __m128i vec_index;
  6778. uint16_t index[8];
  6779. } index_t;
  6780. index_t idx;
  6781. __m256 accumf = _mm256_setzero_ps();
  6782. for (int i = 0; i < nb; ++i) {
  6783. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6784. const __m128i q2_data = _mm_loadu_si128((const __m128i*)x[i].qs);
  6785. idx.vec_index = _mm_and_si128(q2_data, m511);
  6786. const __m128i partial_sign_bits = _mm_srli_epi16(q2_data, 9);
  6787. const __m128i partial_sign_bits_upper = _mm_srli_epi16(q2_data, 13);
  6788. const __m128i partial_sign_bits_for_counting = _mm_xor_si128(partial_sign_bits, partial_sign_bits_upper);
  6789. const __m128i odd_bits = _mm_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6790. const __m128i full_sign_bits = _mm_or_si128(partial_sign_bits, odd_bits);
  6791. const __m256i full_signs = MM256_SET_M128I(full_sign_bits, full_sign_bits);
  6792. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  6793. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)(y[i].qs+32));
  6794. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[idx.index[3]], iq2xs_grid[idx.index[2]],
  6795. iq2xs_grid[idx.index[1]], iq2xs_grid[idx.index[0]]);
  6796. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[idx.index[7]], iq2xs_grid[idx.index[6]],
  6797. iq2xs_grid[idx.index[5]], iq2xs_grid[idx.index[4]]);
  6798. __m256i signs;
  6799. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_1);
  6800. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6801. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6802. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_2);
  6803. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6804. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6805. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6806. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6807. const __m256i sc1 = MM256_SET_M128I(_mm_set1_epi16(2*(x[i].scales[0] >> 4)+1), _mm_set1_epi16(2*(x[i].scales[0] & 0xf)+1));
  6808. const __m256i sc2 = MM256_SET_M128I(_mm_set1_epi16(2*(x[i].scales[1] >> 4)+1), _mm_set1_epi16(2*(x[i].scales[1] & 0xf)+1));
  6809. const __m256i sum = _mm256_add_epi32(_mm256_madd_epi16(sc1, dot1), _mm256_madd_epi16(sc2, dot2));
  6810. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sum), accumf);
  6811. }
  6812. *s = 0.125f * hsum_float_8(accumf);
  6813. #else
  6814. static const uint8_t k_bit_helper[32] = {
  6815. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6816. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6817. };
  6818. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  6819. const __m256i m511 = _mm256_set1_epi16(511);
  6820. const __m128i m4 = _mm_set1_epi8(0xf);
  6821. const __m128i m1 = _mm_set1_epi8(1);
  6822. uint64_t aux64;
  6823. // somewhat hacky, but gives a significant boost in performance
  6824. __m256i aux_gindex;
  6825. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6826. __m256 accumf = _mm256_setzero_ps();
  6827. for (int i = 0; i < nb; ++i) {
  6828. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6829. const uint16_t * restrict q2 = x[i].qs;
  6830. const int8_t * restrict q8 = y[i].qs;
  6831. memcpy(&aux64, x[i].scales, 8);
  6832. __m128i stmp = _mm_set1_epi64x(aux64);
  6833. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6834. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6835. __m256i sumi1 = _mm256_setzero_si256();
  6836. __m256i sumi2 = _mm256_setzero_si256();
  6837. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  6838. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  6839. aux_gindex = _mm256_and_si256(q2_data, m511);
  6840. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  6841. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  6842. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  6843. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6844. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  6845. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6846. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6847. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6848. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6849. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  6850. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  6851. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  6852. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  6853. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  6854. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  6855. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  6856. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  6857. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  6858. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  6859. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  6860. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  6861. __m256i signs;
  6862. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  6863. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6864. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6865. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  6866. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6867. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6868. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  6869. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6870. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  6871. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  6872. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6873. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  6874. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6875. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6876. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  6877. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  6878. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6879. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6880. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  6881. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  6882. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6883. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6884. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  6885. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  6886. }
  6887. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6888. }
  6889. *s = 0.125f * hsum_float_8(accumf);
  6890. #endif
  6891. #else
  6892. float sumf = 0.f;
  6893. for (int i = 0; i < nb; ++i) {
  6894. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6895. const uint16_t * restrict q2 = x[i].qs;
  6896. const uint8_t * restrict sc = x[i].scales;
  6897. const int8_t * restrict q8 = y[i].qs;
  6898. int32_t bsum = 0;
  6899. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6900. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6901. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6902. int32_t sumi = 0;
  6903. for (int l = 0; l < 2; ++l) {
  6904. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6905. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6906. for (int j = 0; j < 8; ++j) {
  6907. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6908. }
  6909. q8 += 8;
  6910. }
  6911. bsum += sumi * ls1;
  6912. sumi = 0;
  6913. for (int l = 2; l < 4; ++l) {
  6914. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6915. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6916. for (int j = 0; j < 8; ++j) {
  6917. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6918. }
  6919. q8 += 8;
  6920. }
  6921. bsum += sumi * ls2;
  6922. q2 += 4;
  6923. }
  6924. sumf += d * bsum;
  6925. }
  6926. *s = 0.125f * sumf;
  6927. #endif
  6928. }
  6929. void ggml_vec_dot_iq2_s_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6930. assert(n % QK_K == 0);
  6931. assert(nrc == 1);
  6932. UNUSED(nrc);
  6933. UNUSED(bx);
  6934. UNUSED(by);
  6935. UNUSED(bs);
  6936. const block_iq2_s * restrict x = vx;
  6937. const block_q8_K * restrict y = vy;
  6938. const int nb = n / QK_K;
  6939. #if defined(__ARM_NEON)
  6940. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  6941. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  6942. };
  6943. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  6944. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  6945. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  6946. const uint8x16_t m1 = vdupq_n_u8(1);
  6947. const int32x4_t vzero = vdupq_n_s32(0);
  6948. uint8x16x2_t vs;
  6949. ggml_int8x16x4_t q2s;
  6950. ggml_int8x16x4_t q8b;
  6951. float sumf = 0;
  6952. for (int i = 0; i < nb; ++i) {
  6953. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6954. const uint8_t * restrict qs = x[i].qs;
  6955. const uint8_t * restrict qh = x[i].qh;
  6956. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  6957. const int8_t * restrict q8 = y[i].qs;
  6958. int sumi1 = 0, sumi2 = 0;
  6959. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6960. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6961. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  6962. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  6963. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  6964. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  6965. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  6966. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  6967. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  6968. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  6969. qs += 8;
  6970. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  6971. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  6972. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  6973. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  6974. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  6975. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  6976. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  6977. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  6978. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  6979. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  6980. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  6981. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  6982. signs += 4;
  6983. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  6984. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  6985. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  6986. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  6987. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  6988. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  6989. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  6990. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  6991. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  6992. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  6993. }
  6994. sumf += d*(sumi1 + sumi2);
  6995. }
  6996. *s = 0.125f * sumf;
  6997. #elif defined(__AVX2__)
  6998. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  6999. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7000. };
  7001. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7002. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7003. };
  7004. const __m128i m4 = _mm_set1_epi8(0xf);
  7005. const __m128i m1 = _mm_set1_epi8(1);
  7006. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7007. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7008. uint64_t aux64;
  7009. __m256 accumf = _mm256_setzero_ps();
  7010. for (int i = 0; i < nb; ++i) {
  7011. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7012. const uint8_t * restrict qs = x[i].qs;
  7013. const uint8_t * restrict qh = x[i].qh;
  7014. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7015. const int8_t * restrict q8 = y[i].qs;
  7016. memcpy(&aux64, x[i].scales, 8);
  7017. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7018. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7019. __m256i sumi1 = _mm256_setzero_si256();
  7020. __m256i sumi2 = _mm256_setzero_si256();
  7021. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7022. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7023. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7024. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7025. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7026. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7027. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7028. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7029. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7030. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7031. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7032. qs += 8;
  7033. __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
  7034. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7035. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7036. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7037. aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
  7038. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7039. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7040. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7041. signs += 4;
  7042. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7043. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7044. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7045. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7046. sumi1 = _mm256_add_epi32(sumi1, p1);
  7047. sumi2 = _mm256_add_epi32(sumi2, p2);
  7048. }
  7049. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7050. }
  7051. *s = 0.125f * hsum_float_8(accumf);
  7052. #else
  7053. float sumf = 0;
  7054. for (int i = 0; i < nb; i++) {
  7055. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7056. const int8_t * q8 = y[i].qs;
  7057. const uint8_t * qs = x[i].qs;
  7058. const uint8_t * qh = x[i].qh;
  7059. const uint8_t * signs = qs + QK_K/8;
  7060. int bsum = 0;
  7061. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7062. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7063. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7064. int sumi1 = 0, sumi2 = 0;
  7065. for (int l = 0; l < 2; ++l) {
  7066. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7067. for (int j = 0; j < 8; ++j) {
  7068. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7069. }
  7070. q8 += 8;
  7071. }
  7072. for (int l = 2; l < 4; ++l) {
  7073. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7074. for (int j = 0; j < 8; ++j) {
  7075. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7076. }
  7077. q8 += 8;
  7078. }
  7079. bsum += ls1 * sumi1 + ls2 * sumi2;
  7080. qs += 4;
  7081. signs += 4;
  7082. }
  7083. sumf += d * bsum;
  7084. }
  7085. *s = 0.125f * sumf;
  7086. #endif
  7087. }
  7088. void ggml_vec_dot_iq3_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7089. assert(n % QK_K == 0);
  7090. assert(nrc == 1);
  7091. UNUSED(nrc);
  7092. UNUSED(bx);
  7093. UNUSED(by);
  7094. UNUSED(bs);
  7095. const block_iq3_xxs * restrict x = vx;
  7096. const block_q8_K * restrict y = vy;
  7097. const int nb = n / QK_K;
  7098. #if defined(__ARM_NEON)
  7099. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7100. uint32_t aux32[2];
  7101. ggml_int8x16x4_t q3s;
  7102. ggml_int8x16x4_t q8b;
  7103. float sumf = 0;
  7104. for (int i = 0; i < nb; ++i) {
  7105. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7106. const uint8_t * restrict q3 = x[i].qs;
  7107. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7108. const int8_t * restrict q8 = y[i].qs;
  7109. float sumf1 = 0, sumf2 = 0;
  7110. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7111. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7112. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7113. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7114. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7115. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7116. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7117. q3 += 16;
  7118. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7119. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7120. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7121. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7122. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7123. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7124. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7125. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7126. 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]);
  7127. 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]);
  7128. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7129. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7130. }
  7131. sumf += d*(sumf1 + sumf2);
  7132. }
  7133. *s = 0.5f * sumf;
  7134. #elif defined(__AVX2__)
  7135. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7136. uint32_t aux32[2];
  7137. __m256 accumf = _mm256_setzero_ps();
  7138. for (int i = 0; i < nb; ++i) {
  7139. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7140. const uint8_t * restrict q3 = x[i].qs;
  7141. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7142. const int8_t * restrict q8 = y[i].qs;
  7143. __m256i sumi1 = _mm256_setzero_si256();
  7144. __m256i sumi2 = _mm256_setzero_si256();
  7145. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7146. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7147. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7148. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7149. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7150. q3 += 8;
  7151. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7152. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7153. q3 += 8;
  7154. memcpy(aux32, gas, 8); gas += 8;
  7155. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7156. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7157. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7158. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7159. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7160. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7161. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7162. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7163. const uint16_t ls1 = aux32[0] >> 28;
  7164. const uint16_t ls2 = aux32[1] >> 28;
  7165. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7166. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7167. sumi1 = _mm256_add_epi32(sumi1, p1);
  7168. sumi2 = _mm256_add_epi32(sumi2, p2);
  7169. }
  7170. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7171. }
  7172. *s = 0.25f * hsum_float_8(accumf);
  7173. #else
  7174. uint32_t aux32;
  7175. float sumf = 0.f;
  7176. for (int i = 0; i < nb; ++i) {
  7177. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7178. const uint8_t * restrict q3 = x[i].qs;
  7179. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7180. const int8_t * restrict q8 = y[i].qs;
  7181. int32_t bsum = 0;
  7182. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7183. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  7184. const uint32_t ls = 2*(aux32 >> 28) + 1;
  7185. int32_t sumi = 0;
  7186. for (int l = 0; l < 4; ++l) {
  7187. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  7188. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  7189. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  7190. for (int j = 0; j < 4; ++j) {
  7191. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  7192. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  7193. }
  7194. q8 += 8;
  7195. }
  7196. q3 += 8;
  7197. bsum += sumi * ls;
  7198. }
  7199. sumf += d * bsum;
  7200. }
  7201. *s = 0.25f * sumf;
  7202. #endif
  7203. }
  7204. void ggml_vec_dot_iq3_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7205. assert(n % QK_K == 0);
  7206. assert(nrc == 1);
  7207. UNUSED(nrc);
  7208. UNUSED(bx);
  7209. UNUSED(by);
  7210. UNUSED(bs);
  7211. const block_iq3_s * restrict x = vx;
  7212. const block_q8_K * restrict y = vy;
  7213. const int nb = n / QK_K;
  7214. #if defined(__ARM_NEON)
  7215. typedef union {
  7216. uint16x8_t vec_index;
  7217. uint16_t index[8];
  7218. } vec_index_t;
  7219. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7220. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7221. };
  7222. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7223. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  7224. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7225. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7226. const int16x8_t hshift = vld1q_s16(k_shift);
  7227. const uint16x8_t m256 = vdupq_n_u16(256);
  7228. const uint8x16_t m1 = vdupq_n_u8(1);
  7229. uint8x16x2_t vs;
  7230. ggml_int8x16x4_t q3s;
  7231. ggml_int8x16x4_t q8b;
  7232. vec_index_t idx;
  7233. #if QK_K == 256
  7234. uint32_t scales32[2];
  7235. const uint8_t * scales8 = (const uint8_t *)scales32;
  7236. #endif
  7237. float sumf = 0;
  7238. for (int i = 0; i < nb; ++i) {
  7239. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7240. const uint8_t * restrict qs = x[i].qs;
  7241. const uint8_t * restrict qh = x[i].qh;
  7242. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7243. const int8_t * restrict q8 = y[i].qs;
  7244. #if QK_K == 256
  7245. memcpy(scales32, x[i].scales, 4);
  7246. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  7247. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  7248. #endif
  7249. int sumi1 = 0, sumi2 = 0;
  7250. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7251. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7252. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  7253. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  7254. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7255. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7256. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7257. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7258. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  7259. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7260. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7261. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7262. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7263. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7264. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7265. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7266. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7267. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7268. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  7269. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  7270. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7271. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7272. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7273. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7274. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7275. signs += 4;
  7276. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  7277. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  7278. 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]);
  7279. 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]);
  7280. #if QK_K == 256
  7281. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  7282. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  7283. #else
  7284. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  7285. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32/2] >> 4));
  7286. #endif
  7287. }
  7288. sumf += d*(sumi1 + sumi2);
  7289. }
  7290. *s = sumf;
  7291. #elif defined(__AVX2__)
  7292. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7293. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7294. };
  7295. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7296. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7297. };
  7298. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7299. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7300. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  7301. const __m256i idx_mask = _mm256_set1_epi32(256);
  7302. typedef union {
  7303. __m256i vec[2];
  7304. uint32_t index[16];
  7305. } index_t;
  7306. index_t idx;
  7307. __m256 accumf = _mm256_setzero_ps();
  7308. for (int i = 0; i < nb; ++i) {
  7309. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7310. const uint8_t * restrict qs = x[i].qs;
  7311. const uint8_t * restrict qh = x[i].qh;
  7312. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7313. const int8_t * restrict q8 = y[i].qs;
  7314. __m256i sumi1 = _mm256_setzero_si256();
  7315. __m256i sumi2 = _mm256_setzero_si256();
  7316. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7317. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7318. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7319. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  7320. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  7321. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  7322. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  7323. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  7324. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  7325. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  7326. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  7327. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  7328. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  7329. const __m256i q2_1 = _mm256_set_epi32(
  7330. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  7331. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  7332. );
  7333. const __m256i q2_2 = _mm256_set_epi32(
  7334. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  7335. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  7336. );
  7337. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7338. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7339. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7340. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7341. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7342. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7343. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7344. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7345. signs += 4;
  7346. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7347. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7348. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  7349. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  7350. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7351. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7352. sumi1 = _mm256_add_epi32(sumi1, p1);
  7353. sumi2 = _mm256_add_epi32(sumi2, p2);
  7354. }
  7355. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7356. }
  7357. *s = hsum_float_8(accumf);
  7358. #else
  7359. float sumf = 0.f;
  7360. for (int i = 0; i < nb; ++i) {
  7361. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7362. const uint8_t * restrict qs = x[i].qs;
  7363. const uint8_t * restrict qh = x[i].qh;
  7364. const uint8_t * restrict signs = x[i].signs;
  7365. const int8_t * restrict q8 = y[i].qs;
  7366. int32_t bsum = 0;
  7367. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7368. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  7369. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  7370. int32_t sumi = 0;
  7371. for (int l = 0; l < 4; ++l) {
  7372. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  7373. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  7374. for (int j = 0; j < 4; ++j) {
  7375. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7376. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7377. }
  7378. q8 += 8;
  7379. }
  7380. qs += 8;
  7381. signs += 4;
  7382. bsum += sumi * ls1;
  7383. sumi = 0;
  7384. for (int l = 0; l < 4; ++l) {
  7385. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  7386. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  7387. for (int j = 0; j < 4; ++j) {
  7388. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7389. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7390. }
  7391. q8 += 8;
  7392. }
  7393. qs += 8;
  7394. signs += 4;
  7395. bsum += sumi * ls2;
  7396. }
  7397. sumf += d * bsum;
  7398. }
  7399. *s = sumf;
  7400. #endif
  7401. }
  7402. #ifdef __AVX2__
  7403. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  7404. const __m256i ax = _mm256_sign_epi8(x, x);
  7405. const __m256i sy = _mm256_sign_epi8(y, x);
  7406. return _mm256_maddubs_epi16(ax, sy);
  7407. }
  7408. #endif
  7409. void ggml_vec_dot_iq1_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7410. assert(n % QK_K == 0);
  7411. assert(nrc == 1);
  7412. UNUSED(nrc);
  7413. UNUSED(bx);
  7414. UNUSED(by);
  7415. UNUSED(bs);
  7416. const block_iq1_s * restrict x = vx;
  7417. const block_q8_K * restrict y = vy;
  7418. const int nb = n / QK_K;
  7419. #if defined __ARM_NEON
  7420. ggml_int8x16x4_t q1b;
  7421. ggml_int8x16x4_t q8b;
  7422. float sumf = 0;
  7423. for (int i = 0; i < nb; ++i) {
  7424. const int8_t * q8 = y[i].qs;
  7425. const uint8_t * qs = x[i].qs;
  7426. const uint16_t * qh = x[i].qh;
  7427. int sumi1 = 0, sumi2 = 0, sumi3 = 0;
  7428. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7429. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  7430. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  7431. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  7432. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  7433. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  7434. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  7435. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  7436. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  7437. qs += 8;
  7438. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7439. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[0], q8b.val[0]), q1b.val[1], q8b.val[1]);
  7440. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[2], q8b.val[2]), q1b.val[3], q8b.val[3]);
  7441. const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7442. const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7443. sumi1 += vaddvq_s32(p1) * ls1;
  7444. sumi2 += vaddvq_s32(p2) * ls2;
  7445. sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
  7446. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
  7447. }
  7448. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
  7449. }
  7450. *s = sumf;
  7451. #elif defined __AVX2__
  7452. __m256 accum = _mm256_setzero_ps();
  7453. float accum1 = 0;
  7454. for (int i = 0; i < nb; ++i) {
  7455. const int8_t * q8 = y[i].qs;
  7456. const uint8_t * qs = x[i].qs;
  7457. const uint16_t * qh = x[i].qh;
  7458. __m256i sumi = _mm256_setzero_si256();
  7459. int sumi1 = 0;
  7460. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7461. const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)],
  7462. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  7463. const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)],
  7464. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  7465. qs += 8;
  7466. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7467. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7468. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7469. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7470. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7471. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7472. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
  7473. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
  7474. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  7475. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  7476. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  7477. }
  7478. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  7479. accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
  7480. accum1 += d * sumi1;
  7481. }
  7482. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  7483. #else
  7484. float sumf = 0;
  7485. for (int i = 0; i < nb; i++) {
  7486. const int8_t * q8 = y[i].qs;
  7487. const uint8_t * qs = x[i].qs;
  7488. const uint16_t * qh = x[i].qh;
  7489. int sumi = 0, sumi1 = 0;
  7490. for (int ib = 0; ib < QK_K/32; ++ib) {
  7491. const int ls = 2*((qh[ib] >> 12) & 7) + 1;
  7492. const int delta = qh[ib] & 0x8000 ? -1 : 1;
  7493. int lsum = 0;
  7494. for (int l = 0; l < 4; ++l) {
  7495. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  7496. for (int j = 0; j < 8; ++j) {
  7497. lsum += q8[j] * grid[j];
  7498. }
  7499. q8 += 8;
  7500. }
  7501. sumi += ls * lsum;
  7502. sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
  7503. qs += 4;
  7504. }
  7505. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
  7506. }
  7507. *s = sumf;
  7508. #endif
  7509. }
  7510. void ggml_vec_dot_iq1_m_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7511. assert(n % QK_K == 0);
  7512. assert(nrc == 1);
  7513. UNUSED(nrc);
  7514. UNUSED(bx);
  7515. UNUSED(by);
  7516. UNUSED(bs);
  7517. const block_iq1_m * restrict x = vx;
  7518. const block_q8_K * restrict y = vy;
  7519. const int nb = n / QK_K;
  7520. #if QK_K != 64
  7521. iq1m_scale_t scale;
  7522. #endif
  7523. #if defined __ARM_NEON
  7524. #if QK_K == 64
  7525. const int32x4_t mask = vdupq_n_s32(0xf);
  7526. #else
  7527. const int32x4_t mask = vdupq_n_s32(0x7);
  7528. #endif
  7529. const int32x4_t mone = vdupq_n_s32(1);
  7530. const int32x4_t mzero = vdupq_n_s32(0);
  7531. ggml_int8x16x4_t deltas;
  7532. deltas.val[0] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(+1));
  7533. deltas.val[1] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(+1));
  7534. deltas.val[2] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(-1));
  7535. deltas.val[3] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(-1));
  7536. ggml_int8x16x4_t q1b;
  7537. ggml_int8x16x4_t q8b;
  7538. uint32_t aux32;
  7539. const uint8_t * aux8 = (const uint8_t *)&aux32;
  7540. float sumf = 0;
  7541. for (int i = 0; i < nb; ++i) {
  7542. const int8_t * q8 = y[i].qs;
  7543. const uint8_t * qs = x[i].qs;
  7544. const uint8_t * qh = x[i].qh;
  7545. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7546. #if QK_K != 64
  7547. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7548. #endif
  7549. int32x4_t sumi1 = mzero;
  7550. int32x4_t sumi2 = mzero;
  7551. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7552. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[0] << 8) & 0x700)))),
  7553. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[0] << 4) & 0x700)))));
  7554. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[1] << 8) & 0x700)))),
  7555. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[1] << 4) & 0x700)))));
  7556. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[2] << 8) & 0x700)))),
  7557. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[2] << 4) & 0x700)))));
  7558. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[3] << 8) & 0x700)))),
  7559. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[3] << 4) & 0x700)))));
  7560. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7561. const int32x4_t p1 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[0], q8b.val[0]), ggml_vdotq_s32(mzero, q1b.val[1], q8b.val[1]));
  7562. const int32x4_t p2 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[2], q8b.val[2]), ggml_vdotq_s32(mzero, q1b.val[3], q8b.val[3]));
  7563. const int32x4_t p12 = vpaddq_s32(p1, p2);
  7564. const uint32_t * qh32 = (const uint32_t *)qh; // we are 4-byte aligned, so we can do that
  7565. aux32 = ((qh32[0] >> 3) & 0x01010101) | ((qh32[0] >> 6) & 0x02020202);
  7566. const int32x4_t p3 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[0]], q8b.val[0]), ggml_vdotq_s32(mzero, deltas.val[aux8[1]], q8b.val[1]));
  7567. const int32x4_t p4 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[2]], q8b.val[2]), ggml_vdotq_s32(mzero, deltas.val[aux8[3]], q8b.val[3]));
  7568. const int32x4_t p34 = vpaddq_s32(p3, p4);
  7569. #if QK_K == 64
  7570. int32x4_t scales_4 = ggml_vld1q_u32(sc[0] >> 0, sc[0] >> 4, sc[0] >> 8, sc[0] >> 12);
  7571. #else
  7572. int32x4_t scales_4 = ggml_vld1q_u32(sc[ib/2] >> 0, sc[ib/2] >> 3, sc[ib/2] >> 6, sc[ib/2] >> 9);
  7573. #endif
  7574. scales_4 = vaddq_s32(vshlq_n_s32(vandq_s32(scales_4, mask), 1), mone);
  7575. sumi1 = vmlaq_s32(sumi1, scales_4, p12);
  7576. sumi2 = vmlaq_s32(sumi2, scales_4, p34);
  7577. qs += 8; qh += 4;
  7578. }
  7579. #if QK_K == 64
  7580. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7581. #else
  7582. sumf += y[i].d * GGML_FP16_TO_FP32(scale.f16) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7583. #endif
  7584. }
  7585. *s = sumf;
  7586. #elif defined __AVX2__
  7587. #if QK_K == 64
  7588. const __m256i mask = _mm256_set1_epi16(0xf);
  7589. #else
  7590. const __m256i mask = _mm256_set1_epi16(0x7);
  7591. #endif
  7592. const __m256i mone = _mm256_set1_epi16(1);
  7593. __m256 accum1 = _mm256_setzero_ps();
  7594. __m256 accum2 = _mm256_setzero_ps();
  7595. for (int i = 0; i < nb; ++i) {
  7596. const int8_t * q8 = y[i].qs;
  7597. const uint8_t * qs = x[i].qs;
  7598. const uint8_t * qh = x[i].qh;
  7599. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7600. #if QK_K != 64
  7601. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7602. #endif
  7603. __m256i sumi1 = _mm256_setzero_si256();
  7604. __m256i sumi2 = _mm256_setzero_si256();
  7605. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7606. const __m256i q1b_1 = _mm256_set_epi64x(
  7607. iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)],
  7608. iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]
  7609. );
  7610. const __m256i q1b_2 = _mm256_set_epi64x(
  7611. iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)],
  7612. iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]
  7613. );
  7614. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7615. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7616. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7617. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7618. const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7619. qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7620. qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7621. qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7622. const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7623. qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7624. qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7625. qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7626. const __m256i dot3 = mul_add_epi8(delta1, q8b_1);
  7627. const __m256i dot4 = mul_add_epi8(delta2, q8b_2);
  7628. #if QK_K == 64
  7629. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 4), _mm_set1_epi16(sc[0] >> 0));
  7630. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 12), _mm_set1_epi16(sc[0] >> 8));
  7631. #else
  7632. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 3), _mm_set1_epi16(sc[ib/2] >> 0));
  7633. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 9), _mm_set1_epi16(sc[ib/2] >> 6));
  7634. #endif
  7635. scale1 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale1, mask), 1), mone);
  7636. scale2 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale2, mask), 1), mone);
  7637. const __m256i p1 = _mm256_madd_epi16(dot1, scale1);
  7638. const __m256i p2 = _mm256_madd_epi16(dot2, scale2);
  7639. const __m256i p3 = _mm256_madd_epi16(dot3, scale1);
  7640. const __m256i p4 = _mm256_madd_epi16(dot4, scale2);
  7641. sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2));
  7642. sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4));
  7643. qs += 8; qh += 4;
  7644. }
  7645. #if QK_K == 64
  7646. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(x[i].d));
  7647. #else
  7648. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(scale.f16));
  7649. #endif
  7650. accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1);
  7651. accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2);
  7652. }
  7653. *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
  7654. #else
  7655. int sum1[2], sum2[2], delta[4];
  7656. float sumf = 0;
  7657. for (int i = 0; i < nb; i++) {
  7658. const int8_t * q8 = y[i].qs;
  7659. const uint8_t * qs = x[i].qs;
  7660. const uint8_t * qh = x[i].qh;
  7661. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7662. #if QK_K != 64
  7663. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7664. #endif
  7665. int sumi1 = 0, sumi2 = 0;
  7666. for (int ib = 0; ib < QK_K/32; ++ib) {
  7667. delta[0] = qh[0] & 0x08 ? -1 : 1;
  7668. delta[1] = qh[0] & 0x80 ? -1 : 1;
  7669. delta[2] = qh[1] & 0x08 ? -1 : 1;
  7670. delta[3] = qh[1] & 0x80 ? -1 : 1;
  7671. sum1[0] = sum1[1] = sum2[0] = sum2[1] = 0;
  7672. for (int l = 0; l < 4; ++l) {
  7673. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((uint16_t)qh[l/2] << (8 - 4*(l%2))) & 0x700)));
  7674. int lsum1 = 0, lsum2 = 0;
  7675. for (int j = 0; j < 8; ++j) {
  7676. lsum1 += q8[j] * grid[j];
  7677. lsum2 += q8[j];
  7678. }
  7679. q8 += 8;
  7680. sum1[l/2] += lsum1;
  7681. sum2[l/2] += lsum2*delta[l];
  7682. }
  7683. #if QK_K == 64
  7684. const int ls1 = 2*((sc[0] >> (8*(ib%2)+0)) & 0xf) + 1;
  7685. const int ls2 = 2*((sc[0] >> (8*(ib%2)+4)) & 0xf) + 1;
  7686. #else
  7687. const int ls1 = 2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1;
  7688. const int ls2 = 2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1;
  7689. #endif
  7690. sumi1 += sum1[0] * ls1 + sum1[1] * ls2;
  7691. sumi2 += sum2[0] * ls1 + sum2[1] * ls2;
  7692. qs += 4;
  7693. qh += 2;
  7694. }
  7695. #if QK_K == 64
  7696. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7697. #else
  7698. sumf += GGML_FP16_TO_FP32(scale.f16) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7699. #endif
  7700. }
  7701. *s = sumf;
  7702. #endif
  7703. }
  7704. void ggml_vec_dot_iq4_nl_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7705. assert(nrc == 1);
  7706. UNUSED(nrc);
  7707. UNUSED(bx);
  7708. UNUSED(by);
  7709. UNUSED(bs);
  7710. assert(n % QK4_NL == 0);
  7711. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  7712. const block_iq4_nl * restrict x = vx;
  7713. const block_q8_0 * restrict y = vy;
  7714. const int nb = n / QK4_NL;
  7715. #if defined __ARM_NEON
  7716. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7717. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7718. uint8x16x2_t q4bits;
  7719. int8x16x4_t q4b;
  7720. int8x16x4_t q8b;
  7721. int32x4_t prod_1, prod_2;
  7722. float sumf = 0;
  7723. for (int ib = 0; ib < nb; ib += 2) {
  7724. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  7725. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  7726. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  7727. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  7728. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  7729. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  7730. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7731. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7732. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7733. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7734. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7735. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7736. sumf +=
  7737. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  7738. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  7739. }
  7740. *s = sumf;
  7741. #elif defined __AVX2__
  7742. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7743. const __m128i m4b = _mm_set1_epi8(0x0f);
  7744. const __m256i mone = _mm256_set1_epi16(1);
  7745. __m256 accum1 = _mm256_setzero_ps();
  7746. __m256 accum2 = _mm256_setzero_ps();
  7747. for (int ib = 0; ib < nb; ib += 2) {
  7748. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  7749. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  7750. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  7751. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  7752. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7753. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7754. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7755. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7756. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7757. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7758. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  7759. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  7760. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  7761. _mm256_cvtepi32_ps(p_1), accum1);
  7762. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  7763. _mm256_cvtepi32_ps(p_2), accum2);
  7764. y += 2;
  7765. x += 2;
  7766. }
  7767. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  7768. #else
  7769. float sumf = 0;
  7770. for (int ib = 0; ib < nb; ++ib) {
  7771. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  7772. int sumi1 = 0, sumi2 = 0;
  7773. for (int j = 0; j < QK4_NL/2; ++j) {
  7774. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  7775. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  7776. }
  7777. sumf += d * (sumi1 + sumi2);
  7778. }
  7779. *s = sumf;
  7780. #endif
  7781. }
  7782. void ggml_vec_dot_iq4_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7783. assert(nrc == 1);
  7784. UNUSED(nrc);
  7785. UNUSED(bx);
  7786. UNUSED(by);
  7787. UNUSED(bs);
  7788. assert(n % QK_K == 0);
  7789. #if QK_K == 64
  7790. ggml_vec_dot_iq4_nl_q8_0(n, s, bs, vx, bx, vy, by, nrc);
  7791. #else
  7792. const block_iq4_xs * restrict x = vx;
  7793. const block_q8_K * restrict y = vy;
  7794. const int nb = n / QK_K;
  7795. #if defined __ARM_NEON
  7796. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7797. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7798. ggml_uint8x16x2_t q4bits;
  7799. ggml_int8x16x4_t q4b;
  7800. ggml_int8x16x4_t q8b;
  7801. int32x4_t prod_1, prod_2;
  7802. float sumf = 0;
  7803. for (int ibl = 0; ibl < nb; ++ibl) {
  7804. const int8_t * q8 = y[ibl].qs;
  7805. const uint8_t * q4 = x[ibl].qs;
  7806. uint16_t h = x[ibl].scales_h;
  7807. int sumi1 = 0, sumi2 = 0;
  7808. for (int ib = 0; ib < QK_K/64; ++ib) {
  7809. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  7810. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7811. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7812. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7813. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7814. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7815. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7816. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7817. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  7818. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  7819. h >>= 4;
  7820. sumi1 += vaddvq_s32(prod_1) * ls1;
  7821. sumi2 += vaddvq_s32(prod_2) * ls2;
  7822. }
  7823. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  7824. }
  7825. *s = sumf;
  7826. #elif defined __AVX2__
  7827. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7828. const __m128i m4b = _mm_set1_epi8(0x0f);
  7829. __m256 accum = _mm256_setzero_ps();
  7830. for (int ibl = 0; ibl < nb; ++ibl) {
  7831. const uint8_t * qs = x[ibl].qs;
  7832. const int8_t * q8 = y[ibl].qs;
  7833. uint16_t sh = x[ibl].scales_h;
  7834. __m256i sumi1 = _mm256_setzero_si256();
  7835. __m256i sumi2 = _mm256_setzero_si256();
  7836. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7837. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7838. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7839. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7840. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7841. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7842. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7843. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7844. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7845. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7846. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7847. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  7848. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  7849. sh >>= 4;
  7850. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  7851. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  7852. sumi1 = _mm256_add_epi32(p_1, sumi1);
  7853. sumi2 = _mm256_add_epi32(p_2, sumi2);
  7854. }
  7855. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  7856. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  7857. }
  7858. *s = hsum_float_8(accum);
  7859. #else
  7860. float sumf = 0;
  7861. for (int ibl = 0; ibl < nb; ++ibl) {
  7862. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  7863. uint16_t h = x[ibl].scales_h;
  7864. const uint8_t * qs = x[ibl].qs;
  7865. const int8_t * q8 = y[ibl].qs;
  7866. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7867. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  7868. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  7869. h >>= 4;
  7870. const float d1 = d4d8*(ls1 - 32);
  7871. const float d2 = d4d8*(ls2 - 32);
  7872. int sumi1 = 0, sumi2 = 0;
  7873. for (int j = 0; j < 16; ++j) {
  7874. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7875. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7876. }
  7877. sumf += d1 * (sumi1 + sumi2);
  7878. qs += 16;
  7879. q8 += 32;
  7880. sumi1 = sumi2 = 0;
  7881. for (int j = 0; j < 16; ++j) {
  7882. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7883. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7884. }
  7885. sumf += d2 * (sumi1 + sumi2);
  7886. qs += 16;
  7887. q8 += 32;
  7888. }
  7889. }
  7890. *s = sumf;
  7891. #endif
  7892. #endif
  7893. }
  7894. // ================================ IQ2 quantization =============================================
  7895. typedef struct {
  7896. uint64_t * grid;
  7897. int * map;
  7898. uint16_t * neighbours;
  7899. } iq2_entry_t;
  7900. static iq2_entry_t iq2_data[4] = {
  7901. {NULL, NULL, NULL},
  7902. {NULL, NULL, NULL},
  7903. {NULL, NULL, NULL},
  7904. {NULL, NULL, NULL},
  7905. };
  7906. static inline int iq2_data_index(enum ggml_type type) {
  7907. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  7908. return type == GGML_TYPE_IQ2_XXS ? 0 :
  7909. type == GGML_TYPE_IQ2_XS ? 1 :
  7910. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
  7911. }
  7912. static inline int iq2_grid_size(enum ggml_type type) {
  7913. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  7914. return type == GGML_TYPE_IQ2_XXS ? 256 :
  7915. type == GGML_TYPE_IQ2_XS ? 512 :
  7916. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
  7917. }
  7918. static int iq2_compare_func(const void * left, const void * right) {
  7919. const int * l = (const int *)left;
  7920. const int * r = (const int *)right;
  7921. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7922. }
  7923. void iq2xs_init_impl(enum ggml_type type) {
  7924. const int gindex = iq2_data_index(type);
  7925. const int grid_size = iq2_grid_size(type);
  7926. if (iq2_data[gindex].grid) {
  7927. return;
  7928. }
  7929. static const uint16_t kgrid_2bit_256[256] = {
  7930. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  7931. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  7932. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  7933. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  7934. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  7935. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  7936. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  7937. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  7938. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  7939. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  7940. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  7941. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  7942. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  7943. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  7944. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  7945. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  7946. };
  7947. static const uint16_t kgrid_2bit_512[512] = {
  7948. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  7949. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  7950. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  7951. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  7952. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  7953. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  7954. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  7955. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  7956. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  7957. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  7958. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  7959. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  7960. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  7961. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  7962. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  7963. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  7964. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  7965. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  7966. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  7967. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  7968. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  7969. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  7970. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  7971. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  7972. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  7973. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  7974. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  7975. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  7976. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  7977. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  7978. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  7979. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  7980. };
  7981. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  7982. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  7983. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  7984. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  7985. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  7986. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  7987. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  7988. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  7989. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  7990. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  7991. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  7992. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  7993. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  7994. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  7995. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  7996. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  7997. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  7998. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  7999. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  8000. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  8001. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  8002. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  8003. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  8004. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  8005. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  8006. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  8007. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  8008. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  8009. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  8010. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  8011. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  8012. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  8013. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  8014. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  8015. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  8016. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  8017. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  8018. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  8019. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  8020. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  8021. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  8022. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  8023. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  8024. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  8025. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  8026. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  8027. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  8028. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  8029. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  8030. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  8031. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  8032. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  8033. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  8034. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  8035. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  8036. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  8037. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  8038. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  8039. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  8040. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  8041. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  8042. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  8043. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  8044. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  8045. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  8046. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  8047. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  8048. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  8049. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  8050. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  8051. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  8052. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  8053. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  8054. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  8055. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  8056. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  8057. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  8058. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  8059. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  8060. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  8061. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  8062. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  8063. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  8064. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  8065. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  8066. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  8067. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  8068. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  8069. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  8070. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  8071. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  8072. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  8073. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  8074. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  8075. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  8076. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  8077. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  8078. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  8079. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  8080. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  8081. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  8082. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  8083. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  8084. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  8085. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  8086. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  8087. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  8088. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  8089. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  8090. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  8091. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  8092. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  8093. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  8094. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  8095. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  8096. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  8097. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  8098. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  8099. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  8100. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  8101. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  8102. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  8103. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  8104. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  8105. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  8106. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  8107. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  8108. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  8109. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  8110. };
  8111. static const uint16_t kgrid_2bit_1024[1024] = {
  8112. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8113. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  8114. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  8115. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  8116. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  8117. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  8118. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  8119. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  8120. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  8121. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  8122. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  8123. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  8124. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  8125. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  8126. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  8127. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  8128. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  8129. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  8130. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  8131. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  8132. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  8133. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  8134. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  8135. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  8136. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  8137. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  8138. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  8139. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  8140. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  8141. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  8142. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  8143. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  8144. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  8145. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  8146. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  8147. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  8148. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  8149. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  8150. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  8151. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  8152. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  8153. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  8154. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  8155. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  8156. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  8157. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  8158. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  8159. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  8160. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  8161. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  8162. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  8163. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  8164. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  8165. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  8166. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  8167. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  8168. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  8169. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  8170. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  8171. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  8172. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  8173. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  8174. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  8175. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  8176. };
  8177. const int kmap_size = 43692;
  8178. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  8179. const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  8180. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  8181. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  8182. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
  8183. uint64_t * kgrid_q2xs;
  8184. int * kmap_q2xs;
  8185. uint16_t * kneighbors_q2xs;
  8186. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8187. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  8188. for (int k = 0; k < grid_size; ++k) {
  8189. int8_t * pos = (int8_t *)(the_grid + k);
  8190. for (int i = 0; i < 8; ++i) {
  8191. int l = (kgrid[k] >> 2*i) & 0x3;
  8192. pos[i] = 2*l + 1;
  8193. }
  8194. }
  8195. kgrid_q2xs = the_grid;
  8196. iq2_data[gindex].grid = the_grid;
  8197. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  8198. iq2_data[gindex].map = kmap_q2xs;
  8199. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  8200. uint64_t aux64;
  8201. uint8_t * aux8 = (uint8_t *)&aux64;
  8202. for (int i = 0; i < grid_size; ++i) {
  8203. aux64 = kgrid_q2xs[i];
  8204. uint16_t index = 0;
  8205. for (int k=0; k<8; ++k) {
  8206. uint16_t q = (aux8[k] - 1)/2;
  8207. index |= (q << 2*k);
  8208. }
  8209. kmap_q2xs[index] = i;
  8210. }
  8211. int8_t pos[8];
  8212. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8213. int num_neighbors = 0, num_not_in_map = 0;
  8214. for (int i = 0; i < kmap_size; ++i) {
  8215. if (kmap_q2xs[i] >= 0) continue;
  8216. ++num_not_in_map;
  8217. for (int k = 0; k < 8; ++k) {
  8218. int l = (i >> 2*k) & 0x3;
  8219. pos[k] = 2*l + 1;
  8220. }
  8221. for (int j = 0; j < grid_size; ++j) {
  8222. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8223. int d2 = 0;
  8224. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8225. dist2[2*j+0] = d2;
  8226. dist2[2*j+1] = j;
  8227. }
  8228. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8229. int n = 0; int d2 = dist2[0];
  8230. int nhave = 1;
  8231. for (int j = 0; j < grid_size; ++j) {
  8232. if (dist2[2*j] > d2) {
  8233. if (nhave == nwant) break;
  8234. d2 = dist2[2*j];
  8235. ++nhave;
  8236. }
  8237. ++n;
  8238. }
  8239. num_neighbors += n;
  8240. }
  8241. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8242. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8243. iq2_data[gindex].neighbours = kneighbors_q2xs;
  8244. int counter = 0;
  8245. for (int i = 0; i < kmap_size; ++i) {
  8246. if (kmap_q2xs[i] >= 0) continue;
  8247. for (int k = 0; k < 8; ++k) {
  8248. int l = (i >> 2*k) & 0x3;
  8249. pos[k] = 2*l + 1;
  8250. }
  8251. for (int j = 0; j < grid_size; ++j) {
  8252. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8253. int d2 = 0;
  8254. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8255. dist2[2*j+0] = d2;
  8256. dist2[2*j+1] = j;
  8257. }
  8258. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8259. kmap_q2xs[i] = -(counter + 1);
  8260. int d2 = dist2[0];
  8261. uint16_t * start = &kneighbors_q2xs[counter++];
  8262. int n = 0, nhave = 1;
  8263. for (int j = 0; j < grid_size; ++j) {
  8264. if (dist2[2*j] > d2) {
  8265. if (nhave == nwant) break;
  8266. d2 = dist2[2*j];
  8267. ++nhave;
  8268. }
  8269. kneighbors_q2xs[counter++] = dist2[2*j+1];
  8270. ++n;
  8271. }
  8272. *start = n;
  8273. }
  8274. free(dist2);
  8275. }
  8276. void iq2xs_free_impl(enum ggml_type type) {
  8277. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  8278. const int gindex = iq2_data_index(type);
  8279. if (iq2_data[gindex].grid) {
  8280. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  8281. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  8282. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  8283. }
  8284. }
  8285. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  8286. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8287. int num_neighbors = neighbours[0];
  8288. GGML_ASSERT(num_neighbors > 0);
  8289. float best_d2 = FLT_MAX;
  8290. int grid_index = -1;
  8291. for (int j = 1; j <= num_neighbors; ++j) {
  8292. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8293. float d2 = 0;
  8294. for (int i = 0; i < 8; ++i) {
  8295. float q = pg[i];
  8296. float diff = scale*q - xval[i];
  8297. d2 += weight[i]*diff*diff;
  8298. }
  8299. if (d2 < best_d2) {
  8300. best_d2 = d2; grid_index = neighbours[j];
  8301. }
  8302. }
  8303. GGML_ASSERT(grid_index >= 0);
  8304. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8305. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  8306. return grid_index;
  8307. }
  8308. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  8309. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  8310. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8311. const int * kmap_q2xs = iq2_data[gindex].map;
  8312. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8313. GGML_ASSERT(quant_weights && "missing quantization weights");
  8314. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8315. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8316. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8317. GGML_ASSERT(n%QK_K == 0);
  8318. const int kMaxQ = 3;
  8319. const int64_t nbl = n/QK_K;
  8320. block_iq2_xxs * y = vy;
  8321. float scales[QK_K/32];
  8322. float weight[32];
  8323. float xval[32];
  8324. int8_t L[32];
  8325. int8_t Laux[32];
  8326. float waux[32];
  8327. uint8_t block_signs[4];
  8328. uint32_t q2[2*(QK_K/32)];
  8329. for (int ibl = 0; ibl < nbl; ++ibl) {
  8330. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8331. memset(q2, 0, QK_K/4);
  8332. float max_scale = 0;
  8333. const float * xbl = x + QK_K*ibl;
  8334. float sumx2 = 0;
  8335. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8336. float sigma2 = sumx2/QK_K;
  8337. for (int ib = 0; ib < QK_K/32; ++ib) {
  8338. const float * xb = xbl + 32*ib;
  8339. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8340. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8341. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8342. for (int k = 0; k < 4; ++k) {
  8343. int nflip = 0;
  8344. uint8_t s = 0;
  8345. for (int i = 0; i < 8; ++i) {
  8346. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8347. else {
  8348. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8349. }
  8350. }
  8351. if (nflip%2) {
  8352. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8353. for (int i = 1; i < 8; ++i) {
  8354. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8355. if (ax < min) {
  8356. min = ax; imin = i;
  8357. }
  8358. }
  8359. xval[8*k+imin] = -xval[8*k+imin];
  8360. s ^= (1 << imin);
  8361. }
  8362. block_signs[k] = s & 127;
  8363. }
  8364. float max = xval[0];
  8365. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8366. if (!max) {
  8367. scales[ib] = 0;
  8368. memset(L, 0, 32);
  8369. continue;
  8370. }
  8371. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  8372. float eff_max = scale*kMaxQ;
  8373. float best = 0;
  8374. for (int is = -6; is <= 6; ++is) {
  8375. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  8376. float this_scale = 1/id;
  8377. for (int k = 0; k < 4; ++k) {
  8378. for (int i = 0; i < 8; ++i) {
  8379. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8380. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8381. }
  8382. uint16_t u = 0;
  8383. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8384. int grid_index = kmap_q2xs[u];
  8385. if (grid_index < 0) {
  8386. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8387. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8388. }
  8389. }
  8390. float sumqx = 0, sumq2 = 0;
  8391. for (int i = 0; i < 32; ++i) {
  8392. float w = weight[i];
  8393. float q = 2*Laux[i] + 1;
  8394. sumqx += w*xval[i]*q;
  8395. sumq2 += w*q*q;
  8396. }
  8397. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8398. scale = sumqx/sumq2; best = scale*sumqx;
  8399. memcpy(L, Laux, 32);
  8400. }
  8401. }
  8402. if (scale > 0) {
  8403. float id = 1/scale;
  8404. for (int k = 0; k < 4; ++k) {
  8405. uint16_t u = 0;
  8406. for (int i = 0; i < 8; ++i) {
  8407. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8408. l = MAX(0, MIN(kMaxQ-1, l));
  8409. u |= (l << 2*i);
  8410. }
  8411. int grid_index = kmap_q2xs[u];
  8412. if (grid_index < 0) {
  8413. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8414. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8415. }
  8416. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  8417. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  8418. }
  8419. float sumqx = 0, sumq2 = 0;
  8420. for (int i = 0; i < 32; ++i) {
  8421. float w = weight[i];
  8422. float q = 2*L[i] + 1;
  8423. sumqx += w*xval[i]*q;
  8424. sumq2 += w*q*q;
  8425. }
  8426. if (sumq2 > 0) scale = sumqx/sumq2;
  8427. }
  8428. if (scale < 0) {
  8429. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8430. // and correspondingly flip quant signs.
  8431. scale = -scale;
  8432. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8433. }
  8434. for (int k = 0; k < 4; ++k) {
  8435. uint16_t u = 0;
  8436. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8437. int grid_index = kmap_q2xs[u];
  8438. if (grid_index < 0) {
  8439. printf("Oops: found point %u not on grid:", u);
  8440. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8441. printf("\n");
  8442. GGML_ASSERT(false);
  8443. }
  8444. q2[2*ib+0] |= (grid_index << 8*k);
  8445. q2[2*ib+1] |= (block_signs[k] << 7*k);
  8446. }
  8447. GGML_ASSERT(scale >= 0);
  8448. scales[ib] = scale;
  8449. max_scale = MAX(max_scale, scale);
  8450. }
  8451. if (!max_scale) {
  8452. memset(y[ibl].qs, 0, QK_K/4);
  8453. continue;
  8454. }
  8455. float d = max_scale/31;
  8456. y[ibl].d = GGML_FP32_TO_FP16(d);
  8457. float id = 1/d;
  8458. for (int ib = 0; ib < QK_K/32; ++ib) {
  8459. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8460. l = MAX(0, MIN(15, l));
  8461. q2[2*ib+1] |= ((uint32_t)l << 28);
  8462. }
  8463. memcpy(y[ibl].qs, q2, QK_K/4);
  8464. }
  8465. }
  8466. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  8467. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  8468. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8469. const int * kmap_q2xs = iq2_data[gindex].map;
  8470. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8471. GGML_ASSERT(quant_weights && "missing quantization weights");
  8472. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8473. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8474. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8475. GGML_ASSERT(n%QK_K == 0);
  8476. const int kMaxQ = 3;
  8477. const int64_t nbl = n/QK_K;
  8478. block_iq2_xs * y = vy;
  8479. float scales[QK_K/16];
  8480. float weight[16];
  8481. float xval[16];
  8482. int8_t L[16];
  8483. int8_t Laux[16];
  8484. float waux[16];
  8485. bool is_on_grid[2];
  8486. bool is_on_grid_aux[2];
  8487. uint8_t block_signs[2];
  8488. uint16_t q2[2*(QK_K/16)];
  8489. for (int ibl = 0; ibl < nbl; ++ibl) {
  8490. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8491. memset(q2, 0, QK_K/4);
  8492. memset(y[ibl].scales, 0, QK_K/32);
  8493. float max_scale = 0;
  8494. const float * xbl = x + QK_K*ibl;
  8495. float sumx2 = 0;
  8496. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8497. float sigma2 = sumx2/QK_K;
  8498. for (int ib = 0; ib < QK_K/16; ++ib) {
  8499. const float * xb = xbl + 16*ib;
  8500. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  8501. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8502. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  8503. for (int k = 0; k < 2; ++k) {
  8504. int nflip = 0;
  8505. uint8_t s = 0;
  8506. for (int i = 0; i < 8; ++i) {
  8507. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8508. else {
  8509. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8510. }
  8511. }
  8512. if (nflip%2) {
  8513. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8514. for (int i = 1; i < 8; ++i) {
  8515. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8516. if (ax < min) {
  8517. min = ax; imin = i;
  8518. }
  8519. }
  8520. xval[8*k+imin] = -xval[8*k+imin];
  8521. s ^= (1 << imin);
  8522. }
  8523. block_signs[k] = s & 127;
  8524. }
  8525. float max = xval[0];
  8526. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  8527. if (!max) {
  8528. scales[ib] = 0;
  8529. memset(L, 0, 16);
  8530. continue;
  8531. }
  8532. float best = 0;
  8533. float scale = max/(2*kMaxQ-1);
  8534. is_on_grid[0] = is_on_grid[1] = true;
  8535. for (int is = -9; is <= 9; ++is) {
  8536. float id = (2*kMaxQ-1+is*0.1f)/max;
  8537. float this_scale = 1/id;
  8538. for (int k = 0; k < 2; ++k) {
  8539. for (int i = 0; i < 8; ++i) {
  8540. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8541. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8542. }
  8543. uint16_t u = 0;
  8544. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8545. int grid_index = kmap_q2xs[u];
  8546. is_on_grid_aux[k] = true;
  8547. if (grid_index < 0) {
  8548. is_on_grid_aux[k] = false;
  8549. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8550. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8551. }
  8552. }
  8553. float sumqx = 0, sumq2 = 0;
  8554. for (int i = 0; i < 16; ++i) {
  8555. float w = weight[i];
  8556. float q = 2*Laux[i] + 1;
  8557. sumqx += w*xval[i]*q;
  8558. sumq2 += w*q*q;
  8559. }
  8560. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8561. scale = sumqx/sumq2; best = scale*sumqx;
  8562. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  8563. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8564. }
  8565. }
  8566. int n_not_ongrid = 0;
  8567. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8568. if (n_not_ongrid > 0 && scale > 0) {
  8569. float id = 1/scale;
  8570. for (int k = 0; k < 2; ++k) {
  8571. if (is_on_grid[k]) continue;
  8572. uint16_t u = 0;
  8573. for (int i = 0; i < 8; ++i) {
  8574. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8575. l = MAX(0, MIN(kMaxQ-1, l));
  8576. u |= (l << 2*i);
  8577. L[8*k + i] = l;
  8578. }
  8579. int grid_index = kmap_q2xs[u];
  8580. if (grid_index < 0) {
  8581. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8582. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8583. }
  8584. }
  8585. float sumqx = 0, sumq2 = 0;
  8586. for (int i = 0; i < 16; ++i) {
  8587. float w = weight[i];
  8588. float q = 2*L[i] + 1;
  8589. sumqx += w*xval[i]*q;
  8590. sumq2 += w*q*q;
  8591. }
  8592. if (sumq2 > 0) scale = sumqx/sumq2;
  8593. }
  8594. if (scale < 0) {
  8595. scale = -scale;
  8596. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8597. }
  8598. for (int k = 0; k < 2; ++k) {
  8599. uint16_t u = 0;
  8600. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8601. int grid_index = kmap_q2xs[u];
  8602. if (grid_index < 0) {
  8603. printf("Oops: found point %u not on grid:", u);
  8604. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8605. printf("\n");
  8606. GGML_ASSERT(false);
  8607. }
  8608. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  8609. }
  8610. GGML_ASSERT(scale >= 0);
  8611. scales[ib] = scale;
  8612. max_scale = MAX(max_scale, scale);
  8613. }
  8614. if (!max_scale) {
  8615. memset(y[ibl].qs, 0, QK_K/4);
  8616. continue;
  8617. }
  8618. float d = max_scale/31;
  8619. y[ibl].d = GGML_FP32_TO_FP16(d);
  8620. float id = 1/d;
  8621. for (int ib = 0; ib < QK_K/16; ++ib) {
  8622. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8623. l = MAX(0, MIN(15, l));
  8624. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  8625. else y[ibl].scales[ib/2] |= (l << 4);
  8626. }
  8627. memcpy(y[ibl].qs, q2, QK_K/4);
  8628. }
  8629. }
  8630. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  8631. GGML_ASSERT(n_per_row%QK_K == 0);
  8632. int64_t nblock = n_per_row/QK_K;
  8633. char * qrow = (char *)dst;
  8634. for (int64_t row = 0; row < nrow; ++row) {
  8635. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  8636. src += n_per_row;
  8637. qrow += nblock*sizeof(block_iq2_xxs);
  8638. }
  8639. return nrow * nblock * sizeof(block_iq2_xxs);
  8640. }
  8641. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  8642. GGML_ASSERT(n_per_row%QK_K == 0);
  8643. int64_t nblock = n_per_row/QK_K;
  8644. char * qrow = (char *)dst;
  8645. for (int64_t row = 0; row < nrow; ++row) {
  8646. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  8647. src += n_per_row;
  8648. qrow += nblock*sizeof(block_iq2_xs);
  8649. }
  8650. return nrow * nblock * sizeof(block_iq2_xs);
  8651. }
  8652. //
  8653. // ============================================= 3-bit using D4 lattice
  8654. //
  8655. typedef struct {
  8656. uint32_t * grid;
  8657. int * map;
  8658. uint16_t * neighbours;
  8659. } iq3_entry_t;
  8660. static iq3_entry_t iq3_data[2] = {
  8661. {NULL, NULL, NULL},
  8662. {NULL, NULL, NULL},
  8663. };
  8664. static inline int iq3_data_index(int grid_size) {
  8665. (void)grid_size;
  8666. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8667. return grid_size == 256 ? 0 : 1;
  8668. }
  8669. static int iq3_compare_func(const void * left, const void * right) {
  8670. const int * l = (const int *)left;
  8671. const int * r = (const int *)right;
  8672. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8673. }
  8674. void iq3xs_init_impl(int grid_size) {
  8675. const int gindex = iq3_data_index(grid_size);
  8676. if (iq3_data[gindex].grid) {
  8677. return;
  8678. }
  8679. static const uint16_t kgrid_256[256] = {
  8680. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  8681. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  8682. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  8683. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  8684. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  8685. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  8686. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  8687. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  8688. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  8689. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  8690. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  8691. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  8692. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  8693. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  8694. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  8695. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  8696. };
  8697. static const uint16_t kgrid_512[512] = {
  8698. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  8699. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  8700. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  8701. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  8702. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  8703. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  8704. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  8705. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  8706. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  8707. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  8708. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  8709. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  8710. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  8711. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  8712. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  8713. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  8714. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  8715. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  8716. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  8717. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  8718. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  8719. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  8720. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  8721. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  8722. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  8723. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  8724. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  8725. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  8726. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  8727. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  8728. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  8729. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  8730. };
  8731. const int kmap_size = 4096;
  8732. const int nwant = grid_size == 256 ? 2 : 3;
  8733. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  8734. uint32_t * kgrid_q3xs;
  8735. int * kmap_q3xs;
  8736. uint16_t * kneighbors_q3xs;
  8737. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8738. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  8739. for (int k = 0; k < grid_size; ++k) {
  8740. int8_t * pos = (int8_t *)(the_grid + k);
  8741. for (int i = 0; i < 4; ++i) {
  8742. int l = (kgrid[k] >> 3*i) & 0x7;
  8743. pos[i] = 2*l + 1;
  8744. }
  8745. }
  8746. kgrid_q3xs = the_grid;
  8747. iq3_data[gindex].grid = the_grid;
  8748. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  8749. iq3_data[gindex].map = kmap_q3xs;
  8750. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  8751. uint32_t aux32;
  8752. uint8_t * aux8 = (uint8_t *)&aux32;
  8753. for (int i = 0; i < grid_size; ++i) {
  8754. aux32 = kgrid_q3xs[i];
  8755. uint16_t index = 0;
  8756. for (int k=0; k<4; ++k) {
  8757. uint16_t q = (aux8[k] - 1)/2;
  8758. index |= (q << 3*k);
  8759. }
  8760. kmap_q3xs[index] = i;
  8761. }
  8762. int8_t pos[4];
  8763. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8764. int num_neighbors = 0, num_not_in_map = 0;
  8765. for (int i = 0; i < kmap_size; ++i) {
  8766. if (kmap_q3xs[i] >= 0) continue;
  8767. ++num_not_in_map;
  8768. for (int k = 0; k < 4; ++k) {
  8769. int l = (i >> 3*k) & 0x7;
  8770. pos[k] = 2*l + 1;
  8771. }
  8772. for (int j = 0; j < grid_size; ++j) {
  8773. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8774. int d2 = 0;
  8775. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8776. dist2[2*j+0] = d2;
  8777. dist2[2*j+1] = j;
  8778. }
  8779. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8780. int n = 0; int d2 = dist2[0];
  8781. int nhave = 1;
  8782. for (int j = 0; j < grid_size; ++j) {
  8783. if (dist2[2*j] > d2) {
  8784. if (nhave == nwant) break;
  8785. d2 = dist2[2*j];
  8786. ++nhave;
  8787. }
  8788. ++n;
  8789. }
  8790. num_neighbors += n;
  8791. }
  8792. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8793. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8794. iq3_data[gindex].neighbours = kneighbors_q3xs;
  8795. int counter = 0;
  8796. for (int i = 0; i < kmap_size; ++i) {
  8797. if (kmap_q3xs[i] >= 0) continue;
  8798. for (int k = 0; k < 4; ++k) {
  8799. int l = (i >> 3*k) & 0x7;
  8800. pos[k] = 2*l + 1;
  8801. }
  8802. for (int j = 0; j < grid_size; ++j) {
  8803. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8804. int d2 = 0;
  8805. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8806. dist2[2*j+0] = d2;
  8807. dist2[2*j+1] = j;
  8808. }
  8809. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8810. kmap_q3xs[i] = -(counter + 1);
  8811. int d2 = dist2[0];
  8812. uint16_t * start = &kneighbors_q3xs[counter++];
  8813. int n = 0, nhave = 1;
  8814. for (int j = 0; j < grid_size; ++j) {
  8815. if (dist2[2*j] > d2) {
  8816. if (nhave == nwant) break;
  8817. d2 = dist2[2*j];
  8818. ++nhave;
  8819. }
  8820. kneighbors_q3xs[counter++] = dist2[2*j+1];
  8821. ++n;
  8822. }
  8823. *start = n;
  8824. }
  8825. free(dist2);
  8826. }
  8827. void iq3xs_free_impl(int grid_size) {
  8828. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8829. const int gindex = iq3_data_index(grid_size);
  8830. if (iq3_data[gindex].grid) {
  8831. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  8832. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  8833. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  8834. }
  8835. }
  8836. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  8837. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8838. int num_neighbors = neighbours[0];
  8839. GGML_ASSERT(num_neighbors > 0);
  8840. float best_d2 = FLT_MAX;
  8841. int grid_index = -1;
  8842. for (int j = 1; j <= num_neighbors; ++j) {
  8843. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8844. float d2 = 0;
  8845. for (int i = 0; i < 4; ++i) {
  8846. float q = pg[i];
  8847. float diff = scale*q - xval[i];
  8848. d2 += weight[i]*diff*diff;
  8849. }
  8850. if (d2 < best_d2) {
  8851. best_d2 = d2; grid_index = neighbours[j];
  8852. }
  8853. }
  8854. GGML_ASSERT(grid_index >= 0);
  8855. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8856. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  8857. return grid_index;
  8858. }
  8859. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int64_t n,
  8860. const float * restrict quant_weights) {
  8861. const int gindex = iq3_data_index(grid_size);
  8862. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  8863. const int * kmap_q3xs = iq3_data[gindex].map;
  8864. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  8865. //GGML_ASSERT(quant_weights && "missing quantization weights");
  8866. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  8867. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  8868. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  8869. GGML_ASSERT(n%QK_K == 0);
  8870. const int kMaxQ = 8;
  8871. const int64_t nbl = n/QK_K;
  8872. ggml_fp16_t * dh;
  8873. uint8_t * qs;
  8874. int block_size;
  8875. if (grid_size == 256) {
  8876. block_iq3_xxs * y = vy;
  8877. dh = &y->d;
  8878. qs = y->qs;
  8879. block_size = sizeof(block_iq3_xxs);
  8880. } else {
  8881. block_iq3_s * y = vy;
  8882. dh = &y->d;
  8883. qs = y->qs;
  8884. block_size = sizeof(block_iq3_s);
  8885. }
  8886. int quant_size = block_size - sizeof(ggml_fp16_t);
  8887. float scales[QK_K/32];
  8888. float weight[32];
  8889. float xval[32];
  8890. int8_t L[32];
  8891. int8_t Laux[32];
  8892. float waux[32];
  8893. bool is_on_grid[8];
  8894. bool is_on_grid_aux[8];
  8895. uint8_t block_signs[8];
  8896. uint8_t q3[3*(QK_K/8)+QK_K/32];
  8897. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  8898. uint8_t * qh = q3 + 3*(QK_K/8);
  8899. for (int ibl = 0; ibl < nbl; ++ibl) {
  8900. dh[0] = GGML_FP32_TO_FP16(0.f);
  8901. memset(q3, 0, 3*QK_K/8+QK_K/32);
  8902. float max_scale = 0;
  8903. const float * xbl = x + QK_K*ibl;
  8904. float sumx2 = 0;
  8905. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8906. float sigma2 = 2*sumx2/QK_K;
  8907. for (int ib = 0; ib < QK_K/32; ++ib) {
  8908. const float * xb = xbl + 32*ib;
  8909. if (quant_weights) {
  8910. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8911. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8912. } else {
  8913. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  8914. }
  8915. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8916. for (int k = 0; k < 4; ++k) {
  8917. int nflip = 0;
  8918. uint8_t s = 0;
  8919. for (int i = 0; i < 8; ++i) {
  8920. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8921. else {
  8922. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8923. }
  8924. }
  8925. if (nflip%2) {
  8926. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8927. for (int i = 1; i < 8; ++i) {
  8928. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8929. if (ax < min) {
  8930. min = ax; imin = i;
  8931. }
  8932. }
  8933. xval[8*k+imin] = -xval[8*k+imin];
  8934. s ^= (1 << imin);
  8935. }
  8936. block_signs[k] = s & 127;
  8937. }
  8938. float max = xval[0];
  8939. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8940. if (!max) {
  8941. scales[ib] = 0;
  8942. memset(L, 0, 32);
  8943. continue;
  8944. }
  8945. float best = 0;
  8946. float scale = max/(2*kMaxQ-1);
  8947. for (int is = -15; is <= 15; ++is) {
  8948. float id = (2*kMaxQ-1+is*0.2f)/max;
  8949. float this_scale = 1/id;
  8950. for (int k = 0; k < 8; ++k) {
  8951. for (int i = 0; i < 4; ++i) {
  8952. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8953. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8954. }
  8955. uint16_t u = 0;
  8956. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  8957. int grid_index = kmap_q3xs[u];
  8958. is_on_grid_aux[k] = true;
  8959. if (grid_index < 0) {
  8960. is_on_grid_aux[k] = false;
  8961. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8962. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  8963. }
  8964. }
  8965. float sumqx = 0, sumq2 = 0;
  8966. for (int i = 0; i < 32; ++i) {
  8967. float w = weight[i];
  8968. float q = 2*Laux[i] + 1;
  8969. sumqx += w*xval[i]*q;
  8970. sumq2 += w*q*q;
  8971. }
  8972. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8973. scale = sumqx/sumq2; best = scale*sumqx;
  8974. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  8975. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8976. }
  8977. }
  8978. int n_not_ongrid = 0;
  8979. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8980. if (n_not_ongrid > 0 && scale > 0) {
  8981. float id = 1/scale;
  8982. for (int k = 0; k < 8; ++k) {
  8983. if (is_on_grid[k]) continue;
  8984. uint16_t u = 0;
  8985. for (int i = 0; i < 4; ++i) {
  8986. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8987. l = MAX(0, MIN(kMaxQ-1, l));
  8988. u |= (l << 3*i);
  8989. }
  8990. int grid_index = kmap_q3xs[u];
  8991. if (grid_index < 0) {
  8992. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8993. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  8994. }
  8995. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  8996. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  8997. }
  8998. float sumqx = 0, sumq2 = 0;
  8999. for (int i = 0; i < 32; ++i) {
  9000. float w = weight[i];
  9001. float q = 2*L[i] + 1;
  9002. sumqx += w*xval[i]*q;
  9003. sumq2 += w*q*q;
  9004. }
  9005. if (sumq2 > 0) scale = sumqx/sumq2;
  9006. }
  9007. if (scale < 0) {
  9008. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9009. // and correspondingly flip quant signs.
  9010. scale = -scale;
  9011. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  9012. }
  9013. for (int k = 0; k < 8; ++k) {
  9014. uint16_t u = 0;
  9015. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9016. int grid_index = kmap_q3xs[u];
  9017. if (grid_index < 0) {
  9018. printf("Oops: found point %u not on grid:", u);
  9019. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9020. printf("\n");
  9021. GGML_ASSERT(false);
  9022. }
  9023. if (grid_size == 256) {
  9024. q3[8*ib+k] = grid_index;
  9025. } else {
  9026. q3[8*ib+k] = grid_index & 255;
  9027. qh[ib] |= ((grid_index >> 8) << k);
  9028. }
  9029. }
  9030. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  9031. GGML_ASSERT(scale >= 0);
  9032. scales[ib] = scale;
  9033. max_scale = MAX(max_scale, scale);
  9034. }
  9035. if (!max_scale) {
  9036. memset(qs, 0, quant_size);
  9037. dh += block_size/sizeof(ggml_fp16_t);
  9038. qs += block_size;
  9039. continue;
  9040. }
  9041. float d = max_scale/31;
  9042. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  9043. float id = 1/d;
  9044. for (int ib = 0; ib < QK_K/32; ++ib) {
  9045. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9046. l = MAX(0, MIN(15, l));
  9047. scales_and_signs[ib] |= ((uint32_t)l << 28);
  9048. }
  9049. memcpy(qs, q3, quant_size);
  9050. dh += block_size/sizeof(ggml_fp16_t);
  9051. qs += block_size;
  9052. }
  9053. }
  9054. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9055. GGML_ASSERT(n_per_row%QK_K == 0);
  9056. int64_t nblock = n_per_row/QK_K;
  9057. char * qrow = (char *)dst;
  9058. for (int64_t row = 0; row < nrow; ++row) {
  9059. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  9060. src += n_per_row;
  9061. qrow += nblock*sizeof(block_iq3_xxs);
  9062. }
  9063. return nrow * nblock * sizeof(block_iq3_xxs);
  9064. }
  9065. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int64_t k) {
  9066. assert(k % QK_K == 0);
  9067. block_iq3_xxs * restrict y = vy;
  9068. quantize_row_iq3_xxs_reference(x, y, k);
  9069. }
  9070. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int64_t k) {
  9071. assert(k % QK_K == 0);
  9072. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  9073. }
  9074. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  9075. const float * restrict quant_weights,
  9076. float * scales,
  9077. float * weight,
  9078. float * xval,
  9079. int8_t * L,
  9080. int8_t * Laux,
  9081. float * waux,
  9082. bool * is_on_grid,
  9083. bool * is_on_grid_aux,
  9084. uint8_t * block_signs) {
  9085. const int gindex = iq3_data_index(512);
  9086. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9087. const int * kmap_q3xs = iq3_data[gindex].map;
  9088. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9089. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9090. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9091. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9092. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9093. GGML_ASSERT(n%QK_K == 0);
  9094. const int kMaxQ = 8;
  9095. const int64_t nbl = n/QK_K;
  9096. block_iq3_s * y = vy;
  9097. const int bs4 = block_size/4;
  9098. const int bs8 = block_size/8;
  9099. for (int ibl = 0; ibl < nbl; ++ibl) {
  9100. memset(&y[ibl], 0, sizeof(block_iq3_s));
  9101. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9102. uint8_t * qs = y[ibl].qs;
  9103. uint8_t * qh = y[ibl].qh;
  9104. uint8_t * signs = y[ibl].signs;
  9105. float max_scale = 0;
  9106. const float * xbl = x + QK_K*ibl;
  9107. float sumx2 = 0;
  9108. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9109. float sigma2 = 2*sumx2/QK_K;
  9110. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9111. const float * xb = xbl + block_size*ib;
  9112. if (quant_weights) {
  9113. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9114. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9115. } else {
  9116. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9117. }
  9118. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  9119. for (int k = 0; k < bs8; ++k) {
  9120. uint8_t s = 0;
  9121. for (int i = 0; i < 8; ++i) {
  9122. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9123. else {
  9124. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9125. }
  9126. }
  9127. block_signs[k] = s;
  9128. }
  9129. float max = xval[0];
  9130. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  9131. if (!max) {
  9132. scales[ib] = 0;
  9133. continue;
  9134. }
  9135. float best = 0;
  9136. float scale = max/(2*kMaxQ-1);
  9137. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  9138. for (int is = -9; is <= 9; ++is) {
  9139. float id = (2*kMaxQ-1+is*0.2f)/max;
  9140. float this_scale = 1/id;
  9141. for (int k = 0; k < bs4; ++k) {
  9142. for (int i = 0; i < 4; ++i) {
  9143. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9144. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9145. }
  9146. uint16_t u = 0;
  9147. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9148. int grid_index = kmap_q3xs[u];
  9149. is_on_grid_aux[k] = true;
  9150. if (grid_index < 0) {
  9151. is_on_grid_aux[k] = false;
  9152. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9153. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9154. }
  9155. }
  9156. float sumqx = 0, sumq2 = 0;
  9157. for (int i = 0; i < block_size; ++i) {
  9158. float w = weight[i];
  9159. float q = 2*Laux[i] + 1;
  9160. sumqx += w*xval[i]*q;
  9161. sumq2 += w*q*q;
  9162. }
  9163. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9164. scale = sumqx/sumq2; best = scale*sumqx;
  9165. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  9166. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9167. }
  9168. }
  9169. int n_not_ongrid = 0;
  9170. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9171. if (n_not_ongrid > 0 && scale > 0) {
  9172. float id = 1/scale;
  9173. for (int k = 0; k < bs4; ++k) {
  9174. //if (is_on_grid[k]) continue;
  9175. uint16_t u = 0;
  9176. for (int i = 0; i < 4; ++i) {
  9177. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9178. l = MAX(0, MIN(kMaxQ-1, l));
  9179. u |= (l << 3*i);
  9180. }
  9181. int grid_index = kmap_q3xs[u];
  9182. if (grid_index < 0) {
  9183. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9184. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9185. }
  9186. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9187. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9188. }
  9189. float sumqx = 0, sumq2 = 0;
  9190. for (int i = 0; i < block_size; ++i) {
  9191. float w = weight[i];
  9192. float q = 2*L[i] + 1;
  9193. sumqx += w*xval[i]*q;
  9194. sumq2 += w*q*q;
  9195. }
  9196. if (sumq2 > 0) scale = sumqx/sumq2;
  9197. }
  9198. if (scale < 0) {
  9199. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9200. // and correspondingly flip quant signs.
  9201. scale = -scale;
  9202. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  9203. }
  9204. for (int k = 0; k < bs4; ++k) {
  9205. uint16_t u = 0;
  9206. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9207. int grid_index = kmap_q3xs[u];
  9208. if (grid_index < 0) {
  9209. printf("Oops: found point %u not on grid:", u);
  9210. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9211. printf("\n");
  9212. GGML_ASSERT(false);
  9213. }
  9214. qs[k] = grid_index & 255;
  9215. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  9216. }
  9217. qs += bs4;
  9218. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  9219. signs += bs8;
  9220. GGML_ASSERT(scale >= 0);
  9221. scales[ib] = scale;
  9222. max_scale = MAX(max_scale, scale);
  9223. }
  9224. if (!max_scale) {
  9225. continue;
  9226. }
  9227. float d = max_scale/31;
  9228. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  9229. float id = 1/d;
  9230. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  9231. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  9232. l1 = MAX(0, MIN(15, l1));
  9233. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  9234. l2 = MAX(0, MIN(15, l2));
  9235. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  9236. }
  9237. }
  9238. }
  9239. #define IQ3S_BLOCK_SIZE 32
  9240. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9241. GGML_ASSERT(n_per_row%QK_K == 0);
  9242. int64_t nblock = n_per_row/QK_K;
  9243. float scales[QK_K/IQ3S_BLOCK_SIZE];
  9244. float weight[IQ3S_BLOCK_SIZE];
  9245. float xval[IQ3S_BLOCK_SIZE];
  9246. int8_t L[IQ3S_BLOCK_SIZE];
  9247. int8_t Laux[IQ3S_BLOCK_SIZE];
  9248. float waux[IQ3S_BLOCK_SIZE];
  9249. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  9250. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  9251. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  9252. char * qrow = (char *)dst;
  9253. for (int64_t row = 0; row < nrow; ++row) {
  9254. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  9255. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  9256. src += n_per_row;
  9257. qrow += nblock*sizeof(block_iq3_s);
  9258. }
  9259. return nrow * nblock * sizeof(block_iq3_s);
  9260. }
  9261. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int64_t k) {
  9262. assert(k % QK_K == 0);
  9263. block_iq3_s * restrict y = vy;
  9264. quantize_row_iq3_s_reference(x, y, k);
  9265. }
  9266. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int64_t k) {
  9267. assert(k % QK_K == 0);
  9268. quantize_iq3_s(x, y, 1, k, NULL);
  9269. }
  9270. // =================================== 1.5 bpw ===================================================
  9271. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9272. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  9273. int num_neighbors = neighbours[0];
  9274. GGML_ASSERT(num_neighbors > 0);
  9275. float best_score = 0;
  9276. int grid_index = -1;
  9277. for (int j = 1; j <= num_neighbors; ++j) {
  9278. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9279. float sumqx = 0, sumq2 = 0;
  9280. for (int i = 0; i < 8; ++i) {
  9281. float q = (pg[i] - 3)/2;
  9282. float w = weight[i];
  9283. sumqx += w*q*xval[i];
  9284. sumq2 += w*q*q;
  9285. }
  9286. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9287. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  9288. grid_index = neighbours[j];
  9289. }
  9290. }
  9291. if (grid_index < 0) {
  9292. for (int i = 0; i < ngrid; ++i) {
  9293. const int8_t * grid_i = (const int8_t *)(grid + i);
  9294. float sumqx = 0, sumq2 = 0;
  9295. for (int j = 0; j < 8; ++j) {
  9296. float w = weight[j];
  9297. float q = (grid_i[j] - 3)/2;
  9298. sumqx += w*q*xval[j];
  9299. sumq2 += w*q*q;
  9300. }
  9301. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9302. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  9303. grid_index = i;
  9304. }
  9305. }
  9306. }
  9307. if (grid_index < 0) {
  9308. printf("Oops, did not find grid point\n");
  9309. printf("Have %d neighbours\n", num_neighbors);
  9310. for (int j = 1; j <= num_neighbors; ++j) {
  9311. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9312. float sumqx = 0, sumq2 = 0;
  9313. for (int i = 0; i < 8; ++i) {
  9314. float q = (pg[i] - 3)/2;
  9315. float w = weight[i];
  9316. sumqx += w*q*xval[i];
  9317. sumq2 += w*q*q;
  9318. }
  9319. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9320. }
  9321. }
  9322. GGML_ASSERT(grid_index >= 0);
  9323. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9324. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  9325. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9326. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9327. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9328. return grid_index;
  9329. }
  9330. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9331. const float * restrict xval, const float * restrict weight, float scale, const float * restrict xg, int8_t * restrict L, int ngrid) {
  9332. int num_neighbors = neighbours[0];
  9333. GGML_ASSERT(num_neighbors > 0);
  9334. float best_score = FLT_MAX;
  9335. int grid_index = -1;
  9336. for (int j = 1; j <= num_neighbors; ++j) {
  9337. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9338. float d2 = 0;
  9339. for (int i = 0; i < 8; ++i) {
  9340. float q = xg[(pg[i] - 1)/2];
  9341. float w = weight[i];
  9342. float diff = scale*q - xval[i];
  9343. d2 += w*diff*diff;
  9344. }
  9345. if (d2 < best_score) {
  9346. best_score = d2;
  9347. grid_index = neighbours[j];
  9348. }
  9349. }
  9350. if (grid_index < 0) {
  9351. for (int i = 0; i < ngrid; ++i) {
  9352. const int8_t * grid_i = (const int8_t *)(grid + i);
  9353. float d2 = 0;
  9354. for (int j = 0; j < 8; ++j) {
  9355. float w = weight[j];
  9356. float q = xg[(grid_i[j] - 1)/2];
  9357. float diff = scale*q - xval[i];
  9358. d2 += w*diff*diff;
  9359. }
  9360. if (d2 < best_score) {
  9361. best_score = d2;
  9362. grid_index = i;
  9363. }
  9364. }
  9365. }
  9366. if (grid_index < 0) {
  9367. printf("Oops, did not find grid point\n");
  9368. printf("Have %d neighbours\n", num_neighbors);
  9369. for (int j = 1; j <= num_neighbors; ++j) {
  9370. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9371. float sumqx = 0, sumq2 = 0;
  9372. for (int i = 0; i < 8; ++i) {
  9373. float q = xg[(pg[i] - 1)/2];
  9374. float w = weight[i];
  9375. sumqx += w*q*xval[i];
  9376. sumq2 += w*q*q;
  9377. }
  9378. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9379. }
  9380. }
  9381. GGML_ASSERT(grid_index >= 0);
  9382. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9383. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9384. return grid_index;
  9385. }
  9386. static int iq1_sort_helper(const void * left, const void * right) {
  9387. const float * l = left;
  9388. const float * r = right;
  9389. return *l < *r ? -1 : *l > *r ? 1 : 0;
  9390. }
  9391. #define IQ1S_BLOCK_SIZE 32
  9392. #define IQ1M_BLOCK_SIZE 16
  9393. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  9394. float * scales,
  9395. float * weight,
  9396. float * sumx,
  9397. float * sumw,
  9398. float * pairs,
  9399. int8_t * L,
  9400. uint16_t * index,
  9401. int8_t * shifts) {
  9402. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  9403. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9404. const int * kmap_q2xs = iq2_data[gindex].map;
  9405. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9406. GGML_ASSERT(quant_weights && "missing quantization weights");
  9407. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9408. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9409. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9410. GGML_ASSERT(n%QK_K == 0);
  9411. block_iq1_s * y = vy;
  9412. const int64_t nbl = n/QK_K;
  9413. const int block_size = IQ1S_BLOCK_SIZE;
  9414. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  9415. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  9416. int * idx = (int *)(pairs + 1);
  9417. for (int ibl = 0; ibl < nbl; ++ibl) {
  9418. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9419. memset(y[ibl].qs, 0, QK_K/8);
  9420. memset(y[ibl].qh, 0, QK_K/16);
  9421. float max_scale = 0;
  9422. const float * xbl = x + QK_K*ibl;
  9423. float sumx2 = 0;
  9424. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9425. float sigma2 = 2*sumx2/QK_K;
  9426. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9427. const float * xb = xbl + block_size*ib;
  9428. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9429. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9430. float max = fabsf(xb[0]);
  9431. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9432. if (!max) {
  9433. scales[ib] = 0;
  9434. memset(L, 1, block_size);
  9435. continue;
  9436. }
  9437. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9438. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9439. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9440. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9441. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9442. // for each possible and score for each split.
  9443. for (int j = 0; j < block_size; ++j) {
  9444. pairs[2*j] = xb[j];
  9445. idx[2*j] = j;
  9446. }
  9447. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9448. {
  9449. sumx[0] = sumw[0] = 0;
  9450. for (int j = 0; j < block_size; ++j) {
  9451. int i = idx[2*j];
  9452. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  9453. sumw[j+1] = sumw[j] + weight[i];
  9454. }
  9455. }
  9456. float best_score = 0, scale = max;
  9457. int besti1 = -1, besti2 = -1, best_shift = 0;
  9458. for (int i1 = 0; i1 <= block_size; ++i1) {
  9459. for (int i2 = i1; i2 <= block_size; ++i2) {
  9460. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
  9461. float sumq2 = (sumw[i1] - sumw[0])*x_p[0]*x_p[0] + (sumw[i2] - sumw[i1])*x_p[1]*x_p[1] + (sumw[block_size] - sumw[i2])*x_p[2]*x_p[2];
  9462. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9463. scale = sumqx/sumq2; best_score = scale*sumqx;
  9464. besti1 = i1; besti2 = i2; best_shift = 1;
  9465. }
  9466. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
  9467. sumq2 = (sumw[i1] - sumw[0])*x_m[0]*x_m[0] + (sumw[i2] - sumw[i1])*x_m[1]*x_m[1] + (sumw[block_size] - sumw[i2])*x_m[2]*x_m[2];
  9468. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9469. scale = sumqx/sumq2; best_score = scale*sumqx;
  9470. besti1 = i1; besti2 = i2; best_shift = -1;
  9471. }
  9472. }
  9473. }
  9474. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  9475. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9476. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9477. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9478. if (scale < 0) {
  9479. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9480. scale = -scale; best_shift = -best_shift;
  9481. }
  9482. bool all_on_grid = true;
  9483. const float * xx = best_shift == 1 ? x_p : x_m;
  9484. for (int k = 0; k < block_size/8; ++k) {
  9485. uint16_t u = 0;
  9486. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9487. int grid_index = kmap_q2xs[u];
  9488. if (grid_index < 0) {
  9489. all_on_grid = false;
  9490. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9491. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9492. GGML_ASSERT(grid_index >= 0);
  9493. }
  9494. index[k] = grid_index;
  9495. }
  9496. if (!all_on_grid) {
  9497. float sumqx = 0, sumq2 = 0;
  9498. for (int k = 0; k < block_size/8; ++k) {
  9499. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9500. for (int j = 0; j < 8; ++j) {
  9501. float w = weight[8*k + j];
  9502. float q = xx[(pg[j] - 1)/2];
  9503. sumqx += w*q*xb[8*k+j];
  9504. sumq2 += w*q*q;
  9505. }
  9506. }
  9507. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  9508. }
  9509. uint16_t h = 0;
  9510. for (int k = 0; k < block_size/8; ++k) {
  9511. y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
  9512. h |= (index[k] >> 8) << 3*k;
  9513. }
  9514. y[ibl].qh[ib] = h;
  9515. GGML_ASSERT(scale >= 0);
  9516. scales[ib] = scale;
  9517. shifts[ib] = best_shift;
  9518. max_scale = MAX(max_scale, scale);
  9519. }
  9520. if (!max_scale) {
  9521. continue;
  9522. }
  9523. float d = max_scale/15;
  9524. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
  9525. float id = 1/d;
  9526. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9527. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9528. l = MAX(0, MIN(7, l));
  9529. if (shifts[ib] == -1) l |= 8;
  9530. y[ibl].qh[ib] |= (l << 12);
  9531. }
  9532. }
  9533. }
  9534. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9535. GGML_ASSERT(n_per_row%QK_K == 0);
  9536. float scales[QK_K/IQ1S_BLOCK_SIZE];
  9537. float weight[IQ1S_BLOCK_SIZE];
  9538. int8_t L[IQ1S_BLOCK_SIZE];
  9539. float sumx[IQ1S_BLOCK_SIZE+1];
  9540. float sumw[IQ1S_BLOCK_SIZE+1];
  9541. float pairs[2*IQ1S_BLOCK_SIZE];
  9542. uint16_t index[IQ1S_BLOCK_SIZE/8];
  9543. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  9544. int64_t nblock = n_per_row/QK_K;
  9545. char * qrow = (char *)dst;
  9546. for (int64_t row = 0; row < nrow; ++row) {
  9547. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
  9548. src += n_per_row;
  9549. qrow += nblock*sizeof(block_iq1_s);
  9550. }
  9551. return nrow * nblock * sizeof(block_iq1_s);
  9552. }
  9553. static void quantize_row_iq1_m_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  9554. float * scales,
  9555. float * weight,
  9556. float * pairs,
  9557. int8_t * L,
  9558. uint16_t * index,
  9559. int8_t * shifts) {
  9560. const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
  9561. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9562. const int * kmap_q2xs = iq2_data[gindex].map;
  9563. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9564. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9565. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9566. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9567. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9568. GGML_ASSERT(n%QK_K == 0);
  9569. block_iq1_m * y = vy;
  9570. const int64_t nbl = n/QK_K;
  9571. const int block_size = IQ1M_BLOCK_SIZE;
  9572. const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
  9573. const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
  9574. const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
  9575. int * idx = (int *)(pairs + 1);
  9576. float sumqx[4], sumq2[4];
  9577. iq1m_scale_t s;
  9578. const float * xx;
  9579. for (int ibl = 0; ibl < nbl; ++ibl) {
  9580. #if QK_K == 64
  9581. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9582. #endif
  9583. memset(y[ibl].qs, 0, QK_K/8);
  9584. memset(y[ibl].qh, 0, QK_K/16);
  9585. memset(y[ibl].scales, 0, QK_K/32);
  9586. float max_scale = 0;
  9587. const float * xbl = x + QK_K*ibl;
  9588. float sumx2 = 0;
  9589. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9590. float sigma2 = 2*sumx2/QK_K;
  9591. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9592. const float * xb = xbl + block_size*ib;
  9593. if (quant_weights) {
  9594. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9595. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9596. } else {
  9597. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9598. }
  9599. float max = fabsf(xb[0]);
  9600. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9601. if (!max) {
  9602. scales[ib] = 0;
  9603. memset(L, 1, block_size);
  9604. continue;
  9605. }
  9606. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9607. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9608. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9609. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9610. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9611. // for each possible and score for each split.
  9612. for (int j = 0; j < block_size; ++j) {
  9613. pairs[2*j] = xb[j];
  9614. idx[2*j] = j;
  9615. }
  9616. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9617. float best_score = 0, scale = max;
  9618. int besti1 = -1, besti2 = -1, best_k = -1;
  9619. // 0: +, +
  9620. // 1: +, -
  9621. // 2: -, +
  9622. // 3: -, -
  9623. for (int i1 = 0; i1 <= block_size; ++i1) {
  9624. for (int i2 = i1; i2 <= block_size; ++i2) {
  9625. memset(sumqx, 0, 4*sizeof(float));
  9626. memset(sumq2, 0, 4*sizeof(float));
  9627. for (int j = 0; j < i1; ++j) {
  9628. int i = idx[2*j];
  9629. if (i < block_size/2) {
  9630. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9631. sumqx[1] += weight[i]*x_p[0]*xb[i];
  9632. sumqx[2] += weight[i]*x_m[0]*xb[i];
  9633. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9634. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9635. sumq2[1] += weight[i]*x_p[0]*x_p[0];
  9636. sumq2[2] += weight[i]*x_m[0]*x_m[0];
  9637. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9638. } else {
  9639. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9640. sumqx[2] += weight[i]*x_p[0]*xb[i];
  9641. sumqx[1] += weight[i]*x_m[0]*xb[i];
  9642. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9643. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9644. sumq2[2] += weight[i]*x_p[0]*x_p[0];
  9645. sumq2[1] += weight[i]*x_m[0]*x_m[0];
  9646. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9647. }
  9648. }
  9649. for (int j = i1; j < i2; ++j) {
  9650. int i = idx[2*j];
  9651. if (i < block_size/2) {
  9652. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9653. sumqx[1] += weight[i]*x_p[1]*xb[i];
  9654. sumqx[2] += weight[i]*x_m[1]*xb[i];
  9655. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9656. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9657. sumq2[1] += weight[i]*x_p[1]*x_p[1];
  9658. sumq2[2] += weight[i]*x_m[1]*x_m[1];
  9659. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9660. } else {
  9661. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9662. sumqx[2] += weight[i]*x_p[1]*xb[i];
  9663. sumqx[1] += weight[i]*x_m[1]*xb[i];
  9664. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9665. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9666. sumq2[2] += weight[i]*x_p[1]*x_p[1];
  9667. sumq2[1] += weight[i]*x_m[1]*x_m[1];
  9668. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9669. }
  9670. }
  9671. for (int j = i2; j < block_size; ++j) {
  9672. int i = idx[2*j];
  9673. if (i < block_size/2) {
  9674. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9675. sumqx[1] += weight[i]*x_p[2]*xb[i];
  9676. sumqx[2] += weight[i]*x_m[2]*xb[i];
  9677. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9678. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9679. sumq2[1] += weight[i]*x_p[2]*x_p[2];
  9680. sumq2[2] += weight[i]*x_m[2]*x_m[2];
  9681. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9682. } else {
  9683. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9684. sumqx[2] += weight[i]*x_p[2]*xb[i];
  9685. sumqx[1] += weight[i]*x_m[2]*xb[i];
  9686. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9687. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9688. sumq2[2] += weight[i]*x_p[2]*x_p[2];
  9689. sumq2[1] += weight[i]*x_m[2]*x_m[2];
  9690. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9691. }
  9692. }
  9693. for (int k = 0; k < 4; ++k) {
  9694. if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
  9695. scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
  9696. besti1 = i1; besti2 = i2; best_k = k;
  9697. }
  9698. }
  9699. }
  9700. }
  9701. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_k >= 0);
  9702. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9703. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9704. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9705. if (scale < 0) {
  9706. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9707. scale = -scale;
  9708. best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
  9709. }
  9710. bool all_on_grid = true;
  9711. for (int k = 0; k < block_size/8; ++k) {
  9712. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9713. else xx = best_k%2 == 0 ? x_p : x_m;
  9714. uint16_t u = 0;
  9715. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9716. int grid_index = kmap_q2xs[u];
  9717. if (grid_index < 0) {
  9718. all_on_grid = false;
  9719. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9720. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9721. GGML_ASSERT(grid_index >= 0);
  9722. }
  9723. index[k] = grid_index;
  9724. }
  9725. if (!all_on_grid) {
  9726. float sumqx_f = 0, sumq2_f = 0;
  9727. for (int k = 0; k < block_size/8; ++k) {
  9728. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9729. else xx = best_k%2 == 0 ? x_p : x_m;
  9730. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9731. for (int j = 0; j < 8; ++j) {
  9732. float w = weight[8*k + j];
  9733. float q = xx[(pg[j] - 1)/2];
  9734. sumqx_f += w*q*xb[8*k+j];
  9735. sumq2_f += w*q*q;
  9736. }
  9737. }
  9738. if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
  9739. }
  9740. y[ibl].qs[2*ib + 0] = index[0] & 255;
  9741. y[ibl].qs[2*ib + 1] = index[1] & 255;
  9742. y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
  9743. GGML_ASSERT(scale >= 0);
  9744. scales[ib] = scale;
  9745. shifts[ib] = best_k;
  9746. max_scale = MAX(max_scale, scale);
  9747. }
  9748. if (!max_scale) {
  9749. continue;
  9750. }
  9751. uint16_t * sc = (uint16_t *)y[ibl].scales;
  9752. #if QK_K == 64
  9753. float d = max_scale/31;
  9754. #else
  9755. float d = max_scale/15;
  9756. #endif
  9757. float id = 1/d;
  9758. float sumqx_f = 0, sumq2_f = 0;
  9759. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9760. int l = nearest_int(0.5f*(id*scales[ib+0]-1));
  9761. #if QK_K == 64
  9762. l = MAX(0, MIN(15, l));
  9763. sc[ib/4] |= (l << 4*(ib%4));
  9764. #else
  9765. l = MAX(0, MIN(7, l));
  9766. sc[ib/4] |= (l << 3*(ib%4));
  9767. #endif
  9768. y[ibl].qh[ib] |= masks[shifts[ib]];
  9769. const float * xb = xbl + block_size*ib;
  9770. if (quant_weights) {
  9771. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9772. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9773. } else {
  9774. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9775. }
  9776. for (int k = 0; k < block_size/8; ++k) {
  9777. if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
  9778. else xx = shifts[ib]%2 == 0 ? x_p : x_m;
  9779. const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
  9780. for (int j = 0; j < 8; ++j) {
  9781. float w = weight[8*k + j];
  9782. float q = xx[(pg[j] - 1)/2]*(2*l+1);
  9783. sumqx_f += w*q*xb[8*k+j];
  9784. sumq2_f += w*q*q;
  9785. }
  9786. }
  9787. }
  9788. if (sumq2_f > 0) d = sumqx_f/sumq2_f;
  9789. s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
  9790. #if QK_K == 64
  9791. y[ibl].d = s.f16;
  9792. #else
  9793. sc[0] |= ((s.u16 & 0x000f) << 12);
  9794. sc[1] |= ((s.u16 & 0x00f0) << 8);
  9795. sc[2] |= ((s.u16 & 0x0f00) << 4);
  9796. sc[3] |= ((s.u16 & 0xf000) << 0);
  9797. #endif
  9798. }
  9799. }
  9800. size_t quantize_iq1_m(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9801. GGML_ASSERT(n_per_row%QK_K == 0);
  9802. float scales[QK_K/IQ1M_BLOCK_SIZE];
  9803. float weight[IQ1M_BLOCK_SIZE];
  9804. int8_t L[IQ1M_BLOCK_SIZE];
  9805. float pairs[2*IQ1M_BLOCK_SIZE];
  9806. uint16_t index[IQ1M_BLOCK_SIZE/8];
  9807. int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
  9808. int64_t nblock = n_per_row/QK_K;
  9809. char * qrow = (char *)dst;
  9810. for (int64_t row = 0; row < nrow; ++row) {
  9811. quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
  9812. src += n_per_row;
  9813. qrow += nblock*sizeof(block_iq1_m);
  9814. }
  9815. return nrow * nblock * sizeof(block_iq1_m);
  9816. }
  9817. // ============================ 4-bit non-linear quants
  9818. static inline int best_index_int8(int n, const int8_t * val, float x) {
  9819. if (x <= val[0]) return 0;
  9820. if (x >= val[n-1]) return n-1;
  9821. int ml = 0, mu = n-1;
  9822. while (mu-ml > 1) {
  9823. int mav = (ml+mu)/2;
  9824. if (x < val[mav]) mu = mav; else ml = mav;
  9825. }
  9826. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  9827. }
  9828. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  9829. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  9830. float * scales, float * weight, uint8_t * L,
  9831. const int8_t * values,
  9832. const float * quant_weights,
  9833. const int ntry) {
  9834. float sigma2 = 0;
  9835. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  9836. sigma2 *= 2.f/super_block_size;
  9837. memset(q4, 0, super_block_size/2);
  9838. dh[0] = GGML_FP32_TO_FP16(0.f);
  9839. float max_scale = 0, amax_scale = 0;
  9840. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9841. const float * xb = x + ib*block_size;
  9842. uint8_t * Lb = L + ib*block_size;
  9843. if (quant_weights) {
  9844. const float * qw = quant_weights + ib*block_size;
  9845. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  9846. } else {
  9847. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  9848. }
  9849. float amax = 0, max = 0;
  9850. for (int j = 0; j < block_size; ++j) {
  9851. float ax = fabsf(xb[j]);
  9852. if (ax > amax) {
  9853. amax = ax; max = xb[j];
  9854. }
  9855. }
  9856. if (!amax) {
  9857. scales[ib] = 0;
  9858. continue;
  9859. }
  9860. float d = ntry > 0 ? -max/values[0] : max/values[0];
  9861. float id = 1/d;
  9862. float sumqx = 0, sumq2 = 0;
  9863. for (int j = 0; j < block_size; ++j) {
  9864. float al = id*xb[j];
  9865. int l = best_index_int8(16, values, al);
  9866. Lb[j] = l;
  9867. float q = values[l];
  9868. float w = weight[j];
  9869. sumqx += w*q*xb[j];
  9870. sumq2 += w*q*q;
  9871. }
  9872. d = sumqx/sumq2;
  9873. float best = d*sumqx;
  9874. for (int itry = -ntry; itry <= ntry; ++itry) {
  9875. id = (itry + values[0])/max;
  9876. sumqx = sumq2 = 0;
  9877. for (int j = 0; j < block_size; ++j) {
  9878. float al = id*xb[j];
  9879. int l = best_index_int8(16, values, al);
  9880. float q = values[l];
  9881. float w = weight[j];
  9882. sumqx += w*q*xb[j];
  9883. sumq2 += w*q*q;
  9884. }
  9885. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9886. d = sumqx/sumq2; best = d * sumqx;
  9887. }
  9888. }
  9889. scales[ib] = d;
  9890. float abs_d = fabsf(d);
  9891. if (abs_d > amax_scale) {
  9892. amax_scale = abs_d; max_scale = d;
  9893. }
  9894. }
  9895. if (super_block_size/block_size > 1) {
  9896. int nb = super_block_size/block_size;
  9897. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  9898. float d = -max_scale/32;
  9899. dh[0] = GGML_FP32_TO_FP16(d);
  9900. float id = d ? 1/d : 0.f;
  9901. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9902. int l = nearest_int(id*scales[ib]);
  9903. l = MAX(-32, MIN(31, l));
  9904. float dl = d * l;
  9905. float idl = dl ? 1/dl : 0.f;
  9906. uint8_t * Lb = L + ib*block_size;
  9907. const float * xb = x + ib*block_size;
  9908. for (int j = 0; j < block_size; ++j) {
  9909. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  9910. }
  9911. l += 32;
  9912. uint8_t l_l = l & 0xf;
  9913. uint8_t l_h = l >> 4;
  9914. if (ib%2 == 0) scales_l[ib/2] = l_l;
  9915. else scales_l[ib/2] |= (l_l << 4);
  9916. scales_h[ib/8] |= (l_h << 2*(ib%8));
  9917. }
  9918. } else {
  9919. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  9920. if (ntry > 0) {
  9921. float id = scales[0] ? 1/scales[0] : 0;
  9922. for (int j = 0; j < super_block_size; ++j) {
  9923. L[j] = best_index_int8(16, values, id*x[j]);
  9924. }
  9925. }
  9926. }
  9927. for (int i = 0; i < super_block_size/32; ++i) {
  9928. for (int j = 0; j < 16; ++j) {
  9929. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  9930. }
  9931. }
  9932. }
  9933. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9934. GGML_ASSERT(n_per_row%QK4_NL == 0);
  9935. int64_t nblock = n_per_row/QK4_NL;
  9936. char * qrow = (char *)dst;
  9937. uint8_t L[QK4_NL];
  9938. float weight[QK4_NL];
  9939. uint16_t unused_h;
  9940. uint8_t * unused_l = NULL;
  9941. float scale;
  9942. for (int64_t row = 0; row < nrow; ++row) {
  9943. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  9944. for (int ibl = 0; ibl < nblock; ++ibl) {
  9945. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  9946. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9947. &scale, weight, L, kvalues_iq4nl, qw, 7);
  9948. }
  9949. src += n_per_row;
  9950. qrow += nblock*sizeof(block_iq4_nl);
  9951. }
  9952. return nrow * nblock * sizeof(block_iq4_nl);
  9953. }
  9954. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int64_t k) {
  9955. GGML_ASSERT(k%QK4_NL == 0);
  9956. int64_t nblock = k/QK4_NL;
  9957. uint8_t L[QK4_NL];
  9958. float weight[QK4_NL];
  9959. uint16_t unused_h;
  9960. uint8_t * unused_l = NULL;
  9961. float scale;
  9962. block_iq4_nl * iq4 = (block_iq4_nl *)vy;
  9963. for (int ibl = 0; ibl < nblock; ++ibl) {
  9964. quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9965. &scale, weight, L, kvalues_iq4nl, NULL, -1);
  9966. }
  9967. }
  9968. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int64_t k) {
  9969. assert(k % QK4_NL == 0);
  9970. quantize_row_iq4_nl(x, y, k);
  9971. }
  9972. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9973. #if QK_K == 64
  9974. return quantize_iq4_nl(src, dst, nrow, n_per_row, quant_weights);
  9975. #else
  9976. GGML_ASSERT(n_per_row%QK_K == 0);
  9977. int64_t nblock = n_per_row/QK_K;
  9978. char * qrow = (char *)dst;
  9979. uint8_t L[QK_K];
  9980. float weight[32];
  9981. float scales[QK_K/32];
  9982. for (int64_t row = 0; row < nrow; ++row) {
  9983. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  9984. for (int ibl = 0; ibl < nblock; ++ibl) {
  9985. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  9986. quantize_row_iq4_nl_impl(QK_K, 32, src + QK_K*ibl, &iq4[ibl].d, iq4[ibl].qs, &iq4[ibl].scales_h, iq4[ibl].scales_l,
  9987. scales, weight, L, kvalues_iq4nl, qw, 7);
  9988. }
  9989. src += n_per_row;
  9990. qrow += nblock*sizeof(block_iq4_xs);
  9991. }
  9992. return nrow * nblock * sizeof(block_iq4_xs);
  9993. #endif
  9994. }
  9995. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int64_t k) {
  9996. assert(k % QK_K == 0);
  9997. block_iq4_xs * restrict y = vy;
  9998. quantize_row_iq4_xs_reference(x, y, k);
  9999. }
  10000. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int64_t k) {
  10001. assert(k % QK_K == 0);
  10002. quantize_iq4_xs(x, y, 1, k, NULL);
  10003. }
  10004. // =============================== 2.5625 bpw
  10005. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  10006. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  10007. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  10008. const int * kmap_q2xs = iq2_data[gindex].map;
  10009. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  10010. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  10011. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  10012. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  10013. GGML_ASSERT(n%QK_K == 0);
  10014. const int kMaxQ = 3;
  10015. const int64_t nbl = n/QK_K;
  10016. block_iq2_s * y = vy;
  10017. float scales[QK_K/16];
  10018. float weight[16];
  10019. float xval[16];
  10020. int8_t L[16];
  10021. int8_t Laux[16];
  10022. float waux[16];
  10023. bool is_on_grid[2];
  10024. bool is_on_grid_aux[2];
  10025. uint8_t block_signs[2];
  10026. for (int ibl = 0; ibl < nbl; ++ibl) {
  10027. memset(&y[ibl], 0, sizeof(block_iq2_s));
  10028. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  10029. float max_scale = 0;
  10030. const float * xbl = x + QK_K*ibl;
  10031. float sumx2 = 0;
  10032. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10033. float sigma2 = 2*sumx2/QK_K;
  10034. for (int ib = 0; ib < QK_K/16; ++ib) {
  10035. const float * xb = xbl + 16*ib;
  10036. if (quant_weights) {
  10037. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  10038. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10039. } else {
  10040. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  10041. }
  10042. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  10043. for (int k = 0; k < 2; ++k) {
  10044. uint8_t s = 0;
  10045. for (int i = 0; i < 8; ++i) {
  10046. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  10047. else {
  10048. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  10049. }
  10050. }
  10051. block_signs[k] = s;
  10052. }
  10053. float max = xval[0];
  10054. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  10055. if (!max) {
  10056. scales[ib] = 0;
  10057. continue;
  10058. }
  10059. float best = 0;
  10060. float scale = max/(2*kMaxQ-1);
  10061. is_on_grid[0] = is_on_grid[1] = true;
  10062. for (int is = -9; is <= 9; ++is) {
  10063. float id = (2*kMaxQ-1+is*0.1f)/max;
  10064. float this_scale = 1/id;
  10065. for (int k = 0; k < 2; ++k) {
  10066. for (int i = 0; i < 8; ++i) {
  10067. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10068. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  10069. }
  10070. uint16_t u = 0;
  10071. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  10072. int grid_index = kmap_q2xs[u];
  10073. is_on_grid_aux[k] = true;
  10074. if (grid_index < 0) {
  10075. is_on_grid_aux[k] = false;
  10076. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10077. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  10078. }
  10079. }
  10080. float sumqx = 0, sumq2 = 0;
  10081. for (int i = 0; i < 16; ++i) {
  10082. float w = weight[i];
  10083. float q = 2*Laux[i] + 1;
  10084. sumqx += w*xval[i]*q;
  10085. sumq2 += w*q*q;
  10086. }
  10087. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10088. scale = sumqx/sumq2; best = scale*sumqx;
  10089. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  10090. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  10091. }
  10092. }
  10093. int n_not_ongrid = 0;
  10094. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  10095. if (n_not_ongrid > 0 && scale > 0) {
  10096. float id = 1/scale;
  10097. for (int k = 0; k < 2; ++k) {
  10098. if (is_on_grid[k]) continue;
  10099. uint16_t u = 0;
  10100. for (int i = 0; i < 8; ++i) {
  10101. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10102. l = MAX(0, MIN(kMaxQ-1, l));
  10103. u |= (l << 2*i);
  10104. L[8*k + i] = l;
  10105. }
  10106. int grid_index = kmap_q2xs[u];
  10107. if (grid_index < 0) {
  10108. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10109. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  10110. }
  10111. }
  10112. float sumqx = 0, sumq2 = 0;
  10113. for (int i = 0; i < 16; ++i) {
  10114. float w = weight[i];
  10115. float q = 2*L[i] + 1;
  10116. sumqx += w*xval[i]*q;
  10117. sumq2 += w*q*q;
  10118. }
  10119. if (sumq2 > 0) scale = sumqx/sumq2;
  10120. }
  10121. if (scale < 0) {
  10122. scale = -scale;
  10123. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  10124. }
  10125. for (int k = 0; k < 2; ++k) {
  10126. uint16_t u = 0;
  10127. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  10128. int grid_index = kmap_q2xs[u];
  10129. if (grid_index < 0) {
  10130. printf("Oops: found point %u not on grid:", u);
  10131. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  10132. printf("\n");
  10133. GGML_ASSERT(false);
  10134. }
  10135. const int i8 = 2*ib + k;
  10136. y[ibl].qs[i8] = grid_index & 255;
  10137. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  10138. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  10139. }
  10140. GGML_ASSERT(scale >= 0);
  10141. scales[ib] = scale;
  10142. max_scale = MAX(max_scale, scale);
  10143. }
  10144. if (!max_scale) {
  10145. continue;
  10146. }
  10147. float d = max_scale/31;
  10148. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  10149. float id = 1/d;
  10150. for (int ib = 0; ib < QK_K/16; ++ib) {
  10151. int l = nearest_int(0.5f*(id*scales[ib]-1));
  10152. l = MAX(0, MIN(15, l));
  10153. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  10154. else y[ibl].scales[ib/2] |= (l << 4);
  10155. }
  10156. }
  10157. }
  10158. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  10159. GGML_ASSERT(n_per_row%QK_K == 0);
  10160. int64_t nblock = n_per_row/QK_K;
  10161. char * qrow = (char *)dst;
  10162. for (int64_t row = 0; row < nrow; ++row) {
  10163. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  10164. src += n_per_row;
  10165. qrow += nblock*sizeof(block_iq2_s);
  10166. }
  10167. return nrow * nblock * sizeof(block_iq2_s);
  10168. }
  10169. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int64_t k) {
  10170. assert(k % QK_K == 0);
  10171. quantize_iq2_s(x, y, 1, k, NULL);
  10172. }
  10173. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int64_t k) {
  10174. assert(k % QK_K == 0);
  10175. block_iq2_s * restrict y = vy;
  10176. quantize_row_iq2_s_reference(x, y, k);
  10177. }