ggml-quants.c 507 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385
  1. #define GGML_COMMON_IMPL_C
  2. #include "ggml-common.h"
  3. #include "ggml-quants.h"
  4. #include "ggml-impl.h"
  5. #define GGML_COMMON_IMPL_C
  6. #include "ggml-common.h"
  7. #include <math.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <float.h>
  11. #include <stdlib.h> // for qsort
  12. #include <stdio.h> // for GGML_ASSERT
  13. #define UNUSED GGML_UNUSED
  14. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  15. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  16. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  17. // multiply int8_t, add results pairwise twice
  18. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  19. // Get absolute values of x vectors
  20. const __m128i ax = _mm_sign_epi8(x, x);
  21. // Sign the values of the y vectors
  22. const __m128i sy = _mm_sign_epi8(y, x);
  23. // Perform multiplication and create 16-bit values
  24. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  25. const __m128i ones = _mm_set1_epi16(1);
  26. return _mm_madd_epi16(ones, dot);
  27. }
  28. #if __AVX__ || __AVX2__ || __AVX512F__
  29. // horizontally add 8 floats
  30. static inline float hsum_float_8(const __m256 x) {
  31. __m128 res = _mm256_extractf128_ps(x, 1);
  32. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  33. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  34. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  35. return _mm_cvtss_f32(res);
  36. }
  37. // horizontally add 8 int32_t
  38. static inline int hsum_i32_8(const __m256i a) {
  39. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  40. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  41. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  42. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  43. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  44. }
  45. // horizontally add 4 int32_t
  46. static inline int hsum_i32_4(const __m128i a) {
  47. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  48. const __m128i sum64 = _mm_add_epi32(hi64, a);
  49. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  50. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  51. }
  52. #if defined(__AVX2__) || defined(__AVX512F__)
  53. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  54. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  55. uint32_t x32;
  56. memcpy(&x32, x, sizeof(uint32_t));
  57. const __m256i shuf_mask = _mm256_set_epi64x(
  58. 0x0303030303030303, 0x0202020202020202,
  59. 0x0101010101010101, 0x0000000000000000);
  60. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  61. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  62. bytes = _mm256_or_si256(bytes, bit_mask);
  63. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  64. }
  65. // Unpack 32 4-bit fields into 32 bytes
  66. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  67. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  68. {
  69. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  70. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  71. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  72. return _mm256_and_si256(lowMask, bytes);
  73. }
  74. // add int16_t pairwise and return as float vector
  75. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  76. const __m256i ones = _mm256_set1_epi16(1);
  77. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  78. return _mm256_cvtepi32_ps(summed_pairs);
  79. }
  80. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  81. #if defined(__AVXVNNI__) || (defined(__AVX512VNNI__) && defined(__AVX512VL__))
  82. const __m256i zero = _mm256_setzero_si256();
  83. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  84. return _mm256_cvtepi32_ps(summed_pairs);
  85. #else
  86. // Perform multiplication and create 16-bit values
  87. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  88. return sum_i16_pairs_float(dot);
  89. #endif
  90. }
  91. // multiply int8_t, add results pairwise twice and return as float vector
  92. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  93. #if __AVXVNNIINT8__
  94. const __m256i zero = _mm256_setzero_si256();
  95. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  96. return _mm256_cvtepi32_ps(summed_pairs);
  97. #else
  98. // Get absolute values of x vectors
  99. const __m256i ax = _mm256_sign_epi8(x, x);
  100. // Sign the values of the y vectors
  101. const __m256i sy = _mm256_sign_epi8(y, x);
  102. return mul_sum_us8_pairs_float(ax, sy);
  103. #endif
  104. }
  105. static inline __m128i packNibbles( __m256i bytes )
  106. {
  107. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  108. #if __AVX512F__
  109. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  110. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  111. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  112. #else
  113. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  114. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  115. __m256i low = _mm256_and_si256( lowByte, bytes );
  116. high = _mm256_srli_epi16( high, 4 );
  117. bytes = _mm256_or_si256( low, high );
  118. // Compress uint16_t lanes into bytes
  119. __m128i r0 = _mm256_castsi256_si128( bytes );
  120. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  121. return _mm_packus_epi16( r0, r1 );
  122. #endif
  123. }
  124. #elif defined(__AVX__)
  125. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  126. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  127. uint32_t x32;
  128. memcpy(&x32, x, sizeof(uint32_t));
  129. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  130. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  131. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  132. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  133. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  134. bytesl = _mm_or_si128(bytesl, bit_mask);
  135. bytesh = _mm_or_si128(bytesh, bit_mask);
  136. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  137. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  138. return MM256_SET_M128I(bytesh, bytesl);
  139. }
  140. // Unpack 32 4-bit fields into 32 bytes
  141. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  142. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  143. {
  144. // Load 16 bytes from memory
  145. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  146. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  147. const __m128i lowMask = _mm_set1_epi8(0xF);
  148. tmpl = _mm_and_si128(lowMask, tmpl);
  149. tmph = _mm_and_si128(lowMask, tmph);
  150. return MM256_SET_M128I(tmph, tmpl);
  151. }
  152. // add int16_t pairwise and return as float vector
  153. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  154. const __m128i ones = _mm_set1_epi16(1);
  155. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  156. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  157. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  158. return _mm256_cvtepi32_ps(summed_pairs);
  159. }
  160. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  161. const __m128i axl = _mm256_castsi256_si128(ax);
  162. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  163. const __m128i syl = _mm256_castsi256_si128(sy);
  164. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  165. // Perform multiplication and create 16-bit values
  166. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  167. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  168. return sum_i16_pairs_float(doth, dotl);
  169. }
  170. // multiply int8_t, add results pairwise twice and return as float vector
  171. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  172. const __m128i xl = _mm256_castsi256_si128(x);
  173. const __m128i xh = _mm256_extractf128_si256(x, 1);
  174. const __m128i yl = _mm256_castsi256_si128(y);
  175. const __m128i yh = _mm256_extractf128_si256(y, 1);
  176. // Get absolute values of x vectors
  177. const __m128i axl = _mm_sign_epi8(xl, xl);
  178. const __m128i axh = _mm_sign_epi8(xh, xh);
  179. // Sign the values of the y vectors
  180. const __m128i syl = _mm_sign_epi8(yl, xl);
  181. const __m128i syh = _mm_sign_epi8(yh, xh);
  182. // Perform multiplication and create 16-bit values
  183. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  184. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  185. return sum_i16_pairs_float(doth, dotl);
  186. }
  187. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  188. {
  189. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  190. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  191. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  192. __m128i low = _mm_and_si128( lowByte, bytes1 );
  193. high = _mm_srli_epi16( high, 4 );
  194. bytes1 = _mm_or_si128( low, high );
  195. high = _mm_andnot_si128( lowByte, bytes2 );
  196. low = _mm_and_si128( lowByte, bytes2 );
  197. high = _mm_srli_epi16( high, 4 );
  198. bytes2 = _mm_or_si128( low, high );
  199. return _mm_packus_epi16( bytes1, bytes2);
  200. }
  201. #endif
  202. #elif defined(__SSSE3__)
  203. // horizontally add 4x4 floats
  204. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  205. __m128 res_0 =_mm_hadd_ps(a, b);
  206. __m128 res_1 =_mm_hadd_ps(c, d);
  207. __m128 res =_mm_hadd_ps(res_0, res_1);
  208. res =_mm_hadd_ps(res, res);
  209. res =_mm_hadd_ps(res, res);
  210. return _mm_cvtss_f32(res);
  211. }
  212. #endif // __AVX__ || __AVX2__ || __AVX512F__
  213. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  214. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  215. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  216. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  217. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  218. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  219. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  220. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  221. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  222. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  223. // precomputed tables for expanding 8bits to 8 bytes:
  224. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  225. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  226. #endif
  227. // reference implementation for deterministic creation of model files
  228. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int64_t k) {
  229. static const int qk = QK4_0;
  230. assert(k % qk == 0);
  231. const int nb = k / qk;
  232. for (int i = 0; i < nb; i++) {
  233. float amax = 0.0f; // absolute max
  234. float max = 0.0f;
  235. for (int j = 0; j < qk; j++) {
  236. const float v = x[i*qk + j];
  237. if (amax < fabsf(v)) {
  238. amax = fabsf(v);
  239. max = v;
  240. }
  241. }
  242. const float d = max / -8;
  243. const float id = d ? 1.0f/d : 0.0f;
  244. y[i].d = GGML_FP32_TO_FP16(d);
  245. for (int j = 0; j < qk/2; ++j) {
  246. const float x0 = x[i*qk + 0 + j]*id;
  247. const float x1 = x[i*qk + qk/2 + j]*id;
  248. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  249. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  250. y[i].qs[j] = xi0;
  251. y[i].qs[j] |= xi1 << 4;
  252. }
  253. }
  254. }
  255. void quantize_row_q4_0(const float * restrict x, void * restrict y, int64_t k) {
  256. quantize_row_q4_0_reference(x, y, k);
  257. }
  258. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int64_t k) {
  259. const int qk = QK4_1;
  260. assert(k % qk == 0);
  261. const int nb = k / qk;
  262. for (int i = 0; i < nb; i++) {
  263. float min = FLT_MAX;
  264. float max = -FLT_MAX;
  265. for (int j = 0; j < qk; j++) {
  266. const float v = x[i*qk + j];
  267. if (v < min) min = v;
  268. if (v > max) max = v;
  269. }
  270. const float d = (max - min) / ((1 << 4) - 1);
  271. const float id = d ? 1.0f/d : 0.0f;
  272. y[i].d = GGML_FP32_TO_FP16(d);
  273. y[i].m = GGML_FP32_TO_FP16(min);
  274. for (int j = 0; j < qk/2; ++j) {
  275. const float x0 = (x[i*qk + 0 + j] - min)*id;
  276. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  277. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  278. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  279. y[i].qs[j] = xi0;
  280. y[i].qs[j] |= xi1 << 4;
  281. }
  282. }
  283. }
  284. void quantize_row_q4_1(const float * restrict x, void * restrict y, int64_t k) {
  285. quantize_row_q4_1_reference(x, y, k);
  286. }
  287. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int64_t k) {
  288. static const int qk = QK5_0;
  289. assert(k % qk == 0);
  290. const int nb = k / qk;
  291. for (int i = 0; i < nb; i++) {
  292. float amax = 0.0f; // absolute max
  293. float max = 0.0f;
  294. for (int j = 0; j < qk; j++) {
  295. const float v = x[i*qk + j];
  296. if (amax < fabsf(v)) {
  297. amax = fabsf(v);
  298. max = v;
  299. }
  300. }
  301. const float d = max / -16;
  302. const float id = d ? 1.0f/d : 0.0f;
  303. y[i].d = GGML_FP32_TO_FP16(d);
  304. uint32_t qh = 0;
  305. for (int j = 0; j < qk/2; ++j) {
  306. const float x0 = x[i*qk + 0 + j]*id;
  307. const float x1 = x[i*qk + qk/2 + j]*id;
  308. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  309. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  310. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  311. // get the 5-th bit and store it in qh at the right position
  312. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  313. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  314. }
  315. memcpy(&y[i].qh, &qh, sizeof(qh));
  316. }
  317. }
  318. void quantize_row_q5_0(const float * restrict x, void * restrict y, int64_t k) {
  319. quantize_row_q5_0_reference(x, y, k);
  320. }
  321. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int64_t k) {
  322. const int qk = QK5_1;
  323. assert(k % qk == 0);
  324. const int nb = k / qk;
  325. for (int i = 0; i < nb; i++) {
  326. float min = FLT_MAX;
  327. float max = -FLT_MAX;
  328. for (int j = 0; j < qk; j++) {
  329. const float v = x[i*qk + j];
  330. if (v < min) min = v;
  331. if (v > max) max = v;
  332. }
  333. const float d = (max - min) / ((1 << 5) - 1);
  334. const float id = d ? 1.0f/d : 0.0f;
  335. y[i].d = GGML_FP32_TO_FP16(d);
  336. y[i].m = GGML_FP32_TO_FP16(min);
  337. uint32_t qh = 0;
  338. for (int j = 0; j < qk/2; ++j) {
  339. const float x0 = (x[i*qk + 0 + j] - min)*id;
  340. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  341. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  342. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  343. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  344. // get the 5-th bit and store it in qh at the right position
  345. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  346. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  347. }
  348. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  349. }
  350. }
  351. void quantize_row_q5_1(const float * restrict x, void * restrict y, int64_t k) {
  352. quantize_row_q5_1_reference(x, y, k);
  353. }
  354. // reference implementation for deterministic creation of model files
  355. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int64_t k) {
  356. assert(k % QK8_0 == 0);
  357. const int nb = k / QK8_0;
  358. for (int i = 0; i < nb; i++) {
  359. float amax = 0.0f; // absolute max
  360. for (int j = 0; j < QK8_0; j++) {
  361. const float v = x[i*QK8_0 + j];
  362. amax = MAX(amax, fabsf(v));
  363. }
  364. const float d = amax / ((1 << 7) - 1);
  365. const float id = d ? 1.0f/d : 0.0f;
  366. y[i].d = GGML_FP32_TO_FP16(d);
  367. for (int j = 0; j < QK8_0; ++j) {
  368. const float x0 = x[i*QK8_0 + j]*id;
  369. y[i].qs[j] = roundf(x0);
  370. }
  371. }
  372. }
  373. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int64_t k) {
  374. assert(QK8_0 == 32);
  375. assert(k % QK8_0 == 0);
  376. const int nb = k / QK8_0;
  377. block_q8_0 * restrict y = vy;
  378. #if defined(__ARM_NEON)
  379. for (int i = 0; i < nb; i++) {
  380. float32x4_t srcv [8];
  381. float32x4_t asrcv[8];
  382. float32x4_t amaxv[8];
  383. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  384. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  385. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  386. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  387. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  388. const float amax = vmaxvq_f32(amaxv[0]);
  389. const float d = amax / ((1 << 7) - 1);
  390. const float id = d ? 1.0f/d : 0.0f;
  391. y[i].d = GGML_FP32_TO_FP16(d);
  392. for (int j = 0; j < 8; j++) {
  393. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  394. const int32x4_t vi = vcvtnq_s32_f32(v);
  395. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  396. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  397. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  398. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  399. }
  400. }
  401. #elif defined(__wasm_simd128__)
  402. for (int i = 0; i < nb; i++) {
  403. v128_t srcv [8];
  404. v128_t asrcv[8];
  405. v128_t amaxv[8];
  406. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  407. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  408. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  409. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  410. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  411. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  412. wasm_f32x4_extract_lane(amaxv[0], 1)),
  413. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  414. wasm_f32x4_extract_lane(amaxv[0], 3)));
  415. const float d = amax / ((1 << 7) - 1);
  416. const float id = d ? 1.0f/d : 0.0f;
  417. y[i].d = GGML_FP32_TO_FP16(d);
  418. for (int j = 0; j < 8; j++) {
  419. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  420. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  421. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  422. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  423. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  424. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  425. }
  426. }
  427. #elif defined(__AVX2__) || defined(__AVX__)
  428. for (int i = 0; i < nb; i++) {
  429. // Load elements into 4 AVX vectors
  430. __m256 v0 = _mm256_loadu_ps( x );
  431. __m256 v1 = _mm256_loadu_ps( x + 8 );
  432. __m256 v2 = _mm256_loadu_ps( x + 16 );
  433. __m256 v3 = _mm256_loadu_ps( x + 24 );
  434. x += 32;
  435. // Compute max(abs(e)) for the block
  436. const __m256 signBit = _mm256_set1_ps( -0.0f );
  437. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  438. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  439. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  440. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  441. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  442. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  443. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  444. const float maxScalar = _mm_cvtss_f32( max4 );
  445. // Quantize these floats
  446. const float d = maxScalar / 127.f;
  447. y[i].d = GGML_FP32_TO_FP16(d);
  448. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  449. const __m256 mul = _mm256_set1_ps( id );
  450. // Apply the multiplier
  451. v0 = _mm256_mul_ps( v0, mul );
  452. v1 = _mm256_mul_ps( v1, mul );
  453. v2 = _mm256_mul_ps( v2, mul );
  454. v3 = _mm256_mul_ps( v3, mul );
  455. // Round to nearest integer
  456. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  457. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  458. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  459. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  460. // Convert floats to integers
  461. __m256i i0 = _mm256_cvtps_epi32( v0 );
  462. __m256i i1 = _mm256_cvtps_epi32( v1 );
  463. __m256i i2 = _mm256_cvtps_epi32( v2 );
  464. __m256i i3 = _mm256_cvtps_epi32( v3 );
  465. #if defined(__AVX2__)
  466. // Convert int32 to int16
  467. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  468. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  469. // Convert int16 to int8
  470. i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  471. // We got our precious signed bytes, but the order is now wrong
  472. // These AVX2 pack instructions process 16-byte pieces independently
  473. // The following instruction is fixing the order
  474. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  475. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  476. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  477. #else
  478. // Since we don't have in AVX some necessary functions,
  479. // we split the registers in half and call AVX2 analogs from SSE
  480. __m128i ni0 = _mm256_castsi256_si128( i0 );
  481. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  482. __m128i ni2 = _mm256_castsi256_si128( i1 );
  483. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  484. __m128i ni4 = _mm256_castsi256_si128( i2 );
  485. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  486. __m128i ni6 = _mm256_castsi256_si128( i3 );
  487. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  488. // Convert int32 to int16
  489. ni0 = _mm_packs_epi32( ni0, ni1 );
  490. ni2 = _mm_packs_epi32( ni2, ni3 );
  491. ni4 = _mm_packs_epi32( ni4, ni5 );
  492. ni6 = _mm_packs_epi32( ni6, ni7 );
  493. // Convert int16 to int8
  494. ni0 = _mm_packs_epi16( ni0, ni2 );
  495. ni4 = _mm_packs_epi16( ni4, ni6 );
  496. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  497. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  498. #endif
  499. }
  500. #elif defined(__riscv_v_intrinsic)
  501. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  502. for (int i = 0; i < nb; i++) {
  503. // load elements
  504. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  505. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  506. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  507. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  508. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  509. const float d = amax / ((1 << 7) - 1);
  510. const float id = d ? 1.0f/d : 0.0f;
  511. y[i].d = GGML_FP32_TO_FP16(d);
  512. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  513. // convert to integer
  514. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  515. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  516. // store result
  517. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  518. }
  519. #else
  520. GGML_UNUSED(nb);
  521. // scalar
  522. quantize_row_q8_0_reference(x, y, k);
  523. #endif
  524. }
  525. // reference implementation for deterministic creation of model files
  526. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int64_t k) {
  527. assert(QK8_1 == 32);
  528. assert(k % QK8_1 == 0);
  529. const int nb = k / QK8_1;
  530. for (int i = 0; i < nb; i++) {
  531. float amax = 0.0f; // absolute max
  532. for (int j = 0; j < QK8_1; j++) {
  533. const float v = x[i*QK8_1 + j];
  534. amax = MAX(amax, fabsf(v));
  535. }
  536. const float d = amax / ((1 << 7) - 1);
  537. const float id = d ? 1.0f/d : 0.0f;
  538. y[i].d = GGML_FP32_TO_FP16(d);
  539. int sum = 0;
  540. for (int j = 0; j < QK8_1/2; ++j) {
  541. const float v0 = x[i*QK8_1 + j]*id;
  542. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  543. y[i].qs[ j] = roundf(v0);
  544. y[i].qs[QK8_1/2 + j] = roundf(v1);
  545. sum += y[i].qs[ j];
  546. sum += y[i].qs[QK8_1/2 + j];
  547. }
  548. y[i].s = GGML_FP32_TO_FP16(sum*d);
  549. }
  550. }
  551. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int64_t k) {
  552. assert(k % QK8_1 == 0);
  553. const int nb = k / QK8_1;
  554. block_q8_1 * restrict y = vy;
  555. #if defined(__ARM_NEON)
  556. for (int i = 0; i < nb; i++) {
  557. float32x4_t srcv [8];
  558. float32x4_t asrcv[8];
  559. float32x4_t amaxv[8];
  560. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  561. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  562. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  563. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  564. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  565. const float amax = vmaxvq_f32(amaxv[0]);
  566. const float d = amax / ((1 << 7) - 1);
  567. const float id = d ? 1.0f/d : 0.0f;
  568. y[i].d = GGML_FP32_TO_FP16(d);
  569. int32x4_t accv = vdupq_n_s32(0);
  570. for (int j = 0; j < 8; j++) {
  571. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  572. const int32x4_t vi = vcvtnq_s32_f32(v);
  573. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  574. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  575. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  576. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  577. accv = vaddq_s32(accv, vi);
  578. }
  579. y[i].s = GGML_FP32_TO_FP16(d * vaddvq_s32(accv));
  580. }
  581. #elif defined(__wasm_simd128__)
  582. for (int i = 0; i < nb; i++) {
  583. v128_t srcv [8];
  584. v128_t asrcv[8];
  585. v128_t amaxv[8];
  586. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  587. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  588. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  589. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  590. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  591. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  592. wasm_f32x4_extract_lane(amaxv[0], 1)),
  593. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  594. wasm_f32x4_extract_lane(amaxv[0], 3)));
  595. const float d = amax / ((1 << 7) - 1);
  596. const float id = d ? 1.0f/d : 0.0f;
  597. y[i].d = GGML_FP32_TO_FP16(d);
  598. v128_t accv = wasm_i32x4_splat(0);
  599. for (int j = 0; j < 8; j++) {
  600. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  601. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  602. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  603. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  604. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  605. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  606. accv = wasm_i32x4_add(accv, vi);
  607. }
  608. y[i].s = GGML_FP32_TO_FP16(
  609. d * (wasm_i32x4_extract_lane(accv, 0) +
  610. wasm_i32x4_extract_lane(accv, 1) +
  611. wasm_i32x4_extract_lane(accv, 2) +
  612. wasm_i32x4_extract_lane(accv, 3)));
  613. }
  614. #elif defined(__AVX2__) || defined(__AVX__)
  615. for (int i = 0; i < nb; i++) {
  616. // Load elements into 4 AVX vectors
  617. __m256 v0 = _mm256_loadu_ps( x );
  618. __m256 v1 = _mm256_loadu_ps( x + 8 );
  619. __m256 v2 = _mm256_loadu_ps( x + 16 );
  620. __m256 v3 = _mm256_loadu_ps( x + 24 );
  621. x += 32;
  622. // Compute max(abs(e)) for the block
  623. const __m256 signBit = _mm256_set1_ps( -0.0f );
  624. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  625. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  626. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  627. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  628. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  629. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  630. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  631. const float maxScalar = _mm_cvtss_f32( max4 );
  632. // Quantize these floats
  633. const float d = maxScalar / 127.f;
  634. y[i].d = GGML_FP32_TO_FP16(d);
  635. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  636. const __m256 mul = _mm256_set1_ps( id );
  637. // Apply the multiplier
  638. v0 = _mm256_mul_ps( v0, mul );
  639. v1 = _mm256_mul_ps( v1, mul );
  640. v2 = _mm256_mul_ps( v2, mul );
  641. v3 = _mm256_mul_ps( v3, mul );
  642. // Round to nearest integer
  643. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  644. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  645. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  646. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  647. // Convert floats to integers
  648. __m256i i0 = _mm256_cvtps_epi32( v0 );
  649. __m256i i1 = _mm256_cvtps_epi32( v1 );
  650. __m256i i2 = _mm256_cvtps_epi32( v2 );
  651. __m256i i3 = _mm256_cvtps_epi32( v3 );
  652. #if defined(__AVX2__)
  653. // Compute the sum of the quants and set y[i].s
  654. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3))));
  655. // Convert int32 to int16
  656. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  657. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  658. // Convert int16 to int8
  659. i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  660. // We got our precious signed bytes, but the order is now wrong
  661. // These AVX2 pack instructions process 16-byte pieces independently
  662. // The following instruction is fixing the order
  663. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  664. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  665. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  666. #else
  667. // Since we don't have in AVX some necessary functions,
  668. // we split the registers in half and call AVX2 analogs from SSE
  669. __m128i ni0 = _mm256_castsi256_si128( i0 );
  670. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  671. __m128i ni2 = _mm256_castsi256_si128( i1 );
  672. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  673. __m128i ni4 = _mm256_castsi256_si128( i2 );
  674. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  675. __m128i ni6 = _mm256_castsi256_si128( i3 );
  676. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  677. // Compute the sum of the quants and set y[i].s
  678. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  679. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  680. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_4(_mm_add_epi32(s0, s1)));
  681. // Convert int32 to int16
  682. ni0 = _mm_packs_epi32( ni0, ni1 );
  683. ni2 = _mm_packs_epi32( ni2, ni3 );
  684. ni4 = _mm_packs_epi32( ni4, ni5 );
  685. ni6 = _mm_packs_epi32( ni6, ni7 );
  686. // Convert int16 to int8
  687. ni0 = _mm_packs_epi16( ni0, ni2 );
  688. ni4 = _mm_packs_epi16( ni4, ni6 );
  689. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  690. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  691. #endif
  692. }
  693. #elif defined(__riscv_v_intrinsic)
  694. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  695. for (int i = 0; i < nb; i++) {
  696. // load elements
  697. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  698. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  699. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  700. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  701. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  702. const float d = amax / ((1 << 7) - 1);
  703. const float id = d ? 1.0f/d : 0.0f;
  704. y[i].d = GGML_FP32_TO_FP16(d);
  705. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  706. // convert to integer
  707. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  708. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  709. // store result
  710. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  711. // compute sum for y[i].s
  712. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  713. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  714. // set y[i].s
  715. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  716. y[i].s = GGML_FP32_TO_FP16(sum*d);
  717. }
  718. #else
  719. GGML_UNUSED(nb);
  720. // scalar
  721. quantize_row_q8_1_reference(x, y, k);
  722. #endif
  723. }
  724. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int64_t k) {
  725. static const int qk = QK4_0;
  726. assert(k % qk == 0);
  727. const int nb = k / qk;
  728. for (int i = 0; i < nb; i++) {
  729. const float d = GGML_FP16_TO_FP32(x[i].d);
  730. for (int j = 0; j < qk/2; ++j) {
  731. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  732. const int x1 = (x[i].qs[j] >> 4) - 8;
  733. y[i*qk + j + 0 ] = x0*d;
  734. y[i*qk + j + qk/2] = x1*d;
  735. }
  736. }
  737. }
  738. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int64_t k) {
  739. static const int qk = QK4_1;
  740. assert(k % qk == 0);
  741. const int nb = k / qk;
  742. for (int i = 0; i < nb; i++) {
  743. const float d = GGML_FP16_TO_FP32(x[i].d);
  744. const float m = GGML_FP16_TO_FP32(x[i].m);
  745. for (int j = 0; j < qk/2; ++j) {
  746. const int x0 = (x[i].qs[j] & 0x0F);
  747. const int x1 = (x[i].qs[j] >> 4);
  748. y[i*qk + j + 0 ] = x0*d + m;
  749. y[i*qk + j + qk/2] = x1*d + m;
  750. }
  751. }
  752. }
  753. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int64_t k) {
  754. static const int qk = QK5_0;
  755. assert(k % qk == 0);
  756. const int nb = k / qk;
  757. for (int i = 0; i < nb; i++) {
  758. const float d = GGML_FP16_TO_FP32(x[i].d);
  759. uint32_t qh;
  760. memcpy(&qh, x[i].qh, sizeof(qh));
  761. for (int j = 0; j < qk/2; ++j) {
  762. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  763. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  764. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  765. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  766. y[i*qk + j + 0 ] = x0*d;
  767. y[i*qk + j + qk/2] = x1*d;
  768. }
  769. }
  770. }
  771. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int64_t k) {
  772. static const int qk = QK5_1;
  773. assert(k % qk == 0);
  774. const int nb = k / qk;
  775. for (int i = 0; i < nb; i++) {
  776. const float d = GGML_FP16_TO_FP32(x[i].d);
  777. const float m = GGML_FP16_TO_FP32(x[i].m);
  778. uint32_t qh;
  779. memcpy(&qh, x[i].qh, sizeof(qh));
  780. for (int j = 0; j < qk/2; ++j) {
  781. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  782. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  783. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  784. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  785. y[i*qk + j + 0 ] = x0*d + m;
  786. y[i*qk + j + qk/2] = x1*d + m;
  787. }
  788. }
  789. }
  790. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int64_t k) {
  791. static const int qk = QK8_0;
  792. assert(k % qk == 0);
  793. const int nb = k / qk;
  794. for (int i = 0; i < nb; i++) {
  795. const float d = GGML_FP16_TO_FP32(x[i].d);
  796. for (int j = 0; j < qk; ++j) {
  797. y[i*qk + j] = x[i].qs[j]*d;
  798. }
  799. }
  800. }
  801. //
  802. // 2-6 bit quantization in super-blocks
  803. //
  804. //
  805. // ===================== Helper functions
  806. //
  807. static inline int nearest_int(float fval) {
  808. assert(fval <= 4194303.f);
  809. float val = fval + 12582912.f;
  810. int i; memcpy(&i, &val, sizeof(int));
  811. return (i & 0x007fffff) - 0x00400000;
  812. }
  813. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  814. const float * restrict qw) {
  815. float max = 0;
  816. float amax = 0;
  817. for (int i = 0; i < n; ++i) {
  818. float ax = fabsf(x[i]);
  819. if (ax > amax) { amax = ax; max = x[i]; }
  820. }
  821. if (amax < 1e-30f) { // all zero
  822. for (int i = 0; i < n; ++i) {
  823. L[i] = 0;
  824. }
  825. return 0.f;
  826. }
  827. float iscale = -nmax / max;
  828. if (rmse_type == 0) {
  829. for (int i = 0; i < n; ++i) {
  830. int l = nearest_int(iscale * x[i]);
  831. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  832. }
  833. return 1/iscale;
  834. }
  835. bool return_early = false;
  836. if (rmse_type < 0) {
  837. rmse_type = -rmse_type;
  838. return_early = true;
  839. }
  840. float sumlx = 0;
  841. float suml2 = 0;
  842. #ifdef HAVE_BUGGY_APPLE_LINKER
  843. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  844. for (volatile int i = 0; i < n; ++i) {
  845. #else
  846. for (int i = 0; i < n; ++i) {
  847. #endif
  848. int l = nearest_int(iscale * x[i]);
  849. l = MAX(-nmax, MIN(nmax-1, l));
  850. L[i] = l + nmax;
  851. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  852. sumlx += w*x[i]*l;
  853. suml2 += w*l*l;
  854. }
  855. float scale = sumlx/suml2;
  856. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  857. float best = scale * sumlx;
  858. for (int is = -9; is <= 9; ++is) {
  859. if (is == 0) {
  860. continue;
  861. }
  862. iscale = -(nmax + 0.1f*is) / max;
  863. sumlx = suml2 = 0;
  864. for (int i = 0; i < n; ++i) {
  865. int l = nearest_int(iscale * x[i]);
  866. l = MAX(-nmax, MIN(nmax-1, l));
  867. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  868. sumlx += w*x[i]*l;
  869. suml2 += w*l*l;
  870. }
  871. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  872. for (int i = 0; i < n; ++i) {
  873. int l = nearest_int(iscale * x[i]);
  874. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  875. }
  876. scale = sumlx/suml2; best = scale*sumlx;
  877. }
  878. }
  879. return scale;
  880. }
  881. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  882. float max = 0;
  883. float amax = 0;
  884. for (int i = 0; i < n; ++i) {
  885. float ax = fabsf(x[i]);
  886. if (ax > amax) { amax = ax; max = x[i]; }
  887. }
  888. if (!amax) { // all zero
  889. for (int i = 0; i < n; ++i) { L[i] = 0; }
  890. return 0.f;
  891. }
  892. float iscale = -nmax / max;
  893. if (do_rmse) {
  894. float sumlx = 0;
  895. float suml2 = 0;
  896. for (int i = 0; i < n; ++i) {
  897. int l = nearest_int(iscale * x[i]);
  898. l = MAX(-nmax, MIN(nmax-1, l));
  899. L[i] = l;
  900. float w = x[i]*x[i];
  901. sumlx += w*x[i]*l;
  902. suml2 += w*l*l;
  903. }
  904. for (int itry = 0; itry < 5; ++itry) {
  905. int n_changed = 0;
  906. for (int i = 0; i < n; ++i) {
  907. float w = x[i]*x[i];
  908. float slx = sumlx - w*x[i]*L[i];
  909. if (slx > 0) {
  910. float sl2 = suml2 - w*L[i]*L[i];
  911. int new_l = nearest_int(x[i] * sl2 / slx);
  912. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  913. if (new_l != L[i]) {
  914. slx += w*x[i]*new_l;
  915. sl2 += w*new_l*new_l;
  916. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  917. L[i] = new_l; sumlx = slx; suml2 = sl2;
  918. ++n_changed;
  919. }
  920. }
  921. }
  922. }
  923. if (!n_changed) {
  924. break;
  925. }
  926. }
  927. for (int i = 0; i < n; ++i) {
  928. L[i] += nmax;
  929. }
  930. return sumlx / suml2;
  931. }
  932. for (int i = 0; i < n; ++i) {
  933. int l = nearest_int(iscale * x[i]);
  934. l = MAX(-nmax, MIN(nmax-1, l));
  935. L[i] = l + nmax;
  936. }
  937. return 1/iscale;
  938. }
  939. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  940. int ntry, float alpha) {
  941. float min = x[0];
  942. float max = x[0];
  943. for (int i = 1; i < n; ++i) {
  944. if (x[i] < min) min = x[i];
  945. if (x[i] > max) max = x[i];
  946. }
  947. if (max == min) {
  948. for (int i = 0; i < n; ++i) L[i] = 0;
  949. *the_min = 0;
  950. return 0.f;
  951. }
  952. if (min > 0) min = 0;
  953. float iscale = nmax/(max - min);
  954. float scale = 1/iscale;
  955. for (int itry = 0; itry < ntry; ++itry) {
  956. float sumlx = 0; int suml2 = 0;
  957. bool did_change = false;
  958. for (int i = 0; i < n; ++i) {
  959. int l = nearest_int(iscale*(x[i] - min));
  960. l = MAX(0, MIN(nmax, l));
  961. if (l != L[i]) {
  962. L[i] = l;
  963. did_change = true;
  964. }
  965. sumlx += (x[i] - min)*l;
  966. suml2 += l*l;
  967. }
  968. scale = sumlx/suml2;
  969. float sum = 0;
  970. for (int i = 0; i < n; ++i) {
  971. sum += x[i] - scale*L[i];
  972. }
  973. min = alpha*min + (1 - alpha)*sum/n;
  974. if (min > 0) min = 0;
  975. iscale = 1/scale;
  976. if (!did_change) break;
  977. }
  978. *the_min = -min;
  979. return scale;
  980. }
  981. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  982. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  983. float rmin, float rdelta, int nstep, bool use_mad) {
  984. float min = x[0];
  985. float max = x[0];
  986. float sum_w = weights[0];
  987. float sum_x = sum_w * x[0];
  988. #ifdef HAVE_BUGGY_APPLE_LINKER
  989. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  990. for (volatile int i = 1; i < n; ++i) {
  991. #else
  992. for (int i = 1; i < n; ++i) {
  993. #endif
  994. if (x[i] < min) min = x[i];
  995. if (x[i] > max) max = x[i];
  996. float w = weights[i];
  997. sum_w += w;
  998. sum_x += w * x[i];
  999. }
  1000. if (min > 0) min = 0;
  1001. if (max == min) {
  1002. for (int i = 0; i < n; ++i) L[i] = 0;
  1003. *the_min = -min;
  1004. return 0.f;
  1005. }
  1006. float iscale = nmax/(max - min);
  1007. float scale = 1/iscale;
  1008. float best_mad = 0;
  1009. for (int i = 0; i < n; ++i) {
  1010. int l = nearest_int(iscale*(x[i] - min));
  1011. L[i] = MAX(0, MIN(nmax, l));
  1012. float diff = scale * L[i] + min - x[i];
  1013. diff = use_mad ? fabsf(diff) : diff * diff;
  1014. float w = weights[i];
  1015. best_mad += w * diff;
  1016. }
  1017. if (nstep < 1) {
  1018. *the_min = -min;
  1019. return scale;
  1020. }
  1021. for (int is = 0; is <= nstep; ++is) {
  1022. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1023. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1024. for (int i = 0; i < n; ++i) {
  1025. int l = nearest_int(iscale*(x[i] - min));
  1026. l = MAX(0, MIN(nmax, l));
  1027. Laux[i] = l;
  1028. float w = weights[i];
  1029. sum_l += w*l;
  1030. sum_l2 += w*l*l;
  1031. sum_xl += w*l*x[i];
  1032. }
  1033. float D = sum_w * sum_l2 - sum_l * sum_l;
  1034. if (D > 0) {
  1035. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1036. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1037. if (this_min > 0) {
  1038. this_min = 0;
  1039. this_scale = sum_xl / sum_l2;
  1040. }
  1041. float mad = 0;
  1042. for (int i = 0; i < n; ++i) {
  1043. float diff = this_scale * Laux[i] + this_min - x[i];
  1044. diff = use_mad ? fabsf(diff) : diff * diff;
  1045. float w = weights[i];
  1046. mad += w * diff;
  1047. }
  1048. if (mad < best_mad) {
  1049. for (int i = 0; i < n; ++i) {
  1050. L[i] = Laux[i];
  1051. }
  1052. best_mad = mad;
  1053. scale = this_scale;
  1054. min = this_min;
  1055. }
  1056. }
  1057. }
  1058. *the_min = -min;
  1059. return scale;
  1060. }
  1061. #if QK_K == 256
  1062. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1063. if (j < 4) {
  1064. *d = q[j] & 63; *m = q[j + 4] & 63;
  1065. } else {
  1066. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1067. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1068. }
  1069. }
  1070. #endif
  1071. //========================- 2-bit (de)-quantization
  1072. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int64_t k) {
  1073. assert(k % QK_K == 0);
  1074. const int nb = k / QK_K;
  1075. uint8_t L[QK_K];
  1076. uint8_t Laux[16];
  1077. float weights[16];
  1078. float mins[QK_K/16];
  1079. float scales[QK_K/16];
  1080. const float q4scale = 15.f;
  1081. for (int i = 0; i < nb; i++) {
  1082. float max_scale = 0; // as we are deducting the min, scales are always positive
  1083. float max_min = 0;
  1084. for (int j = 0; j < QK_K/16; ++j) {
  1085. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1086. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1087. float scale = scales[j];
  1088. if (scale > max_scale) {
  1089. max_scale = scale;
  1090. }
  1091. float min = mins[j];
  1092. if (min > max_min) {
  1093. max_min = min;
  1094. }
  1095. }
  1096. if (max_scale > 0) {
  1097. float iscale = q4scale/max_scale;
  1098. for (int j = 0; j < QK_K/16; ++j) {
  1099. int l = nearest_int(iscale*scales[j]);
  1100. y[i].scales[j] = l;
  1101. }
  1102. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1103. } else {
  1104. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1105. y[i].d = GGML_FP32_TO_FP16(0.f);
  1106. }
  1107. if (max_min > 0) {
  1108. float iscale = q4scale/max_min;
  1109. for (int j = 0; j < QK_K/16; ++j) {
  1110. int l = nearest_int(iscale*mins[j]);
  1111. y[i].scales[j] |= (l << 4);
  1112. }
  1113. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1114. } else {
  1115. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1116. }
  1117. for (int j = 0; j < QK_K/16; ++j) {
  1118. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1119. if (!d) continue;
  1120. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1121. for (int ii = 0; ii < 16; ++ii) {
  1122. int l = nearest_int((x[16*j + ii] + dm)/d);
  1123. l = MAX(0, MIN(3, l));
  1124. L[16*j + ii] = l;
  1125. }
  1126. }
  1127. #if QK_K == 256
  1128. for (int j = 0; j < QK_K; j += 128) {
  1129. for (int l = 0; l < 32; ++l) {
  1130. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1131. }
  1132. }
  1133. #else
  1134. for (int l = 0; l < 16; ++l) {
  1135. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1136. }
  1137. #endif
  1138. x += QK_K;
  1139. }
  1140. }
  1141. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int64_t k) {
  1142. assert(k % QK_K == 0);
  1143. const int nb = k / QK_K;
  1144. for (int i = 0; i < nb; i++) {
  1145. const float d = GGML_FP16_TO_FP32(x[i].d);
  1146. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1147. const uint8_t * q = x[i].qs;
  1148. #if QK_K == 256
  1149. int is = 0;
  1150. float dl, ml;
  1151. for (int n = 0; n < QK_K; n += 128) {
  1152. int shift = 0;
  1153. for (int j = 0; j < 4; ++j) {
  1154. uint8_t sc = x[i].scales[is++];
  1155. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1156. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1157. sc = x[i].scales[is++];
  1158. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1159. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1160. shift += 2;
  1161. }
  1162. q += 32;
  1163. }
  1164. #else
  1165. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1166. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1167. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1168. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1169. for (int l = 0; l < 16; ++l) {
  1170. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1171. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1172. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1173. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1174. }
  1175. y += QK_K;
  1176. #endif
  1177. }
  1178. }
  1179. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int64_t k) {
  1180. quantize_row_q2_K_reference(x, vy, k);
  1181. }
  1182. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1183. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1184. float rmin, float rdelta, int nstep, bool use_mad) {
  1185. float min = x[0];
  1186. float max = x[0];
  1187. float sum_w = weights ? weights[0] : x[0]*x[0];
  1188. float sum_x = sum_w * x[0];
  1189. #ifdef HAVE_BUGGY_APPLE_LINKER
  1190. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1191. for (volatile int i = 1; i < n; ++i) {
  1192. #else
  1193. for (int i = 1; i < n; ++i) {
  1194. #endif
  1195. if (x[i] < min) min = x[i];
  1196. if (x[i] > max) max = x[i];
  1197. float w = weights ? weights[i] : x[i]*x[i];
  1198. sum_w += w;
  1199. sum_x += w * x[i];
  1200. }
  1201. if (min > 0) {
  1202. min = 0;
  1203. }
  1204. if (max <= min) {
  1205. memset(L, 0, n);
  1206. *the_min = -min;
  1207. return 0.f;
  1208. }
  1209. float iscale = nmax/(max - min);
  1210. float scale = 1/iscale;
  1211. float best_mad = 0;
  1212. for (int i = 0; i < n; ++i) {
  1213. int l = nearest_int(iscale*(x[i] - min));
  1214. L[i] = MAX(0, MIN(nmax, l));
  1215. float diff = scale * L[i] + min - x[i];
  1216. diff = use_mad ? fabsf(diff) : diff*diff;
  1217. float w = weights ? weights[i] : x[i]*x[i];
  1218. best_mad += w * diff;
  1219. }
  1220. if (nstep < 1) {
  1221. *the_min = -min;
  1222. return scale;
  1223. }
  1224. for (int is = 0; is <= nstep; ++is) {
  1225. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1226. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1227. for (int i = 0; i < n; ++i) {
  1228. int l = nearest_int(iscale*(x[i] - min));
  1229. l = MAX(0, MIN(nmax, l));
  1230. Laux[i] = l;
  1231. float w = weights ? weights[i] : x[i]*x[i];
  1232. sum_l += w*l;
  1233. sum_l2 += w*l*l;
  1234. sum_xl += w*l*x[i];
  1235. }
  1236. float D = sum_w * sum_l2 - sum_l * sum_l;
  1237. if (D > 0) {
  1238. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1239. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1240. if (this_min > 0) {
  1241. this_min = 0;
  1242. this_scale = sum_xl / sum_l2;
  1243. }
  1244. float mad = 0;
  1245. for (int i = 0; i < n; ++i) {
  1246. float diff = this_scale * Laux[i] + this_min - x[i];
  1247. diff = use_mad ? fabsf(diff) : diff*diff;
  1248. float w = weights ? weights[i] : x[i]*x[i];
  1249. mad += w * diff;
  1250. }
  1251. if (mad < best_mad) {
  1252. for (int i = 0; i < n; ++i) {
  1253. L[i] = Laux[i];
  1254. }
  1255. best_mad = mad;
  1256. scale = this_scale;
  1257. min = this_min;
  1258. }
  1259. }
  1260. }
  1261. *the_min = -min;
  1262. return scale;
  1263. }
  1264. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1265. float max = 0;
  1266. for (int i = 0; i < n; ++i) {
  1267. max = MAX(max, x[i]);
  1268. }
  1269. if (!max) { // all zero
  1270. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1271. return 0.f;
  1272. }
  1273. float iscale = nmax / max;
  1274. for (int i = 0; i < n; ++i) {
  1275. L[i] = nearest_int(iscale * x[i]);
  1276. }
  1277. float scale = 1/iscale;
  1278. float best_mse = 0;
  1279. for (int i = 0; i < n; ++i) {
  1280. float diff = x[i] - scale*L[i];
  1281. float w = quant_weights[i];
  1282. best_mse += w*diff*diff;
  1283. }
  1284. for (int is = -4; is <= 4; ++is) {
  1285. if (is == 0) continue;
  1286. float iscale_is = (0.1f*is + nmax)/max;
  1287. float scale_is = 1/iscale_is;
  1288. float mse = 0;
  1289. for (int i = 0; i < n; ++i) {
  1290. int l = nearest_int(iscale_is*x[i]);
  1291. l = MIN(nmax, l);
  1292. float diff = x[i] - scale_is*l;
  1293. float w = quant_weights[i];
  1294. mse += w*diff*diff;
  1295. }
  1296. if (mse < best_mse) {
  1297. best_mse = mse;
  1298. iscale = iscale_is;
  1299. }
  1300. }
  1301. float sumlx = 0;
  1302. float suml2 = 0;
  1303. for (int i = 0; i < n; ++i) {
  1304. int l = nearest_int(iscale * x[i]);
  1305. l = MIN(nmax, l);
  1306. L[i] = l;
  1307. float w = quant_weights[i];
  1308. sumlx += w*x[i]*l;
  1309. suml2 += w*l*l;
  1310. }
  1311. for (int itry = 0; itry < 5; ++itry) {
  1312. int n_changed = 0;
  1313. for (int i = 0; i < n; ++i) {
  1314. float w = quant_weights[i];
  1315. float slx = sumlx - w*x[i]*L[i];
  1316. float sl2 = suml2 - w*L[i]*L[i];
  1317. if (slx > 0 && sl2 > 0) {
  1318. int new_l = nearest_int(x[i] * sl2 / slx);
  1319. new_l = MIN(nmax, new_l);
  1320. if (new_l != L[i]) {
  1321. slx += w*x[i]*new_l;
  1322. sl2 += w*new_l*new_l;
  1323. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1324. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1325. ++n_changed;
  1326. }
  1327. }
  1328. }
  1329. }
  1330. if (!n_changed) {
  1331. break;
  1332. }
  1333. }
  1334. return sumlx / suml2;
  1335. }
  1336. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1337. GGML_ASSERT(quant_weights);
  1338. assert(k % QK_K == 0);
  1339. const int nb = k / QK_K;
  1340. const bool requantize = true;
  1341. uint8_t L[QK_K];
  1342. uint8_t Laux[16];
  1343. float mins[QK_K/16];
  1344. float scales[QK_K/16];
  1345. float sw[QK_K/16];
  1346. float weight[16];
  1347. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1348. for (int i = 0; i < nb; i++) {
  1349. memset(sw, 0, QK_K/16*sizeof(float));
  1350. float sumx2 = 0;
  1351. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1352. float sigma2 = sumx2/QK_K;
  1353. for (int j = 0; j < QK_K/16; ++j) {
  1354. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1355. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1356. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1357. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1358. }
  1359. float dm, mm;
  1360. #if QK_K == 64
  1361. float max_scale = 0, max_min = 0;
  1362. for (int j = 0; j < QK_K/16; ++j) {
  1363. max_scale = MAX(max_scale, scales[j]);
  1364. max_min = MAX(max_min, mins[j]);
  1365. }
  1366. dm = max_scale/15;
  1367. mm = max_min/15;
  1368. if (max_scale) {
  1369. float id = 1/dm;
  1370. for (int j = 0; j < QK_K/16; ++j) {
  1371. int l = nearest_int(id*scales[j]);
  1372. Ls[j] = MAX(0, MIN(15, l));
  1373. }
  1374. } else {
  1375. memset(Ls, 0, QK_K/16);
  1376. }
  1377. if (max_min) {
  1378. float id = 1/mm;
  1379. for (int j = 0; j < QK_K/16; ++j) {
  1380. int l = nearest_int(id*mins[j]);
  1381. Lm[j] = MAX(0, MIN(15, l));
  1382. }
  1383. } else {
  1384. memset(Lm, 0, QK_K/16);
  1385. }
  1386. #else
  1387. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1388. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1389. #endif
  1390. y[i].d = GGML_FP32_TO_FP16(dm);
  1391. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1392. dm = GGML_FP16_TO_FP32(y[i].d);
  1393. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1394. for (int j = 0; j < QK_K/16; ++j) {
  1395. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1396. }
  1397. if (requantize) {
  1398. for (int j = 0; j < QK_K/16; ++j) {
  1399. const float d = dm * (y[i].scales[j] & 0xF);
  1400. if (!d) continue;
  1401. const float m = mm * (y[i].scales[j] >> 4);
  1402. for (int ii = 0; ii < 16; ++ii) {
  1403. int l = nearest_int((x[16*j + ii] + m)/d);
  1404. l = MAX(0, MIN(3, l));
  1405. L[16*j + ii] = l;
  1406. }
  1407. }
  1408. }
  1409. #if QK_K == 256
  1410. for (int j = 0; j < QK_K; j += 128) {
  1411. for (int l = 0; l < 32; ++l) {
  1412. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1413. }
  1414. }
  1415. #else
  1416. for (int l = 0; l < 16; ++l) {
  1417. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1418. }
  1419. #endif
  1420. x += QK_K;
  1421. }
  1422. }
  1423. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1424. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1425. if (!quant_weights) {
  1426. quantize_row_q2_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1427. }
  1428. else {
  1429. char * qrow = (char *)dst;
  1430. for (int64_t row = 0; row < nrow; ++row) {
  1431. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1432. src += n_per_row;
  1433. qrow += row_size;
  1434. }
  1435. }
  1436. return nrow * row_size;
  1437. }
  1438. //========================= 3-bit (de)-quantization
  1439. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int64_t k) {
  1440. assert(k % QK_K == 0);
  1441. const int nb = k / QK_K;
  1442. int8_t L[QK_K];
  1443. float scales[QK_K / 16];
  1444. for (int i = 0; i < nb; i++) {
  1445. float max_scale = 0;
  1446. float amax = 0;
  1447. for (int j = 0; j < QK_K/16; ++j) {
  1448. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1449. float scale = fabsf(scales[j]);
  1450. if (scale > amax) {
  1451. amax = scale; max_scale = scales[j];
  1452. }
  1453. }
  1454. #if QK_K == 256
  1455. memset(y[i].scales, 0, 12);
  1456. if (max_scale) {
  1457. float iscale = -32.f/max_scale;
  1458. for (int j = 0; j < QK_K/16; ++j) {
  1459. int8_t l = nearest_int(iscale*scales[j]);
  1460. l = MAX(-32, MIN(31, l)) + 32;
  1461. if (j < 8) {
  1462. y[i].scales[j] = l & 0xF;
  1463. } else {
  1464. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1465. }
  1466. l >>= 4;
  1467. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1468. }
  1469. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1470. } else {
  1471. y[i].d = GGML_FP32_TO_FP16(0.f);
  1472. }
  1473. int8_t sc;
  1474. for (int j = 0; j < QK_K/16; ++j) {
  1475. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1476. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1477. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1478. if (!d) {
  1479. continue;
  1480. }
  1481. for (int ii = 0; ii < 16; ++ii) {
  1482. int l = nearest_int(x[16*j + ii]/d);
  1483. l = MAX(-4, MIN(3, l));
  1484. L[16*j + ii] = l + 4;
  1485. }
  1486. }
  1487. #else
  1488. if (max_scale) {
  1489. float iscale = -8.f/max_scale;
  1490. for (int j = 0; j < QK_K/16; j+=2) {
  1491. int l1 = nearest_int(iscale*scales[j]);
  1492. l1 = 8 + MAX(-8, MIN(7, l1));
  1493. int l2 = nearest_int(iscale*scales[j+1]);
  1494. l2 = 8 + MAX(-8, MIN(7, l2));
  1495. y[i].scales[j/2] = l1 | (l2 << 4);
  1496. }
  1497. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1498. } else {
  1499. for (int j = 0; j < QK_K/16; j+=2) {
  1500. y[i].scales[j/2] = 0;
  1501. }
  1502. y[i].d = GGML_FP32_TO_FP16(0.f);
  1503. }
  1504. for (int j = 0; j < QK_K/16; ++j) {
  1505. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1506. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1507. if (!d) {
  1508. continue;
  1509. }
  1510. for (int ii = 0; ii < 16; ++ii) {
  1511. int l = nearest_int(x[16*j + ii]/d);
  1512. l = MAX(-4, MIN(3, l));
  1513. L[16*j + ii] = l + 4;
  1514. }
  1515. }
  1516. #endif
  1517. memset(y[i].hmask, 0, QK_K/8);
  1518. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1519. int m = 0;
  1520. uint8_t hm = 1;
  1521. for (int j = 0; j < QK_K; ++j) {
  1522. if (L[j] > 3) {
  1523. y[i].hmask[m] |= hm;
  1524. L[j] -= 4;
  1525. }
  1526. if (++m == QK_K/8) {
  1527. m = 0; hm <<= 1;
  1528. }
  1529. }
  1530. #if QK_K == 256
  1531. for (int j = 0; j < QK_K; j += 128) {
  1532. for (int l = 0; l < 32; ++l) {
  1533. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1534. }
  1535. }
  1536. #else
  1537. for (int l = 0; l < 16; ++l) {
  1538. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1539. }
  1540. #endif
  1541. x += QK_K;
  1542. }
  1543. }
  1544. #if QK_K == 256
  1545. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1546. assert(k % QK_K == 0);
  1547. const int nb = k / QK_K;
  1548. const uint32_t kmask1 = 0x03030303;
  1549. const uint32_t kmask2 = 0x0f0f0f0f;
  1550. uint32_t aux[4];
  1551. const int8_t * scales = (const int8_t*)aux;
  1552. for (int i = 0; i < nb; i++) {
  1553. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1554. const uint8_t * restrict q = x[i].qs;
  1555. const uint8_t * restrict hm = x[i].hmask;
  1556. uint8_t m = 1;
  1557. memcpy(aux, x[i].scales, 12);
  1558. uint32_t tmp = aux[2];
  1559. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1560. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1561. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1562. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1563. int is = 0;
  1564. float dl;
  1565. for (int n = 0; n < QK_K; n += 128) {
  1566. int shift = 0;
  1567. for (int j = 0; j < 4; ++j) {
  1568. dl = d_all * (scales[is++] - 32);
  1569. for (int l = 0; l < 16; ++l) {
  1570. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1571. }
  1572. dl = d_all * (scales[is++] - 32);
  1573. for (int l = 0; l < 16; ++l) {
  1574. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1575. }
  1576. shift += 2;
  1577. m <<= 1;
  1578. }
  1579. q += 32;
  1580. }
  1581. }
  1582. }
  1583. #else
  1584. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int64_t k) {
  1585. assert(k % QK_K == 0);
  1586. assert(QK_K == 64);
  1587. const int nb = k / QK_K;
  1588. for (int i = 0; i < nb; i++) {
  1589. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1590. const uint8_t * restrict q = x[i].qs;
  1591. const uint8_t * restrict hm = x[i].hmask;
  1592. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1593. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1594. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1595. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1596. for (int l=0; l<8; ++l) {
  1597. uint8_t h = hm[l];
  1598. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1599. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1600. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1601. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1602. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1603. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1604. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1605. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1606. }
  1607. y += QK_K;
  1608. }
  1609. }
  1610. #endif
  1611. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int64_t k) {
  1612. quantize_row_q3_K_reference(x, vy, k);
  1613. }
  1614. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int64_t n_per_row, const float * restrict quant_weights) {
  1615. #if QK_K != 256
  1616. (void)quant_weights;
  1617. quantize_row_q3_K_reference(x, y, n_per_row);
  1618. #else
  1619. assert(n_per_row % QK_K == 0);
  1620. const int nb = n_per_row / QK_K;
  1621. int8_t L[QK_K];
  1622. float scales[QK_K / 16];
  1623. float weight[16];
  1624. float sw[QK_K / 16];
  1625. int8_t Ls[QK_K / 16];
  1626. for (int i = 0; i < nb; i++) {
  1627. float sumx2 = 0;
  1628. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1629. float sigma2 = 2*sumx2/QK_K;
  1630. for (int j = 0; j < QK_K/16; ++j) {
  1631. if (quant_weights) {
  1632. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1633. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1634. } else {
  1635. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1636. }
  1637. float sumw = 0;
  1638. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1639. sw[j] = sumw;
  1640. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1641. }
  1642. memset(y[i].scales, 0, 12);
  1643. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1644. for (int j = 0; j < QK_K/16; ++j) {
  1645. int l = Ls[j];
  1646. if (j < 8) {
  1647. y[i].scales[j] = l & 0xF;
  1648. } else {
  1649. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1650. }
  1651. l >>= 4;
  1652. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1653. }
  1654. y[i].d = GGML_FP32_TO_FP16(d_block);
  1655. int8_t sc;
  1656. for (int j = 0; j < QK_K/16; ++j) {
  1657. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1658. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1659. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1660. if (!d) {
  1661. continue;
  1662. }
  1663. for (int ii = 0; ii < 16; ++ii) {
  1664. int l = nearest_int(x[16*j + ii]/d);
  1665. l = MAX(-4, MIN(3, l));
  1666. L[16*j + ii] = l + 4;
  1667. }
  1668. }
  1669. memset(y[i].hmask, 0, QK_K/8);
  1670. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1671. int m = 0;
  1672. uint8_t hm = 1;
  1673. for (int j = 0; j < QK_K; ++j) {
  1674. if (L[j] > 3) {
  1675. y[i].hmask[m] |= hm;
  1676. L[j] -= 4;
  1677. }
  1678. if (++m == QK_K/8) {
  1679. m = 0; hm <<= 1;
  1680. }
  1681. }
  1682. for (int j = 0; j < QK_K; j += 128) {
  1683. for (int l = 0; l < 32; ++l) {
  1684. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1685. }
  1686. }
  1687. x += QK_K;
  1688. }
  1689. #endif
  1690. }
  1691. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1692. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1693. if (!quant_weights) {
  1694. quantize_row_q3_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1695. }
  1696. else {
  1697. char * qrow = (char *)dst;
  1698. for (int64_t row = 0; row < nrow; ++row) {
  1699. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1700. src += n_per_row;
  1701. qrow += row_size;
  1702. }
  1703. }
  1704. return nrow * row_size;
  1705. }
  1706. // ====================== 4-bit (de)-quantization
  1707. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int64_t k) {
  1708. assert(k % QK_K == 0);
  1709. const int nb = k / QK_K;
  1710. uint8_t L[QK_K];
  1711. uint8_t Laux[32];
  1712. float weights[32];
  1713. float mins[QK_K/32];
  1714. float scales[QK_K/32];
  1715. for (int i = 0; i < nb; i++) {
  1716. float max_scale = 0; // as we are deducting the min, scales are always positive
  1717. float max_min = 0;
  1718. for (int j = 0; j < QK_K/32; ++j) {
  1719. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1720. float sum_x2 = 0;
  1721. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1722. float av_x = sqrtf(sum_x2/32);
  1723. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1724. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1725. float scale = scales[j];
  1726. if (scale > max_scale) {
  1727. max_scale = scale;
  1728. }
  1729. float min = mins[j];
  1730. if (min > max_min) {
  1731. max_min = min;
  1732. }
  1733. }
  1734. #if QK_K == 256
  1735. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1736. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1737. for (int j = 0; j < QK_K/32; ++j) {
  1738. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1739. uint8_t lm = nearest_int(inv_min*mins[j]);
  1740. ls = MIN(63, ls);
  1741. lm = MIN(63, lm);
  1742. if (j < 4) {
  1743. y[i].scales[j] = ls;
  1744. y[i].scales[j+4] = lm;
  1745. } else {
  1746. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1747. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1748. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1749. }
  1750. }
  1751. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1752. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1753. uint8_t sc, m;
  1754. for (int j = 0; j < QK_K/32; ++j) {
  1755. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1756. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1757. if (!d) continue;
  1758. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1759. for (int ii = 0; ii < 32; ++ii) {
  1760. int l = nearest_int((x[32*j + ii] + dm)/d);
  1761. l = MAX(0, MIN(15, l));
  1762. L[32*j + ii] = l;
  1763. }
  1764. }
  1765. #else
  1766. const float s_factor = 15.f;
  1767. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1768. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1769. int d1 = nearest_int(inv_scale*scales[0]);
  1770. int m1 = nearest_int(inv_min*mins[0]);
  1771. int d2 = nearest_int(inv_scale*scales[1]);
  1772. int m2 = nearest_int(inv_min*mins[1]);
  1773. y[i].scales[0] = d1 | (m1 << 4);
  1774. y[i].scales[1] = d2 | (m2 << 4);
  1775. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1776. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1777. float sumlx = 0;
  1778. int suml2 = 0;
  1779. for (int j = 0; j < QK_K/32; ++j) {
  1780. const uint8_t sd = y[i].scales[j] & 0xF;
  1781. const uint8_t sm = y[i].scales[j] >> 4;
  1782. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1783. if (!d) continue;
  1784. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1785. for (int ii = 0; ii < 32; ++ii) {
  1786. int l = nearest_int((x[32*j + ii] + m)/d);
  1787. l = MAX(0, MIN(15, l));
  1788. L[32*j + ii] = l;
  1789. sumlx += (x[32*j + ii] + m)*l*sd;
  1790. suml2 += l*l*sd*sd;
  1791. }
  1792. }
  1793. if (suml2) {
  1794. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1795. }
  1796. #endif
  1797. uint8_t * q = y[i].qs;
  1798. for (int j = 0; j < QK_K; j += 64) {
  1799. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1800. q += 32;
  1801. }
  1802. x += QK_K;
  1803. }
  1804. }
  1805. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int64_t k) {
  1806. assert(k % QK_K == 0);
  1807. const int nb = k / QK_K;
  1808. for (int i = 0; i < nb; i++) {
  1809. const uint8_t * q = x[i].qs;
  1810. #if QK_K == 256
  1811. const float d = GGML_FP16_TO_FP32(x[i].d);
  1812. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1813. int is = 0;
  1814. uint8_t sc, m;
  1815. for (int j = 0; j < QK_K; j += 64) {
  1816. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1817. const float d1 = d * sc; const float m1 = min * m;
  1818. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1819. const float d2 = d * sc; const float m2 = min * m;
  1820. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1821. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1822. q += 32; is += 2;
  1823. }
  1824. #else
  1825. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1826. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1827. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1828. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1829. for (int l = 0; l < 32; ++l) {
  1830. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1831. y[l+32] = d2 * (q[l] >> 4) - m2;
  1832. }
  1833. y += QK_K;
  1834. #endif
  1835. }
  1836. }
  1837. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int64_t k) {
  1838. assert(k % QK_K == 0);
  1839. block_q4_K * restrict y = vy;
  1840. quantize_row_q4_K_reference(x, y, k);
  1841. }
  1842. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  1843. #if QK_K != 256
  1844. (void)quant_weights;
  1845. quantize_row_q4_K_reference(x, y, n_per_row);
  1846. #else
  1847. assert(n_per_row % QK_K == 0);
  1848. const int64_t nb = n_per_row / QK_K;
  1849. uint8_t L[QK_K];
  1850. uint8_t Laux[32];
  1851. uint8_t Ls[QK_K/32];
  1852. uint8_t Lm[QK_K/32];
  1853. float weights[32];
  1854. float sw[QK_K/32];
  1855. float mins[QK_K/32];
  1856. float scales[QK_K/32];
  1857. for (int i = 0; i < nb; i++) {
  1858. float sum_x2 = 0;
  1859. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  1860. float sigma2 = 2*sum_x2/QK_K;
  1861. float av_x = sqrtf(sigma2);
  1862. for (int j = 0; j < QK_K/32; ++j) {
  1863. if (quant_weights) {
  1864. const float * qw = quant_weights + QK_K*i + 32*j;
  1865. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  1866. } else {
  1867. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1868. }
  1869. float sumw = 0;
  1870. for (int l = 0; l < 32; ++l) sumw += weights[l];
  1871. sw[j] = sumw;
  1872. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1873. }
  1874. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  1875. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  1876. for (int j = 0; j < QK_K/32; ++j) {
  1877. uint8_t ls = Ls[j];
  1878. uint8_t lm = Lm[j];
  1879. if (j < 4) {
  1880. y[i].scales[j] = ls;
  1881. y[i].scales[j+4] = lm;
  1882. } else {
  1883. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1884. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1885. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1886. }
  1887. }
  1888. y[i].d = GGML_FP32_TO_FP16(d_block);
  1889. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  1890. uint8_t sc, m;
  1891. for (int j = 0; j < QK_K/32; ++j) {
  1892. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1893. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1894. if (!d) continue;
  1895. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1896. for (int ii = 0; ii < 32; ++ii) {
  1897. int l = nearest_int((x[32*j + ii] + dm)/d);
  1898. l = MAX(0, MIN(15, l));
  1899. L[32*j + ii] = l;
  1900. }
  1901. }
  1902. uint8_t * q = y[i].qs;
  1903. for (int j = 0; j < QK_K; j += 64) {
  1904. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1905. q += 32;
  1906. }
  1907. x += QK_K;
  1908. }
  1909. #endif
  1910. }
  1911. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  1912. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  1913. if (!quant_weights) {
  1914. quantize_row_q4_K_reference(src, dst, (int64_t)nrow*n_per_row);
  1915. }
  1916. else {
  1917. char * qrow = (char *)dst;
  1918. for (int64_t row = 0; row < nrow; ++row) {
  1919. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  1920. src += n_per_row;
  1921. qrow += row_size;
  1922. }
  1923. }
  1924. return nrow * row_size;
  1925. }
  1926. // ====================== 5-bit (de)-quantization
  1927. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int64_t k) {
  1928. assert(k % QK_K == 0);
  1929. const int64_t nb = k / QK_K;
  1930. #if QK_K == 256
  1931. uint8_t L[QK_K];
  1932. float mins[QK_K/32];
  1933. float scales[QK_K/32];
  1934. float weights[32];
  1935. uint8_t Laux[32];
  1936. #else
  1937. int8_t L[QK_K];
  1938. float scales[QK_K/16];
  1939. #endif
  1940. for (int i = 0; i < nb; i++) {
  1941. #if QK_K == 256
  1942. float max_scale = 0; // as we are deducting the min, scales are always positive
  1943. float max_min = 0;
  1944. for (int j = 0; j < QK_K/32; ++j) {
  1945. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1946. float sum_x2 = 0;
  1947. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1948. float av_x = sqrtf(sum_x2/32);
  1949. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1950. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  1951. float scale = scales[j];
  1952. if (scale > max_scale) {
  1953. max_scale = scale;
  1954. }
  1955. float min = mins[j];
  1956. if (min > max_min) {
  1957. max_min = min;
  1958. }
  1959. }
  1960. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1961. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1962. for (int j = 0; j < QK_K/32; ++j) {
  1963. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1964. uint8_t lm = nearest_int(inv_min*mins[j]);
  1965. ls = MIN(63, ls);
  1966. lm = MIN(63, lm);
  1967. if (j < 4) {
  1968. y[i].scales[j] = ls;
  1969. y[i].scales[j+4] = lm;
  1970. } else {
  1971. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1972. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1973. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1974. }
  1975. }
  1976. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1977. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1978. uint8_t sc, m;
  1979. for (int j = 0; j < QK_K/32; ++j) {
  1980. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1981. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1982. if (!d) continue;
  1983. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1984. for (int ii = 0; ii < 32; ++ii) {
  1985. int l = nearest_int((x[32*j + ii] + dm)/d);
  1986. l = MAX(0, MIN(31, l));
  1987. L[32*j + ii] = l;
  1988. }
  1989. }
  1990. uint8_t * restrict qh = y[i].qh;
  1991. uint8_t * restrict ql = y[i].qs;
  1992. memset(qh, 0, QK_K/8);
  1993. uint8_t m1 = 1, m2 = 2;
  1994. for (int n = 0; n < QK_K; n += 64) {
  1995. for (int j = 0; j < 32; ++j) {
  1996. int l1 = L[n + j];
  1997. if (l1 > 15) {
  1998. l1 -= 16; qh[j] |= m1;
  1999. }
  2000. int l2 = L[n + j + 32];
  2001. if (l2 > 15) {
  2002. l2 -= 16; qh[j] |= m2;
  2003. }
  2004. ql[j] = l1 | (l2 << 4);
  2005. }
  2006. m1 <<= 2; m2 <<= 2;
  2007. ql += 32;
  2008. }
  2009. #else
  2010. float max_scale = 0, amax = 0;
  2011. for (int j = 0; j < QK_K/16; ++j) {
  2012. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2013. float abs_scale = fabsf(scales[j]);
  2014. if (abs_scale > amax) {
  2015. amax = abs_scale;
  2016. max_scale = scales[j];
  2017. }
  2018. }
  2019. float iscale = -128.f/max_scale;
  2020. for (int j = 0; j < QK_K/16; ++j) {
  2021. int l = nearest_int(iscale*scales[j]);
  2022. y[i].scales[j] = MAX(-128, MIN(127, l));
  2023. }
  2024. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2025. for (int j = 0; j < QK_K/16; ++j) {
  2026. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2027. if (!d) continue;
  2028. for (int ii = 0; ii < 16; ++ii) {
  2029. int l = nearest_int(x[16*j + ii]/d);
  2030. l = MAX(-16, MIN(15, l));
  2031. L[16*j + ii] = l + 16;
  2032. }
  2033. }
  2034. uint8_t * restrict qh = y[i].qh;
  2035. uint8_t * restrict ql = y[i].qs;
  2036. memset(qh, 0, QK_K/8);
  2037. for (int j = 0; j < 32; ++j) {
  2038. int jm = j%8;
  2039. int is = j/8;
  2040. int l1 = L[j];
  2041. if (l1 > 15) {
  2042. l1 -= 16; qh[jm] |= (1 << is);
  2043. }
  2044. int l2 = L[j + 32];
  2045. if (l2 > 15) {
  2046. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2047. }
  2048. ql[j] = l1 | (l2 << 4);
  2049. }
  2050. #endif
  2051. x += QK_K;
  2052. }
  2053. }
  2054. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int64_t k) {
  2055. assert(k % QK_K == 0);
  2056. const int64_t nb = k / QK_K;
  2057. for (int i = 0; i < nb; i++) {
  2058. const uint8_t * ql = x[i].qs;
  2059. const uint8_t * qh = x[i].qh;
  2060. #if QK_K == 256
  2061. const float d = GGML_FP16_TO_FP32(x[i].d);
  2062. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2063. int is = 0;
  2064. uint8_t sc, m;
  2065. uint8_t u1 = 1, u2 = 2;
  2066. for (int j = 0; j < QK_K; j += 64) {
  2067. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2068. const float d1 = d * sc; const float m1 = min * m;
  2069. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2070. const float d2 = d * sc; const float m2 = min * m;
  2071. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2072. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2073. ql += 32; is += 2;
  2074. u1 <<= 2; u2 <<= 2;
  2075. }
  2076. #else
  2077. float d = GGML_FP16_TO_FP32(x[i].d);
  2078. const int8_t * restrict s = x[i].scales;
  2079. for (int l = 0; l < 8; ++l) {
  2080. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2081. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2082. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2083. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2084. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2085. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2086. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2087. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2088. }
  2089. y += QK_K;
  2090. #endif
  2091. }
  2092. }
  2093. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int64_t k) {
  2094. assert(k % QK_K == 0);
  2095. block_q5_K * restrict y = vy;
  2096. quantize_row_q5_K_reference(x, y, k);
  2097. }
  2098. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2099. #if QK_K != 256
  2100. (void)quant_weights;
  2101. quantize_row_q5_K_reference(x, y, n_per_row);
  2102. #else
  2103. assert(n_per_row % QK_K == 0);
  2104. const int64_t nb = n_per_row / QK_K;
  2105. uint8_t L[QK_K];
  2106. uint8_t Laux[32];
  2107. uint8_t Ls[QK_K/32];
  2108. uint8_t Lm[QK_K/32];
  2109. float mins[QK_K/32];
  2110. float scales[QK_K/32];
  2111. float sw[QK_K/32];
  2112. float weights[32];
  2113. for (int i = 0; i < nb; i++) {
  2114. float sum_x2 = 0;
  2115. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2116. float sigma2 = 2*sum_x2/QK_K;
  2117. float av_x = sqrtf(sigma2);
  2118. for (int j = 0; j < QK_K/32; ++j) {
  2119. if (quant_weights) {
  2120. const float * qw = quant_weights + QK_K*i + 32*j;
  2121. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2122. } else {
  2123. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2124. }
  2125. float sumw = 0;
  2126. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2127. sw[j] = sumw;
  2128. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2129. }
  2130. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2131. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2132. for (int j = 0; j < QK_K/32; ++j) {
  2133. uint8_t ls = Ls[j];
  2134. uint8_t lm = Lm[j];
  2135. ls = MIN(63, ls);
  2136. lm = MIN(63, lm);
  2137. if (j < 4) {
  2138. y[i].scales[j] = ls;
  2139. y[i].scales[j+4] = lm;
  2140. } else {
  2141. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2142. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2143. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2144. }
  2145. }
  2146. y[i].d = GGML_FP32_TO_FP16(d_block);
  2147. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2148. uint8_t sc, m;
  2149. for (int j = 0; j < QK_K/32; ++j) {
  2150. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2151. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2152. if (!d) continue;
  2153. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2154. for (int ii = 0; ii < 32; ++ii) {
  2155. int l = nearest_int((x[32*j + ii] + dm)/d);
  2156. l = MAX(0, MIN(31, l));
  2157. L[32*j + ii] = l;
  2158. }
  2159. }
  2160. uint8_t * restrict qh = y[i].qh;
  2161. uint8_t * restrict ql = y[i].qs;
  2162. memset(qh, 0, QK_K/8);
  2163. uint8_t m1 = 1, m2 = 2;
  2164. for (int n = 0; n < QK_K; n += 64) {
  2165. for (int j = 0; j < 32; ++j) {
  2166. int l1 = L[n + j];
  2167. if (l1 > 15) {
  2168. l1 -= 16; qh[j] |= m1;
  2169. }
  2170. int l2 = L[n + j + 32];
  2171. if (l2 > 15) {
  2172. l2 -= 16; qh[j] |= m2;
  2173. }
  2174. ql[j] = l1 | (l2 << 4);
  2175. }
  2176. m1 <<= 2; m2 <<= 2;
  2177. ql += 32;
  2178. }
  2179. x += QK_K;
  2180. }
  2181. #endif
  2182. }
  2183. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2184. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2185. if (!quant_weights) {
  2186. quantize_row_q5_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2187. }
  2188. else {
  2189. char * qrow = (char *)dst;
  2190. for (int64_t row = 0; row < nrow; ++row) {
  2191. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2192. src += n_per_row;
  2193. qrow += row_size;
  2194. }
  2195. }
  2196. return nrow * row_size;
  2197. }
  2198. // ====================== 6-bit (de)-quantization
  2199. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int64_t k) {
  2200. assert(k % QK_K == 0);
  2201. const int64_t nb = k / QK_K;
  2202. int8_t L[QK_K];
  2203. float scales[QK_K/16];
  2204. for (int i = 0; i < nb; i++) {
  2205. float max_scale = 0;
  2206. float max_abs_scale = 0;
  2207. for (int ib = 0; ib < QK_K/16; ++ib) {
  2208. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2209. scales[ib] = scale;
  2210. const float abs_scale = fabsf(scale);
  2211. if (abs_scale > max_abs_scale) {
  2212. max_abs_scale = abs_scale;
  2213. max_scale = scale;
  2214. }
  2215. }
  2216. if (!max_abs_scale) {
  2217. memset(&y[i], 0, sizeof(block_q6_K));
  2218. y[i].d = GGML_FP32_TO_FP16(0.f);
  2219. x += QK_K;
  2220. continue;
  2221. }
  2222. float iscale = -128.f/max_scale;
  2223. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2224. for (int ib = 0; ib < QK_K/16; ++ib) {
  2225. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2226. }
  2227. for (int j = 0; j < QK_K/16; ++j) {
  2228. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2229. if (!d) {
  2230. continue;
  2231. }
  2232. for (int ii = 0; ii < 16; ++ii) {
  2233. int l = nearest_int(x[16*j + ii]/d);
  2234. l = MAX(-32, MIN(31, l));
  2235. L[16*j + ii] = l + 32;
  2236. }
  2237. }
  2238. uint8_t * restrict ql = y[i].ql;
  2239. uint8_t * restrict qh = y[i].qh;
  2240. #if QK_K == 256
  2241. for (int j = 0; j < QK_K; j += 128) {
  2242. for (int l = 0; l < 32; ++l) {
  2243. const uint8_t q1 = L[j + l + 0] & 0xF;
  2244. const uint8_t q2 = L[j + l + 32] & 0xF;
  2245. const uint8_t q3 = L[j + l + 64] & 0xF;
  2246. const uint8_t q4 = L[j + l + 96] & 0xF;
  2247. ql[l+ 0] = q1 | (q3 << 4);
  2248. ql[l+32] = q2 | (q4 << 4);
  2249. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2250. }
  2251. ql += 64;
  2252. qh += 32;
  2253. }
  2254. #else
  2255. for (int l = 0; l < 32; ++l) {
  2256. const uint8_t q1 = L[l + 0] & 0xF;
  2257. const uint8_t q2 = L[l + 32] & 0xF;
  2258. ql[l] = q1 | (q2 << 4);
  2259. }
  2260. for (int l = 0; l < 16; ++l) {
  2261. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2262. }
  2263. #endif
  2264. x += QK_K;
  2265. }
  2266. }
  2267. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int64_t k) {
  2268. assert(k % QK_K == 0);
  2269. const int64_t nb = k / QK_K;
  2270. for (int i = 0; i < nb; i++) {
  2271. const float d = GGML_FP16_TO_FP32(x[i].d);
  2272. const uint8_t * restrict ql = x[i].ql;
  2273. const uint8_t * restrict qh = x[i].qh;
  2274. const int8_t * restrict sc = x[i].scales;
  2275. #if QK_K == 256
  2276. for (int n = 0; n < QK_K; n += 128) {
  2277. for (int l = 0; l < 32; ++l) {
  2278. int is = l/16;
  2279. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2280. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2281. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2282. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2283. y[l + 0] = d * sc[is + 0] * q1;
  2284. y[l + 32] = d * sc[is + 2] * q2;
  2285. y[l + 64] = d * sc[is + 4] * q3;
  2286. y[l + 96] = d * sc[is + 6] * q4;
  2287. }
  2288. y += 128;
  2289. ql += 64;
  2290. qh += 32;
  2291. sc += 8;
  2292. }
  2293. #else
  2294. for (int l = 0; l < 16; ++l) {
  2295. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2296. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2297. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2298. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2299. y[l+ 0] = d * sc[0] * q1;
  2300. y[l+16] = d * sc[1] * q2;
  2301. y[l+32] = d * sc[2] * q3;
  2302. y[l+48] = d * sc[3] * q4;
  2303. }
  2304. y += 64;
  2305. #endif
  2306. }
  2307. }
  2308. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int64_t k) {
  2309. assert(k % QK_K == 0);
  2310. block_q6_K * restrict y = vy;
  2311. quantize_row_q6_K_reference(x, y, k);
  2312. }
  2313. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int64_t n_per_row, const float * quant_weights) {
  2314. #if QK_K != 256
  2315. (void)quant_weights;
  2316. quantize_row_q6_K_reference(x, y, n_per_row);
  2317. #else
  2318. assert(n_per_row % QK_K == 0);
  2319. const int64_t nb = n_per_row / QK_K;
  2320. int8_t L[QK_K];
  2321. float scales[QK_K/16];
  2322. //float weights[16];
  2323. for (int i = 0; i < nb; i++) {
  2324. //float sum_x2 = 0;
  2325. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2326. //float sigma2 = sum_x2/QK_K;
  2327. float max_scale = 0;
  2328. float max_abs_scale = 0;
  2329. for (int ib = 0; ib < QK_K/16; ++ib) {
  2330. float scale;
  2331. if (quant_weights) {
  2332. const float * qw = quant_weights + QK_K*i + 16*ib;
  2333. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2334. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2335. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2336. } else {
  2337. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2338. }
  2339. scales[ib] = scale;
  2340. const float abs_scale = fabsf(scale);
  2341. if (abs_scale > max_abs_scale) {
  2342. max_abs_scale = abs_scale;
  2343. max_scale = scale;
  2344. }
  2345. }
  2346. if (!max_abs_scale) {
  2347. memset(&y[i], 0, sizeof(block_q6_K));
  2348. y[i].d = GGML_FP32_TO_FP16(0.f);
  2349. x += QK_K;
  2350. continue;
  2351. }
  2352. float iscale = -128.f/max_scale;
  2353. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2354. for (int ib = 0; ib < QK_K/16; ++ib) {
  2355. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2356. }
  2357. for (int j = 0; j < QK_K/16; ++j) {
  2358. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2359. if (!d) {
  2360. continue;
  2361. }
  2362. for (int ii = 0; ii < 16; ++ii) {
  2363. int l = nearest_int(x[16*j + ii]/d);
  2364. l = MAX(-32, MIN(31, l));
  2365. L[16*j + ii] = l + 32;
  2366. }
  2367. }
  2368. uint8_t * restrict ql = y[i].ql;
  2369. uint8_t * restrict qh = y[i].qh;
  2370. for (int j = 0; j < QK_K; j += 128) {
  2371. for (int l = 0; l < 32; ++l) {
  2372. const uint8_t q1 = L[j + l + 0] & 0xF;
  2373. const uint8_t q2 = L[j + l + 32] & 0xF;
  2374. const uint8_t q3 = L[j + l + 64] & 0xF;
  2375. const uint8_t q4 = L[j + l + 96] & 0xF;
  2376. ql[l+ 0] = q1 | (q3 << 4);
  2377. ql[l+32] = q2 | (q4 << 4);
  2378. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2379. }
  2380. ql += 64;
  2381. qh += 32;
  2382. }
  2383. x += QK_K;
  2384. }
  2385. #endif
  2386. }
  2387. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2388. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2389. if (!quant_weights) {
  2390. quantize_row_q6_K_reference(src, dst, (int64_t)nrow*n_per_row);
  2391. }
  2392. else {
  2393. char * qrow = (char *)dst;
  2394. for (int64_t row = 0; row < nrow; ++row) {
  2395. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2396. src += n_per_row;
  2397. qrow += row_size;
  2398. }
  2399. }
  2400. return nrow * row_size;
  2401. }
  2402. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2403. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2404. if (!quant_weights) {
  2405. quantize_row_q4_0_reference(x, y, n_per_row);
  2406. return;
  2407. }
  2408. float weight[QK4_0];
  2409. int8_t L[QK4_0];
  2410. float sum_x2 = 0;
  2411. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2412. float sigma2 = sum_x2/n_per_row;
  2413. const int64_t nb = n_per_row/QK4_0;
  2414. for (int ib = 0; ib < nb; ++ib) {
  2415. const float * xb = x + QK4_0 * ib;
  2416. const float * qw = quant_weights + QK4_0 * ib;
  2417. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2418. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2419. y[ib].d = GGML_FP32_TO_FP16(d);
  2420. for (int j = 0; j < 16; ++j) {
  2421. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2422. }
  2423. }
  2424. }
  2425. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2426. if (!quant_weights) {
  2427. quantize_row_q4_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2428. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2429. }
  2430. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2431. char * qrow = (char *)dst;
  2432. for (int64_t row = 0; row < nrow; ++row) {
  2433. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2434. src += n_per_row;
  2435. qrow += row_size;
  2436. }
  2437. return nrow * row_size;
  2438. }
  2439. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2440. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2441. if (!quant_weights) {
  2442. quantize_row_q4_1_reference(x, y, n_per_row);
  2443. return;
  2444. }
  2445. float weight[QK4_1];
  2446. uint8_t L[QK4_1], Laux[QK4_1];
  2447. float sum_x2 = 0;
  2448. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2449. float sigma2 = sum_x2/n_per_row;
  2450. const int64_t nb = n_per_row/QK4_1;
  2451. for (int ib = 0; ib < nb; ++ib) {
  2452. const float * xb = x + QK4_1 * ib;
  2453. const float * qw = quant_weights + QK4_1 * ib;
  2454. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2455. float min;
  2456. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2457. y[ib].d = GGML_FP32_TO_FP16(d);
  2458. y[ib].m = GGML_FP32_TO_FP16(-min);
  2459. for (int j = 0; j < 16; ++j) {
  2460. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2461. }
  2462. }
  2463. }
  2464. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2465. if (!quant_weights) {
  2466. quantize_row_q4_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2467. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2468. }
  2469. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2470. char * qrow = (char *)dst;
  2471. for (int64_t row = 0; row < nrow; ++row) {
  2472. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2473. src += n_per_row;
  2474. qrow += row_size;
  2475. }
  2476. return nrow * row_size;
  2477. }
  2478. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2479. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2480. if (!quant_weights) {
  2481. quantize_row_q5_0_reference(x, y, n_per_row);
  2482. return;
  2483. }
  2484. float weight[QK5_0];
  2485. int8_t L[QK5_0];
  2486. float sum_x2 = 0;
  2487. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2488. float sigma2 = sum_x2/n_per_row;
  2489. const int64_t nb = n_per_row/QK5_0;
  2490. for (int ib = 0; ib < nb; ++ib) {
  2491. const float * xb = x + QK5_0 * ib;
  2492. const float * qw = quant_weights + QK5_0 * ib;
  2493. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2494. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2495. y[ib].d = GGML_FP32_TO_FP16(d);
  2496. uint32_t qh = 0;
  2497. for (int j = 0; j < 16; ++j) {
  2498. const uint8_t xi0 = L[j];
  2499. const uint8_t xi1 = L[j+16];
  2500. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2501. // get the 5-th bit and store it in qh at the right position
  2502. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2503. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2504. }
  2505. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2506. }
  2507. }
  2508. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2509. if (!quant_weights) {
  2510. quantize_row_q5_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2511. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2512. }
  2513. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2514. char * qrow = (char *)dst;
  2515. for (int64_t row = 0; row < nrow; ++row) {
  2516. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2517. src += n_per_row;
  2518. qrow += row_size;
  2519. }
  2520. return nrow * row_size;
  2521. }
  2522. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int64_t n_per_row, const float * quant_weights) {
  2523. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2524. if (!quant_weights) {
  2525. quantize_row_q5_1_reference(x, y, n_per_row);
  2526. return;
  2527. }
  2528. float weight[QK5_1];
  2529. uint8_t L[QK5_1], Laux[QK5_1];
  2530. float sum_x2 = 0;
  2531. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2532. float sigma2 = sum_x2/n_per_row;
  2533. const int64_t nb = n_per_row/QK5_1;
  2534. for (int ib = 0; ib < nb; ++ib) {
  2535. const float * xb = x + QK5_1 * ib;
  2536. const float * qw = quant_weights + QK5_1 * ib;
  2537. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2538. float min;
  2539. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2540. y[ib].d = GGML_FP32_TO_FP16(d);
  2541. y[ib].m = GGML_FP32_TO_FP16(-min);
  2542. uint32_t qh = 0;
  2543. for (int j = 0; j < 16; ++j) {
  2544. const uint8_t xi0 = L[j];
  2545. const uint8_t xi1 = L[j+16];
  2546. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2547. // get the 5-th bit and store it in qh at the right position
  2548. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2549. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2550. }
  2551. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2552. }
  2553. }
  2554. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2555. if (!quant_weights) {
  2556. quantize_row_q5_1_reference(src, dst, (int64_t)nrow*n_per_row);
  2557. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2558. }
  2559. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2560. char * qrow = (char *)dst;
  2561. for (int64_t row = 0; row < nrow; ++row) {
  2562. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2563. src += n_per_row;
  2564. qrow += row_size;
  2565. }
  2566. return nrow * row_size;
  2567. }
  2568. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  2569. (void)quant_weights; // not used
  2570. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2571. quantize_row_q8_0_reference(src, dst, (int64_t)nrow*n_per_row);
  2572. return nrow * row_size;
  2573. }
  2574. // ====================== "True" 2-bit (de)-quantization
  2575. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int64_t k) {
  2576. assert(k % QK_K == 0);
  2577. const int64_t nb = k / QK_K;
  2578. uint32_t aux32[2];
  2579. const uint8_t * aux8 = (const uint8_t *)aux32;
  2580. for (int i = 0; i < nb; i++) {
  2581. const float d = GGML_FP16_TO_FP32(x[i].d);
  2582. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2583. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2584. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2585. for (int l = 0; l < 4; ++l) {
  2586. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2587. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2588. for (int j = 0; j < 8; ++j) {
  2589. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2590. }
  2591. y += 8;
  2592. }
  2593. }
  2594. }
  2595. }
  2596. // ====================== 2.3125 bpw (de)-quantization
  2597. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int64_t k) {
  2598. assert(k % QK_K == 0);
  2599. const int64_t nb = k / QK_K;
  2600. float db[2];
  2601. for (int i = 0; i < nb; i++) {
  2602. const float d = GGML_FP16_TO_FP32(x[i].d);
  2603. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2604. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2605. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2606. for (int l = 0; l < 4; ++l) {
  2607. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2608. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2609. for (int j = 0; j < 8; ++j) {
  2610. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2611. }
  2612. y += 8;
  2613. }
  2614. }
  2615. }
  2616. }
  2617. // ====================== 2.5625 bpw (de)-quantization
  2618. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int64_t k) {
  2619. assert(k % QK_K == 0);
  2620. const int64_t nb = k / QK_K;
  2621. float db[2];
  2622. for (int i = 0; i < nb; i++) {
  2623. const float d = GGML_FP16_TO_FP32(x[i].d);
  2624. const uint8_t * qs = x[i].qs;
  2625. const uint8_t * qh = x[i].qh;
  2626. const uint8_t * signs = qs + QK_K/8;
  2627. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2628. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2629. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2630. for (int l = 0; l < 4; ++l) {
  2631. const float dl = db[l/2];
  2632. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2633. for (int j = 0; j < 8; ++j) {
  2634. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2635. }
  2636. y += 8;
  2637. }
  2638. qs += 4;
  2639. signs += 4;
  2640. }
  2641. }
  2642. }
  2643. // ====================== 3.0625 bpw (de)-quantization
  2644. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int64_t k) {
  2645. assert(k % QK_K == 0);
  2646. const int64_t nb = k / QK_K;
  2647. uint32_t aux32;
  2648. for (int i = 0; i < nb; i++) {
  2649. const float d = GGML_FP16_TO_FP32(x[i].d);
  2650. const uint8_t * qs = x[i].qs;
  2651. const uint8_t * scales_and_signs = qs + QK_K/4;
  2652. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2653. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2654. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2655. for (int l = 0; l < 4; ++l) {
  2656. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2657. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2658. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2659. for (int j = 0; j < 4; ++j) {
  2660. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2661. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2662. }
  2663. y += 8;
  2664. }
  2665. qs += 8;
  2666. }
  2667. }
  2668. }
  2669. // ====================== 3.3125 bpw (de)-quantization
  2670. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int64_t k) {
  2671. assert(k % QK_K == 0);
  2672. const int64_t nb = k / QK_K;
  2673. for (int i = 0; i < nb; i++) {
  2674. const float d = GGML_FP16_TO_FP32(x[i].d);
  2675. const uint8_t * qs = x[i].qs;
  2676. const uint8_t * qh = x[i].qh;
  2677. const uint8_t * signs = x[i].signs;
  2678. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2679. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2680. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2681. for (int l = 0; l < 4; ++l) {
  2682. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2683. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2684. for (int j = 0; j < 4; ++j) {
  2685. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2686. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2687. }
  2688. y += 8;
  2689. }
  2690. qs += 8;
  2691. signs += 4;
  2692. for (int l = 0; l < 4; ++l) {
  2693. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2694. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2695. for (int j = 0; j < 4; ++j) {
  2696. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2697. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2698. }
  2699. y += 8;
  2700. }
  2701. qh += 2;
  2702. qs += 8;
  2703. signs += 4;
  2704. }
  2705. }
  2706. }
  2707. // ====================== 1.5625 bpw (de)-quantization
  2708. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int64_t k) {
  2709. assert(k % QK_K == 0);
  2710. const int64_t nb = k / QK_K;
  2711. for (int i = 0; i < nb; i++) {
  2712. const float d = GGML_FP16_TO_FP32(x[i].d);
  2713. const uint8_t * qs = x[i].qs;
  2714. const uint16_t * qh = x[i].qh;
  2715. for (int ib = 0; ib < QK_K/32; ++ib) {
  2716. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2717. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2718. for (int l = 0; l < 4; ++l) {
  2719. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2720. for (int j = 0; j < 8; ++j) {
  2721. y[j] = dl * (grid[j] + delta);
  2722. }
  2723. y += 8;
  2724. }
  2725. qs += 4;
  2726. }
  2727. }
  2728. }
  2729. void dequantize_row_iq1_m(const block_iq1_m * restrict x, float * restrict y, int64_t k) {
  2730. assert(k % QK_K == 0);
  2731. const int64_t nb = k / QK_K;
  2732. float delta[4];
  2733. uint16_t idx[4];
  2734. #if QK_K != 64
  2735. iq1m_scale_t scale;
  2736. #endif
  2737. for (int i = 0; i < nb; i++) {
  2738. const uint16_t * sc = (const uint16_t *)x[i].scales;
  2739. #if QK_K == 64
  2740. const float d = GGML_FP16_TO_FP32(x[i].d);
  2741. #else
  2742. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2743. const float d = GGML_FP16_TO_FP32(scale.f16);
  2744. #endif
  2745. const uint8_t * qs = x[i].qs;
  2746. const uint8_t * qh = x[i].qh;
  2747. for (int ib = 0; ib < QK_K/32; ++ib) {
  2748. #if QK_K == 64
  2749. const float dl1 = d * (2*((sc[ib/2] >> (8*(ib%2)+0)) & 0xf) + 1);
  2750. const float dl2 = d * (2*((sc[ib/2] >> (8*(ib%2)+4)) & 0xf) + 1);
  2751. #else
  2752. const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
  2753. const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
  2754. #endif
  2755. idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
  2756. idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
  2757. idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
  2758. idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
  2759. delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2760. delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2761. delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2762. delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2763. for (int l = 0; l < 2; ++l) {
  2764. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2765. for (int j = 0; j < 8; ++j) {
  2766. y[j] = dl1 * (grid[j] + delta[l]);
  2767. }
  2768. y += 8;
  2769. }
  2770. for (int l = 2; l < 4; ++l) {
  2771. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2772. for (int j = 0; j < 8; ++j) {
  2773. y[j] = dl2 * (grid[j] + delta[l]);
  2774. }
  2775. y += 8;
  2776. }
  2777. qs += 4;
  2778. qh += 2;
  2779. }
  2780. }
  2781. }
  2782. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2783. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int64_t k) {
  2784. assert(k % QK4_NL == 0);
  2785. const int64_t nb = k / QK4_NL;
  2786. for (int i = 0; i < nb; i++) {
  2787. const uint8_t * qs = x[i].qs;
  2788. const float d = GGML_FP16_TO_FP32(x[i].d);
  2789. for (int j = 0; j < QK4_NL/2; ++j) {
  2790. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  2791. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  2792. }
  2793. y += QK4_NL;
  2794. qs += QK4_NL/2;
  2795. }
  2796. }
  2797. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int64_t k) {
  2798. assert(k % QK_K == 0);
  2799. #if QK_K == 64
  2800. dequantize_row_iq4_nl((const block_iq4_nl *)x, y, k);
  2801. #else
  2802. const int64_t nb = k / QK_K;
  2803. for (int i = 0; i < nb; i++) {
  2804. const uint8_t * qs = x[i].qs;
  2805. const float d = GGML_FP16_TO_FP32(x[i].d);
  2806. for (int ib = 0; ib < QK_K/32; ++ib) {
  2807. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  2808. const float dl = d * (ls - 32);
  2809. for (int j = 0; j < 16; ++j) {
  2810. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  2811. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  2812. }
  2813. y += 32;
  2814. qs += 16;
  2815. }
  2816. }
  2817. #endif
  2818. }
  2819. //===================================== Q8_K ==============================================
  2820. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int64_t k) {
  2821. assert(k % QK_K == 0);
  2822. const int64_t nb = k / QK_K;
  2823. for (int i = 0; i < nb; i++) {
  2824. float max = 0;
  2825. float amax = 0;
  2826. for (int j = 0; j < QK_K; ++j) {
  2827. float ax = fabsf(x[j]);
  2828. if (ax > amax) {
  2829. amax = ax; max = x[j];
  2830. }
  2831. }
  2832. if (!amax) {
  2833. y[i].d = 0;
  2834. memset(y[i].qs, 0, QK_K);
  2835. x += QK_K;
  2836. continue;
  2837. }
  2838. //const float iscale = -128.f/max;
  2839. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  2840. const float iscale = -127.f/max;
  2841. for (int j = 0; j < QK_K; ++j) {
  2842. int v = nearest_int(iscale*x[j]);
  2843. y[i].qs[j] = MIN(127, v);
  2844. }
  2845. for (int j = 0; j < QK_K/16; ++j) {
  2846. int sum = 0;
  2847. for (int ii = 0; ii < 16; ++ii) {
  2848. sum += y[i].qs[j*16 + ii];
  2849. }
  2850. y[i].bsums[j] = sum;
  2851. }
  2852. y[i].d = 1/iscale;
  2853. x += QK_K;
  2854. }
  2855. }
  2856. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int64_t k) {
  2857. assert(k % QK_K == 0);
  2858. const int64_t nb = k / QK_K;
  2859. for (int i = 0; i < nb; i++) {
  2860. for (int j = 0; j < QK_K; ++j) {
  2861. *y++ = x[i].d * x[i].qs[j];
  2862. }
  2863. }
  2864. }
  2865. void quantize_row_q8_K(const float * restrict x, void * restrict y, int64_t k) {
  2866. quantize_row_q8_K_reference(x, y, k);
  2867. }
  2868. //===================================== Dot ptoducts =================================
  2869. //
  2870. // Helper functions
  2871. //
  2872. #if __AVX__ || __AVX2__ || __AVX512F__
  2873. // shuffles to pick the required scales in dot products
  2874. static inline __m256i get_scale_shuffle_q3k(int i) {
  2875. static const uint8_t k_shuffle[128] = {
  2876. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  2877. 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
  2878. 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
  2879. 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13, 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,
  2880. };
  2881. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2882. }
  2883. static inline __m256i get_scale_shuffle_k4(int i) {
  2884. static const uint8_t k_shuffle[256] = {
  2885. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
  2886. 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  2887. 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5,
  2888. 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7,
  2889. 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9,
  2890. 10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,10,11,
  2891. 12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13,
  2892. 14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15,14,15
  2893. };
  2894. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2895. }
  2896. static inline __m128i get_scale_shuffle(int i) {
  2897. static const uint8_t k_shuffle[128] = {
  2898. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2899. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  2900. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  2901. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  2902. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  2903. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  2904. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  2905. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  2906. };
  2907. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  2908. }
  2909. #endif
  2910. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  2911. const int qk = QK8_0;
  2912. const int nb = n / qk;
  2913. assert(n % qk == 0);
  2914. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2915. assert((nrc == 2) || (nrc == 1));
  2916. #else
  2917. assert(nrc == 1);
  2918. #endif
  2919. UNUSED(nrc);
  2920. UNUSED(bx);
  2921. UNUSED(by);
  2922. UNUSED(bs);
  2923. const block_q4_0 * restrict x = vx;
  2924. const block_q8_0 * restrict y = vy;
  2925. #if defined(__ARM_FEATURE_MATMUL_INT8)
  2926. if (nrc == 2) {
  2927. const block_q4_0 * restrict vx0 = vx;
  2928. const block_q4_0 * restrict vx1 = vx + bx;
  2929. const block_q8_0 * restrict vy0 = vy;
  2930. const block_q8_0 * restrict vy1 = vy + by;
  2931. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2932. for (int i = 0; i < nb; i++) {
  2933. const block_q4_0 * restrict b_x0 = &vx0[i];
  2934. const block_q4_0 * restrict b_x1 = &vx1[i];
  2935. const block_q8_0 * restrict b_y0 = &vy0[i];
  2936. const block_q8_0 * restrict b_y1 = &vy1[i];
  2937. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2938. const int8x16_t s8b = vdupq_n_s8(0x8);
  2939. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  2940. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  2941. // 4-bit -> 8-bit
  2942. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2943. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2944. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2945. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2946. // sub 8
  2947. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  2948. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  2949. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  2950. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  2951. // load y
  2952. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  2953. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  2954. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  2955. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  2956. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  2957. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  2958. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  2959. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  2960. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  2961. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  2962. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  2963. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  2964. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  2965. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  2966. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  2967. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  2968. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  2969. l1, r1)), l2, r2)), l3, r3))), scale);
  2970. }
  2971. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  2972. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  2973. vst1_f32(s, vget_low_f32(sumv2));
  2974. vst1_f32(s + bs, vget_high_f32(sumv2));
  2975. return;
  2976. }
  2977. #endif
  2978. #if defined(__ARM_NEON)
  2979. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2980. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2981. assert(nb % 2 == 0); // TODO: handle odd nb
  2982. for (int i = 0; i < nb; i += 2) {
  2983. const block_q4_0 * restrict x0 = &x[i + 0];
  2984. const block_q4_0 * restrict x1 = &x[i + 1];
  2985. const block_q8_0 * restrict y0 = &y[i + 0];
  2986. const block_q8_0 * restrict y1 = &y[i + 1];
  2987. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2988. const int8x16_t s8b = vdupq_n_s8(0x8);
  2989. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2990. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2991. // 4-bit -> 8-bit
  2992. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2993. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2994. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2995. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2996. // sub 8
  2997. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  2998. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  2999. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3000. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3001. // load y
  3002. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3003. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3004. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3005. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3006. // dot product into int32x4_t
  3007. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3008. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3009. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3010. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3011. }
  3012. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3013. #elif defined(__AVX2__)
  3014. // Initialize accumulator with zeros
  3015. __m256 acc = _mm256_setzero_ps();
  3016. // Main loop
  3017. for (int i = 0; i < nb; ++i) {
  3018. /* Compute combined scale for the block */
  3019. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3020. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3021. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3022. const __m256i off = _mm256_set1_epi8( 8 );
  3023. qx = _mm256_sub_epi8( qx, off );
  3024. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3025. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3026. /* Multiply q with scale and accumulate */
  3027. acc = _mm256_fmadd_ps( d, q, acc );
  3028. }
  3029. *s = hsum_float_8(acc);
  3030. #elif defined(__AVX__)
  3031. // Initialize accumulator with zeros
  3032. __m256 acc = _mm256_setzero_ps();
  3033. // Main loop
  3034. for (int i = 0; i < nb; ++i) {
  3035. // Compute combined scale for the block
  3036. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3037. const __m128i lowMask = _mm_set1_epi8(0xF);
  3038. const __m128i off = _mm_set1_epi8(8);
  3039. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3040. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3041. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3042. bx_0 = _mm_sub_epi8(bx_0, off);
  3043. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3044. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3045. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3046. bx_0 = _mm_sub_epi8(bx_0, off);
  3047. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3048. // Convert int32_t to float
  3049. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3050. // Apply the scale, and accumulate
  3051. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3052. }
  3053. *s = hsum_float_8(acc);
  3054. #elif defined(__SSSE3__)
  3055. // set constants
  3056. const __m128i lowMask = _mm_set1_epi8(0xF);
  3057. const __m128i off = _mm_set1_epi8(8);
  3058. // Initialize accumulator with zeros
  3059. __m128 acc_0 = _mm_setzero_ps();
  3060. __m128 acc_1 = _mm_setzero_ps();
  3061. __m128 acc_2 = _mm_setzero_ps();
  3062. __m128 acc_3 = _mm_setzero_ps();
  3063. // First round without accumulation
  3064. {
  3065. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3066. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3067. // Compute combined scale for the block 0 and 1
  3068. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3069. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3070. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3071. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3072. bx_0 = _mm_sub_epi8(bx_0, off);
  3073. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3074. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3075. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3076. bx_1 = _mm_sub_epi8(bx_1, off);
  3077. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3078. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3079. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3080. // Compute combined scale for the block 2 and 3
  3081. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3082. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3083. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3084. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3085. bx_2 = _mm_sub_epi8(bx_2, off);
  3086. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3087. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3088. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3089. bx_3 = _mm_sub_epi8(bx_3, off);
  3090. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3091. // Convert int32_t to float
  3092. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3093. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3094. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3095. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3096. // Apply the scale
  3097. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3098. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3099. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3100. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3101. }
  3102. assert(nb % 2 == 0); // TODO: handle odd nb
  3103. // Main loop
  3104. for (int i = 2; i < nb; i+=2) {
  3105. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3106. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3107. // Compute combined scale for the block 0 and 1
  3108. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3109. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3110. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3111. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3112. bx_0 = _mm_sub_epi8(bx_0, off);
  3113. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3114. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3115. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3116. bx_1 = _mm_sub_epi8(bx_1, off);
  3117. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3118. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3119. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3120. // Compute combined scale for the block 2 and 3
  3121. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3122. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3123. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3124. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3125. bx_2 = _mm_sub_epi8(bx_2, off);
  3126. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3127. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3128. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3129. bx_3 = _mm_sub_epi8(bx_3, off);
  3130. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3131. // Convert int32_t to float
  3132. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3133. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3134. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3135. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3136. // Apply the scale
  3137. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3138. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3139. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3140. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3141. // Acummulate
  3142. acc_0 = _mm_add_ps(p0_d, acc_0);
  3143. acc_1 = _mm_add_ps(p1_d, acc_1);
  3144. acc_2 = _mm_add_ps(p2_d, acc_2);
  3145. acc_3 = _mm_add_ps(p3_d, acc_3);
  3146. }
  3147. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3148. #elif defined(__riscv_v_intrinsic)
  3149. float sumf = 0.0;
  3150. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3151. for (int i = 0; i < nb; i++) {
  3152. // load elements
  3153. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3154. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3155. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3156. // mask and store lower part of x, and then upper part
  3157. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3158. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3159. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3160. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3161. // subtract offset
  3162. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3163. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3164. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3165. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3166. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3167. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3168. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3169. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3170. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3171. }
  3172. *s = sumf;
  3173. #else
  3174. // scalar
  3175. float sumf = 0.0;
  3176. for (int i = 0; i < nb; i++) {
  3177. int sumi = 0;
  3178. for (int j = 0; j < qk/2; ++j) {
  3179. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3180. const int v1 = (x[i].qs[j] >> 4) - 8;
  3181. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3182. }
  3183. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3184. }
  3185. *s = sumf;
  3186. #endif
  3187. }
  3188. void ggml_vec_dot_q4_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3189. const int qk = QK8_1;
  3190. const int nb = n / qk;
  3191. assert(n % qk == 0);
  3192. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3193. assert((nrc == 2) || (nrc == 1));
  3194. #else
  3195. assert(nrc == 1);
  3196. #endif
  3197. UNUSED(nrc);
  3198. UNUSED(bx);
  3199. UNUSED(by);
  3200. UNUSED(bs);
  3201. const block_q4_1 * restrict x = vx;
  3202. const block_q8_1 * restrict y = vy;
  3203. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3204. if (nrc == 2) {
  3205. const block_q4_1 * restrict vx0 = vx;
  3206. const block_q4_1 * restrict vx1 = vx + bx;
  3207. const block_q8_1 * restrict vy0 = vy;
  3208. const block_q8_1 * restrict vy1 = vy + by;
  3209. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3210. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3211. for (int i = 0; i < nb; i++) {
  3212. const block_q4_1 * restrict b_x0 = &vx0[i];
  3213. const block_q4_1 * restrict b_x1 = &vx1[i];
  3214. const block_q8_1 * restrict b_y0 = &vy0[i];
  3215. const block_q8_1 * restrict b_y1 = &vy1[i];
  3216. float32x4_t summs_t = {GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y0->s),
  3217. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y0->s),
  3218. GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y1->s),
  3219. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y1->s)};
  3220. summs0 += summs_t;
  3221. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3222. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3223. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3224. // 4-bit -> 8-bit
  3225. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3226. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3227. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3228. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3229. // load y
  3230. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3231. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3232. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3233. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3234. // mmla into int32x4_t
  3235. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3236. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3237. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3238. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3239. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3240. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3241. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3242. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3243. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3244. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3245. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3246. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3247. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3248. l1, r1)), l2, r2)), l3, r3))), scale);
  3249. }
  3250. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3251. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3252. sumv2 = sumv2 + summs0;
  3253. vst1_f32(s, vget_low_f32(sumv2));
  3254. vst1_f32(s + bs, vget_high_f32(sumv2));
  3255. return;
  3256. }
  3257. #endif
  3258. // TODO: add WASM SIMD
  3259. #if defined(__ARM_NEON)
  3260. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3261. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3262. float summs = 0;
  3263. assert(nb % 2 == 0); // TODO: handle odd nb
  3264. for (int i = 0; i < nb; i += 2) {
  3265. const block_q4_1 * restrict x0 = &x[i + 0];
  3266. const block_q4_1 * restrict x1 = &x[i + 1];
  3267. const block_q8_1 * restrict y0 = &y[i + 0];
  3268. const block_q8_1 * restrict y1 = &y[i + 1];
  3269. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s) + GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3270. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3271. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3272. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3273. // 4-bit -> 8-bit
  3274. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3275. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3276. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3277. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3278. // load y
  3279. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3280. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3281. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3282. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3283. // dot product into int32x4_t
  3284. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3285. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3286. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3287. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3288. }
  3289. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3290. #elif defined(__AVX2__) || defined(__AVX__)
  3291. // Initialize accumulator with zeros
  3292. __m256 acc = _mm256_setzero_ps();
  3293. float summs = 0;
  3294. // Main loop
  3295. for (int i = 0; i < nb; ++i) {
  3296. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3297. const float d1 = GGML_FP16_TO_FP32(y[i].d);
  3298. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3299. const __m256 d0v = _mm256_set1_ps( d0 );
  3300. const __m256 d1v = _mm256_set1_ps( d1 );
  3301. // Compute combined scales
  3302. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3303. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3304. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3305. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3306. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3307. // Accumulate d0*d1*x*y
  3308. #if defined(__AVX2__)
  3309. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3310. #else
  3311. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3312. #endif
  3313. }
  3314. *s = hsum_float_8(acc) + summs;
  3315. #elif defined(__riscv_v_intrinsic)
  3316. float sumf = 0.0;
  3317. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3318. for (int i = 0; i < nb; i++) {
  3319. // load elements
  3320. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3321. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3322. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3323. // mask and store lower part of x, and then upper part
  3324. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3325. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3326. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3327. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3328. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3329. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3330. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3331. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3332. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3333. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3334. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3335. }
  3336. *s = sumf;
  3337. #else
  3338. // scalar
  3339. float sumf = 0.0;
  3340. for (int i = 0; i < nb; i++) {
  3341. int sumi = 0;
  3342. for (int j = 0; j < qk/2; ++j) {
  3343. const int v0 = (x[i].qs[j] & 0x0F);
  3344. const int v1 = (x[i].qs[j] >> 4);
  3345. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3346. }
  3347. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3348. }
  3349. *s = sumf;
  3350. #endif
  3351. }
  3352. void ggml_vec_dot_q5_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3353. const int qk = QK8_0;
  3354. const int nb = n / qk;
  3355. assert(n % qk == 0);
  3356. assert(qk == QK5_0);
  3357. assert(nrc == 1);
  3358. UNUSED(nrc);
  3359. UNUSED(bx);
  3360. UNUSED(by);
  3361. UNUSED(bs);
  3362. const block_q5_0 * restrict x = vx;
  3363. const block_q8_0 * restrict y = vy;
  3364. #if defined(__ARM_NEON)
  3365. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3366. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3367. uint32_t qh0;
  3368. uint32_t qh1;
  3369. uint64_t tmp0[4];
  3370. uint64_t tmp1[4];
  3371. assert(nb % 2 == 0); // TODO: handle odd nb
  3372. for (int i = 0; i < nb; i += 2) {
  3373. const block_q5_0 * restrict x0 = &x[i];
  3374. const block_q5_0 * restrict x1 = &x[i + 1];
  3375. const block_q8_0 * restrict y0 = &y[i];
  3376. const block_q8_0 * restrict y1 = &y[i + 1];
  3377. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3378. // extract the 5th bit via lookup table ((!b) << 4)
  3379. memcpy(&qh0, x0->qh, sizeof(qh0));
  3380. memcpy(&qh1, x1->qh, sizeof(qh1));
  3381. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3382. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3383. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3384. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3385. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3386. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3387. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3388. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3389. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3390. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3391. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3392. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3393. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3394. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3395. // 4-bit -> 8-bit
  3396. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3397. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3398. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3399. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3400. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3401. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3402. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3403. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3404. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3405. // load y
  3406. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3407. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3408. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3409. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3410. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3411. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3412. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3413. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3414. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3415. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3416. }
  3417. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3418. #elif defined(__wasm_simd128__)
  3419. v128_t sumv = wasm_f32x4_splat(0.0f);
  3420. uint32_t qh;
  3421. uint64_t tmp[4];
  3422. // TODO: check if unrolling this is better
  3423. for (int i = 0; i < nb; ++i) {
  3424. const block_q5_0 * restrict x0 = &x[i];
  3425. const block_q8_0 * restrict y0 = &y[i];
  3426. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3427. // extract the 5th bit
  3428. memcpy(&qh, x0->qh, sizeof(qh));
  3429. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3430. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3431. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3432. tmp[3] = table_b2b_1[(qh >> 24) ];
  3433. const v128_t qhl = wasm_v128_load(tmp + 0);
  3434. const v128_t qhh = wasm_v128_load(tmp + 2);
  3435. const v128_t v0 = wasm_v128_load(x0->qs);
  3436. // 4-bit -> 8-bit
  3437. const v128_t v0l = wasm_v128_and (v0, m4b);
  3438. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3439. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3440. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3441. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3442. // load y
  3443. const v128_t v1l = wasm_v128_load(y0->qs);
  3444. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3445. // int8x16 -> int16x8
  3446. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3447. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3448. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3449. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3450. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3451. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3452. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3453. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3454. // dot product
  3455. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3456. wasm_i32x4_add(
  3457. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3458. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3459. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3460. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3461. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3462. }
  3463. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3464. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3465. #elif defined(__AVX2__)
  3466. // Initialize accumulator with zeros
  3467. __m256 acc = _mm256_setzero_ps();
  3468. // Main loop
  3469. for (int i = 0; i < nb; i++) {
  3470. /* Compute combined scale for the block */
  3471. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3472. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3473. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3474. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3475. qx = _mm256_or_si256(qx, bxhi);
  3476. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3477. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3478. /* Multiply q with scale and accumulate */
  3479. acc = _mm256_fmadd_ps(d, q, acc);
  3480. }
  3481. *s = hsum_float_8(acc);
  3482. #elif defined(__AVX__)
  3483. // Initialize accumulator with zeros
  3484. __m256 acc = _mm256_setzero_ps();
  3485. __m128i mask = _mm_set1_epi8((char)0xF0);
  3486. // Main loop
  3487. for (int i = 0; i < nb; i++) {
  3488. /* Compute combined scale for the block */
  3489. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3490. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3491. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3492. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3493. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3494. bxhil = _mm_andnot_si128(bxhil, mask);
  3495. bxhih = _mm_andnot_si128(bxhih, mask);
  3496. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3497. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3498. bxl = _mm_or_si128(bxl, bxhil);
  3499. bxh = _mm_or_si128(bxh, bxhih);
  3500. bx_0 = MM256_SET_M128I(bxh, bxl);
  3501. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3502. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3503. /* Multiply q with scale and accumulate */
  3504. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3505. }
  3506. *s = hsum_float_8(acc);
  3507. #elif defined(__riscv_v_intrinsic)
  3508. float sumf = 0.0;
  3509. uint32_t qh;
  3510. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3511. // These temporary registers are for masking and shift operations
  3512. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3513. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3514. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3515. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3516. for (int i = 0; i < nb; i++) {
  3517. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3518. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3519. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3520. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3521. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3522. // ((qh & (1u << (j + 16))) >> (j + 12));
  3523. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3524. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3525. // narrowing
  3526. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3527. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3528. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3529. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3530. // load
  3531. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3532. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3533. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3534. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3535. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3536. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3537. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3538. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3539. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3540. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3541. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3542. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3543. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3544. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3545. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3546. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3547. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3548. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3549. }
  3550. *s = sumf;
  3551. #else
  3552. // scalar
  3553. float sumf = 0.0;
  3554. for (int i = 0; i < nb; i++) {
  3555. uint32_t qh;
  3556. memcpy(&qh, x[i].qh, sizeof(qh));
  3557. int sumi = 0;
  3558. for (int j = 0; j < qk/2; ++j) {
  3559. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3560. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3561. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3562. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3563. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3564. }
  3565. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3566. }
  3567. *s = sumf;
  3568. #endif
  3569. }
  3570. void ggml_vec_dot_q5_1_q8_1(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3571. const int qk = QK8_1;
  3572. const int nb = n / qk;
  3573. assert(n % qk == 0);
  3574. assert(qk == QK5_1);
  3575. assert(nrc == 1);
  3576. UNUSED(nrc);
  3577. UNUSED(bx);
  3578. UNUSED(by);
  3579. UNUSED(bs);
  3580. const block_q5_1 * restrict x = vx;
  3581. const block_q8_1 * restrict y = vy;
  3582. #if defined(__ARM_NEON)
  3583. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3584. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3585. float summs0 = 0.0f;
  3586. float summs1 = 0.0f;
  3587. uint32_t qh0;
  3588. uint32_t qh1;
  3589. uint64_t tmp0[4];
  3590. uint64_t tmp1[4];
  3591. assert(nb % 2 == 0); // TODO: handle odd nb
  3592. for (int i = 0; i < nb; i += 2) {
  3593. const block_q5_1 * restrict x0 = &x[i];
  3594. const block_q5_1 * restrict x1 = &x[i + 1];
  3595. const block_q8_1 * restrict y0 = &y[i];
  3596. const block_q8_1 * restrict y1 = &y[i + 1];
  3597. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3598. summs0 += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3599. summs1 += GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3600. // extract the 5th bit via lookup table ((b) << 4)
  3601. memcpy(&qh0, x0->qh, sizeof(qh0));
  3602. memcpy(&qh1, x1->qh, sizeof(qh1));
  3603. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3604. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3605. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3606. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3607. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3608. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3609. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3610. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3611. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3612. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3613. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3614. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3615. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3616. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3617. // 4-bit -> 8-bit
  3618. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3619. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3620. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3621. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3622. // add high bit
  3623. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3624. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3625. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3626. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3627. // load y
  3628. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3629. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3630. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3631. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3632. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3633. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3634. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3635. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3636. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3637. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3638. }
  3639. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3640. #elif defined(__wasm_simd128__)
  3641. v128_t sumv = wasm_f32x4_splat(0.0f);
  3642. float summs = 0.0f;
  3643. uint32_t qh;
  3644. uint64_t tmp[4];
  3645. // TODO: check if unrolling this is better
  3646. for (int i = 0; i < nb; ++i) {
  3647. const block_q5_1 * restrict x0 = &x[i];
  3648. const block_q8_1 * restrict y0 = &y[i];
  3649. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3650. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3651. // extract the 5th bit
  3652. memcpy(&qh, x0->qh, sizeof(qh));
  3653. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3654. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3655. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3656. tmp[3] = table_b2b_0[(qh >> 24) ];
  3657. const v128_t qhl = wasm_v128_load(tmp + 0);
  3658. const v128_t qhh = wasm_v128_load(tmp + 2);
  3659. const v128_t v0 = wasm_v128_load(x0->qs);
  3660. // 4-bit -> 8-bit
  3661. const v128_t v0l = wasm_v128_and (v0, m4b);
  3662. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3663. // add high bit
  3664. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3665. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3666. // load y
  3667. const v128_t v1l = wasm_v128_load(y0->qs);
  3668. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3669. // int8x16 -> int16x8
  3670. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3671. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3672. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3673. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3674. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3675. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3676. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3677. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3678. // dot product
  3679. sumv = wasm_f32x4_add(sumv,
  3680. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3681. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3682. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3683. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3684. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3685. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3686. }
  3687. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3688. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3689. #elif defined(__AVX2__)
  3690. // Initialize accumulator with zeros
  3691. __m256 acc = _mm256_setzero_ps();
  3692. float summs = 0.0f;
  3693. // Main loop
  3694. for (int i = 0; i < nb; i++) {
  3695. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3696. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3697. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3698. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3699. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3700. qx = _mm256_or_si256(qx, bxhi);
  3701. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3702. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3703. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  3704. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3705. }
  3706. *s = hsum_float_8(acc) + summs;
  3707. #elif defined(__AVX__)
  3708. // Initialize accumulator with zeros
  3709. __m256 acc = _mm256_setzero_ps();
  3710. __m128i mask = _mm_set1_epi8(0x10);
  3711. float summs = 0.0f;
  3712. // Main loop
  3713. for (int i = 0; i < nb; i++) {
  3714. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3715. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3716. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3717. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3718. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3719. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3720. bxhil = _mm_and_si128(bxhil, mask);
  3721. bxhih = _mm_and_si128(bxhih, mask);
  3722. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3723. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3724. bxl = _mm_or_si128(bxl, bxhil);
  3725. bxh = _mm_or_si128(bxh, bxhih);
  3726. bx_0 = MM256_SET_M128I(bxh, bxl);
  3727. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3728. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3729. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  3730. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3731. }
  3732. *s = hsum_float_8(acc) + summs;
  3733. #elif defined(__riscv_v_intrinsic)
  3734. float sumf = 0.0;
  3735. uint32_t qh;
  3736. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3737. // temporary registers for shift operations
  3738. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3739. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3740. for (int i = 0; i < nb; i++) {
  3741. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3742. // load qh
  3743. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3744. // ((qh >> (j + 0)) << 4) & 0x10;
  3745. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3746. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3747. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3748. // ((qh >> (j + 12)) ) & 0x10;
  3749. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3750. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3751. // narrowing
  3752. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3753. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3754. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3755. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3756. // load
  3757. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3758. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3759. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3760. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3761. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3762. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3763. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3764. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3765. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3766. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3767. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3768. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3769. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3770. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3771. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3772. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3773. }
  3774. *s = sumf;
  3775. #else
  3776. // scalar
  3777. float sumf = 0.0;
  3778. for (int i = 0; i < nb; i++) {
  3779. uint32_t qh;
  3780. memcpy(&qh, x[i].qh, sizeof(qh));
  3781. int sumi = 0;
  3782. for (int j = 0; j < qk/2; ++j) {
  3783. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3784. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3785. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3786. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3787. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3788. }
  3789. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d))*sumi + GGML_FP16_TO_FP32(x[i].m)*GGML_FP16_TO_FP32(y[i].s);
  3790. }
  3791. *s = sumf;
  3792. #endif
  3793. }
  3794. void ggml_vec_dot_q8_0_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3795. const int qk = QK8_0;
  3796. const int nb = n / qk;
  3797. assert(n % qk == 0);
  3798. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3799. assert((nrc == 2) || (nrc == 1));
  3800. #else
  3801. assert(nrc == 1);
  3802. #endif
  3803. UNUSED(nrc);
  3804. UNUSED(bx);
  3805. UNUSED(by);
  3806. UNUSED(bs);
  3807. const block_q8_0 * restrict x = vx;
  3808. const block_q8_0 * restrict y = vy;
  3809. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3810. if (nrc == 2) {
  3811. const block_q8_0 * restrict vx0 = vx;
  3812. const block_q8_0 * restrict vx1 = vx + bx;
  3813. const block_q8_0 * restrict vy0 = vy;
  3814. const block_q8_0 * restrict vy1 = vy + by;
  3815. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3816. for (int i = 0; i < nb; i++) {
  3817. const block_q8_0 * restrict b_x0 = &vx0[i];
  3818. const block_q8_0 * restrict b_y0 = &vy0[i];
  3819. const block_q8_0 * restrict b_x1 = &vx1[i];
  3820. const block_q8_0 * restrict b_y1 = &vy1[i];
  3821. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  3822. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  3823. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  3824. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  3825. // load y
  3826. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3827. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3828. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3829. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3830. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3831. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3832. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3833. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3834. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3835. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3836. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3837. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3838. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3839. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3840. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3841. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3842. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3843. l1, r1)), l2, r2)), l3, r3))), scale);
  3844. }
  3845. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3846. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3847. vst1_f32(s, vget_low_f32(sumv2));
  3848. vst1_f32(s + bs, vget_high_f32(sumv2));
  3849. return;
  3850. }
  3851. #endif
  3852. #if defined(__ARM_NEON)
  3853. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3854. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3855. assert(nb % 2 == 0); // TODO: handle odd nb
  3856. for (int i = 0; i < nb; i += 2) {
  3857. const block_q8_0 * restrict x0 = &x[i + 0];
  3858. const block_q8_0 * restrict x1 = &x[i + 1];
  3859. const block_q8_0 * restrict y0 = &y[i + 0];
  3860. const block_q8_0 * restrict y1 = &y[i + 1];
  3861. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3862. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3863. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3864. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3865. // load y
  3866. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3867. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3868. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3869. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3870. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3871. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3872. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3873. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3874. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3875. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3876. }
  3877. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3878. #elif defined(__AVX2__) || defined(__AVX__)
  3879. // Initialize accumulator with zeros
  3880. __m256 acc = _mm256_setzero_ps();
  3881. // Main loop
  3882. for (int i = 0; i < nb; ++i) {
  3883. // Compute combined scale for the block
  3884. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3885. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3886. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3887. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3888. // Multiply q with scale and accumulate
  3889. #if defined(__AVX2__)
  3890. acc = _mm256_fmadd_ps( d, q, acc );
  3891. #else
  3892. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3893. #endif
  3894. }
  3895. *s = hsum_float_8(acc);
  3896. #elif defined(__riscv_v_intrinsic)
  3897. float sumf = 0.0;
  3898. size_t vl = __riscv_vsetvl_e8m1(qk);
  3899. for (int i = 0; i < nb; i++) {
  3900. // load elements
  3901. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3902. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3903. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  3904. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3905. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3906. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3907. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3908. }
  3909. *s = sumf;
  3910. #else
  3911. // scalar
  3912. float sumf = 0.0;
  3913. for (int i = 0; i < nb; i++) {
  3914. int sumi = 0;
  3915. for (int j = 0; j < qk; j++) {
  3916. sumi += x[i].qs[j]*y[i].qs[j];
  3917. }
  3918. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3919. }
  3920. *s = sumf;
  3921. #endif
  3922. }
  3923. #if QK_K == 256
  3924. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  3925. assert(nrc == 1);
  3926. UNUSED(nrc);
  3927. UNUSED(bx);
  3928. UNUSED(by);
  3929. UNUSED(bs);
  3930. const block_q2_K * restrict x = vx;
  3931. const block_q8_K * restrict y = vy;
  3932. const int nb = n / QK_K;
  3933. #ifdef __ARM_NEON
  3934. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3935. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3936. const int32x4_t vzero = vdupq_n_s32(0);
  3937. ggml_int8x16x2_t q2bytes;
  3938. uint8_t aux[16];
  3939. float sum = 0;
  3940. for (int i = 0; i < nb; ++i) {
  3941. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3942. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3943. const uint8_t * restrict q2 = x[i].qs;
  3944. const int8_t * restrict q8 = y[i].qs;
  3945. const uint8_t * restrict sc = x[i].scales;
  3946. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3947. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3948. vst1q_u8(aux, scales);
  3949. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3950. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3951. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3952. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3953. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3954. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3955. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3956. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3957. int isum = 0;
  3958. int is = 0;
  3959. // We use this macro instead of a function call because for some reason
  3960. // the code runs 2-3% slower, even if the function is declared inline
  3961. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3962. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3963. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3964. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3965. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3966. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3967. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3968. MULTIPLY_ACCUM_WITH_SCALE((index));
  3969. for (int j = 0; j < QK_K/128; ++j) {
  3970. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3971. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3972. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3973. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3974. MULTIPLY_ACCUM_WITH_SCALE(0);
  3975. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3976. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3977. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3978. is += 8;
  3979. }
  3980. sum += d * isum;
  3981. }
  3982. *s = sum;
  3983. #elif defined __AVX2__
  3984. const __m256i m3 = _mm256_set1_epi8(3);
  3985. const __m128i m4 = _mm_set1_epi8(0xF);
  3986. __m256 acc = _mm256_setzero_ps();
  3987. for (int i = 0; i < nb; ++i) {
  3988. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3989. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3990. const uint8_t * restrict q2 = x[i].qs;
  3991. const int8_t * restrict q8 = y[i].qs;
  3992. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3993. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3994. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3995. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  3996. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  3997. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  3998. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  3999. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4000. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4001. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4002. __m256i sumi = _mm256_setzero_si256();
  4003. for (int j = 0; j < QK_K/128; ++j) {
  4004. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4005. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4006. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4007. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4008. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4009. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4010. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4011. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4012. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4013. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4014. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4015. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4016. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4017. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4018. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4019. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4020. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4021. p0 = _mm256_add_epi32(p0, p1);
  4022. p2 = _mm256_add_epi32(p2, p3);
  4023. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4024. }
  4025. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4026. }
  4027. *s = hsum_float_8(acc);
  4028. #elif defined __AVX__
  4029. const __m128i m3 = _mm_set1_epi8(0x3);
  4030. const __m128i m4 = _mm_set1_epi8(0xF);
  4031. const __m128i m2 = _mm_set1_epi8(0x2);
  4032. __m256 acc = _mm256_setzero_ps();
  4033. for (int i = 0; i < nb; ++i) {
  4034. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4035. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4036. const uint8_t * restrict q2 = x[i].qs;
  4037. const int8_t * restrict q8 = y[i].qs;
  4038. // load mins and scales from block_q2_K.scales[QK_K/16]
  4039. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4040. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4041. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4042. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4043. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4044. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4045. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4046. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4047. // sumf += -dmin * summs in 32bits*8
  4048. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4049. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4050. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4051. const __m128i scales[2] = { scales_0, scales_1 };
  4052. __m128i sumi_0 = _mm_setzero_si128();
  4053. __m128i sumi_1 = _mm_setzero_si128();
  4054. for (int j = 0; j < QK_K/128; ++j) {
  4055. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4056. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4057. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4058. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4059. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4060. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4061. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4062. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4063. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4064. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4065. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4066. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4067. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4068. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4069. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4070. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4071. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4072. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4073. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4074. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4075. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4076. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4077. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4078. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4079. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4080. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4081. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4082. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4083. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4084. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4085. __m128i shuffle = _mm_set1_epi16(0x0100);
  4086. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4087. shuffle = _mm_add_epi16(shuffle, m2);
  4088. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4089. shuffle = _mm_add_epi16(shuffle, m2);
  4090. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4091. shuffle = _mm_add_epi16(shuffle, m2);
  4092. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4093. shuffle = _mm_add_epi16(shuffle, m2);
  4094. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4095. shuffle = _mm_add_epi16(shuffle, m2);
  4096. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4097. shuffle = _mm_add_epi16(shuffle, m2);
  4098. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4099. shuffle = _mm_add_epi16(shuffle, m2);
  4100. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4101. p0 = _mm_add_epi32(p0, p1);
  4102. p2 = _mm_add_epi32(p2, p3);
  4103. p4 = _mm_add_epi32(p4, p5);
  4104. p6 = _mm_add_epi32(p6, p7);
  4105. // isum in 32bits*4*2
  4106. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4107. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4108. }
  4109. // sumf += dall * isum - dmin * summs in 32bits
  4110. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4111. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4112. }
  4113. *s = hsum_float_8(acc);
  4114. #elif defined __riscv_v_intrinsic
  4115. float sumf = 0;
  4116. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4117. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4118. for (int i = 0; i < nb; ++i) {
  4119. const uint8_t * q2 = x[i].qs;
  4120. const int8_t * q8 = y[i].qs;
  4121. const uint8_t * sc = x[i].scales;
  4122. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4123. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4124. size_t vl = 16;
  4125. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4126. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4127. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4128. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4129. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4130. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4131. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4132. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4133. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4134. vl = 32;
  4135. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4136. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4137. uint8_t is=0;
  4138. int isum=0;
  4139. for (int j = 0; j < QK_K/128; ++j) {
  4140. // load Q2
  4141. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4142. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4143. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4144. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4145. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4146. // duplicate scale elements for product
  4147. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4148. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4149. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4150. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4151. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4152. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4153. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4154. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4155. // load Q8
  4156. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4157. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4158. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4159. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4160. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4161. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4162. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4163. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4164. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4165. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4166. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4167. q2+=32; q8+=128; is=8;
  4168. }
  4169. sumf += dall * isum;
  4170. }
  4171. *s = sumf;
  4172. #else
  4173. float sumf = 0;
  4174. for (int i = 0; i < nb; ++i) {
  4175. const uint8_t * q2 = x[i].qs;
  4176. const int8_t * q8 = y[i].qs;
  4177. const uint8_t * sc = x[i].scales;
  4178. int summs = 0;
  4179. for (int j = 0; j < 16; ++j) {
  4180. summs += y[i].bsums[j] * (sc[j] >> 4);
  4181. }
  4182. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4183. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4184. int isum = 0;
  4185. int is = 0;
  4186. int d;
  4187. for (int k = 0; k < QK_K/128; ++k) {
  4188. int shift = 0;
  4189. for (int j = 0; j < 4; ++j) {
  4190. d = sc[is++] & 0xF;
  4191. int isuml = 0;
  4192. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4193. isum += d * isuml;
  4194. d = sc[is++] & 0xF;
  4195. isuml = 0;
  4196. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4197. isum += d * isuml;
  4198. shift += 2;
  4199. q8 += 32;
  4200. }
  4201. q2 += 32;
  4202. }
  4203. sumf += dall * isum - dmin * summs;
  4204. }
  4205. *s = sumf;
  4206. #endif
  4207. }
  4208. #else
  4209. void ggml_vec_dot_q2_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4210. assert(nrc == 1);
  4211. UNUSED(nrc);
  4212. UNUSED(bx);
  4213. UNUSED(by);
  4214. UNUSED(bs);
  4215. const block_q2_K * restrict x = vx;
  4216. const block_q8_K * restrict y = vy;
  4217. const int nb = n / QK_K;
  4218. #ifdef __ARM_NEON
  4219. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4220. const int32x4_t vzero = vdupq_n_s32(0);
  4221. ggml_int8x16x4_t q2bytes;
  4222. uint32_t aux32[2];
  4223. const uint8_t * scales = (const uint8_t *)aux32;
  4224. float sum = 0;
  4225. for (int i = 0; i < nb; ++i) {
  4226. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4227. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4228. const uint8_t * restrict q2 = x[i].qs;
  4229. const int8_t * restrict q8 = y[i].qs;
  4230. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4231. aux32[0] = sc[0] & 0x0f0f0f0f;
  4232. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4233. sum += dmin * (scales[4] * y[i].bsums[0] + scales[5] * y[i].bsums[1] + scales[6] * y[i].bsums[2] + scales[7] * y[i].bsums[3]);
  4234. int isum1 = 0, isum2 = 0;
  4235. const uint8x16_t q2bits = vld1q_u8(q2);
  4236. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4237. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4238. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4239. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4240. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4241. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4242. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4243. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4244. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4245. sum += d * (isum1 + isum2);
  4246. }
  4247. *s = sum;
  4248. #elif defined __AVX2__
  4249. const __m256i m3 = _mm256_set1_epi8(3);
  4250. __m256 acc = _mm256_setzero_ps();
  4251. uint32_t ud, um;
  4252. const uint8_t * restrict db = (const uint8_t *)&ud;
  4253. const uint8_t * restrict mb = (const uint8_t *)&um;
  4254. float summs = 0;
  4255. // TODO: optimize this
  4256. for (int i = 0; i < nb; ++i) {
  4257. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4258. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4259. const uint8_t * restrict q2 = x[i].qs;
  4260. const int8_t * restrict q8 = y[i].qs;
  4261. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4262. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4263. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4264. int32_t smin = mb[0] * y[i].bsums[0] + mb[1] * y[i].bsums[1] + mb[2] * y[i].bsums[2] + mb[3] * y[i].bsums[3];
  4265. summs += dmin * smin;
  4266. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4267. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4268. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4269. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4270. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4271. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4272. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4273. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4274. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4275. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4276. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4277. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4278. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4279. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4280. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4281. }
  4282. *s = hsum_float_8(acc) + summs;
  4283. #elif defined __AVX__
  4284. const __m128i m3 = _mm_set1_epi8(3);
  4285. __m256 acc = _mm256_setzero_ps();
  4286. uint32_t ud, um;
  4287. const uint8_t * restrict db = (const uint8_t *)&ud;
  4288. const uint8_t * restrict mb = (const uint8_t *)&um;
  4289. float summs = 0;
  4290. // TODO: optimize this
  4291. for (int i = 0; i < nb; ++i) {
  4292. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4293. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4294. const uint8_t * restrict q2 = x[i].qs;
  4295. const int8_t * restrict q8 = y[i].qs;
  4296. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4297. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4298. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4299. int32_t smin = mb[0] * y[i].bsums[0] + mb[1] * y[i].bsums[1] + mb[2] * y[i].bsums[2] + mb[3] * y[i].bsums[3];
  4300. summs += dmin * smin;
  4301. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4302. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4303. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4304. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4305. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4306. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4307. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4308. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4309. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4310. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4311. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4312. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4313. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4314. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4315. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4316. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4317. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4318. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4319. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4320. }
  4321. *s = hsum_float_8(acc) + summs;
  4322. #elif defined __riscv_v_intrinsic
  4323. uint32_t aux32[2];
  4324. const uint8_t * scales = (const uint8_t *)aux32;
  4325. float sumf = 0;
  4326. for (int i = 0; i < nb; ++i) {
  4327. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4328. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4329. const uint8_t * restrict q2 = x[i].qs;
  4330. const int8_t * restrict q8 = y[i].qs;
  4331. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4332. aux32[0] = sc[0] & 0x0f0f0f0f;
  4333. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4334. sumf += dmin * (scales[4] * y[i].bsums[0] + scales[5] * y[i].bsums[1] + scales[6] * y[i].bsums[2] + scales[7] * y[i].bsums[3]);
  4335. int isum1 = 0;
  4336. int isum2 = 0;
  4337. size_t vl = 16;
  4338. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4339. // load Q2
  4340. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4341. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4342. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4343. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4344. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4345. // load Q8, and take product with Q2
  4346. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4347. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4348. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4349. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4350. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4351. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4352. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4353. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4354. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4355. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4356. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4357. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4358. sumf += d * (isum1 + isum2);
  4359. }
  4360. *s = sumf;
  4361. #else
  4362. float sumf = 0;
  4363. int isum[QK_K/16];
  4364. for (int i = 0; i < nb; ++i) {
  4365. const uint8_t * q2 = x[i].qs;
  4366. const int8_t * q8 = y[i].qs;
  4367. const uint8_t * sc = x[i].scales;
  4368. int summs = 0;
  4369. for (int j = 0; j < QK_K/16; ++j) {
  4370. summs += y[i].bsums[j] * (sc[j] >> 4);
  4371. }
  4372. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4373. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4374. memset(isum, 0, (QK_K/16)*sizeof(int));
  4375. for (int l = 0; l < 16; ++l) {
  4376. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4377. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4378. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4379. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4380. }
  4381. for (int l = 0; l < QK_K/16; ++l) {
  4382. isum[l] *= (sc[l] & 0xF);
  4383. }
  4384. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4385. }
  4386. *s = sumf;
  4387. #endif
  4388. }
  4389. #endif
  4390. #if QK_K == 256
  4391. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4392. assert(n % QK_K == 0);
  4393. assert(nrc == 1);
  4394. UNUSED(nrc);
  4395. UNUSED(bx);
  4396. UNUSED(by);
  4397. UNUSED(bs);
  4398. const uint32_t kmask1 = 0x03030303;
  4399. const uint32_t kmask2 = 0x0f0f0f0f;
  4400. const block_q3_K * restrict x = vx;
  4401. const block_q8_K * restrict y = vy;
  4402. const int nb = n / QK_K;
  4403. #ifdef __ARM_NEON
  4404. uint32_t aux[3];
  4405. uint32_t utmp[4];
  4406. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4407. const int32x4_t vzero = vdupq_n_s32(0);
  4408. const uint8x16_t m0 = vdupq_n_u8(1);
  4409. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4410. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4411. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4412. const int8_t m32 = 32;
  4413. ggml_int8x16x4_t q3bytes;
  4414. float sum = 0;
  4415. for (int i = 0; i < nb; ++i) {
  4416. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4417. const uint8_t * restrict q3 = x[i].qs;
  4418. const uint8_t * restrict qh = x[i].hmask;
  4419. const int8_t * restrict q8 = y[i].qs;
  4420. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4421. ggml_uint8x16x4_t q3h;
  4422. int32_t isum = 0;
  4423. // Set up scales
  4424. memcpy(aux, x[i].scales, 12);
  4425. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4426. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4427. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4428. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4429. int8_t * scale = (int8_t *)utmp;
  4430. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4431. for (int j = 0; j < QK_K/128; ++j) {
  4432. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4433. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4434. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4435. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4436. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4437. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4438. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4439. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4440. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4441. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4442. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4443. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4444. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4445. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4446. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4447. scale += 4;
  4448. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4449. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4450. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4451. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4452. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4453. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4454. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4455. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4456. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4457. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4458. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4459. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4460. scale += 4;
  4461. if (j == 0) {
  4462. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4463. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4464. }
  4465. }
  4466. sum += d * isum;
  4467. }
  4468. *s = sum;
  4469. #elif defined __AVX2__
  4470. const __m256i m3 = _mm256_set1_epi8(3);
  4471. const __m256i mone = _mm256_set1_epi8(1);
  4472. const __m128i m32 = _mm_set1_epi8(32);
  4473. __m256 acc = _mm256_setzero_ps();
  4474. uint32_t aux[3];
  4475. for (int i = 0; i < nb; ++i) {
  4476. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4477. const uint8_t * restrict q3 = x[i].qs;
  4478. const int8_t * restrict q8 = y[i].qs;
  4479. // Set up scales
  4480. memcpy(aux, x[i].scales, 12);
  4481. __m128i scales128 = _mm_set_epi32(
  4482. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4483. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4484. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4485. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4486. scales128 = _mm_sub_epi8(scales128, m32);
  4487. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4488. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4489. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4490. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4491. // high bit
  4492. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4493. // integer accumulator
  4494. __m256i sumi = _mm256_setzero_si256();
  4495. int bit = 0;
  4496. int is = 0;
  4497. for (int j = 0; j < QK_K/128; ++j) {
  4498. // load low 2 bits
  4499. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4500. // prepare low and high bits
  4501. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4502. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4503. ++bit;
  4504. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4505. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4506. ++bit;
  4507. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4508. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4509. ++bit;
  4510. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4511. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4512. ++bit;
  4513. // load Q8 quants
  4514. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4515. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4516. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4517. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4518. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4519. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4520. // and 2 if the high bit was set)
  4521. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4522. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4523. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4524. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4525. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4526. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4527. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4528. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4529. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4530. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4531. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4532. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4533. // multiply with scales
  4534. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4535. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4536. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4537. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4538. // accumulate
  4539. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4540. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4541. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4542. }
  4543. // multiply with block scale and accumulate
  4544. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4545. }
  4546. *s = hsum_float_8(acc);
  4547. #elif defined __AVX__
  4548. const __m128i m3 = _mm_set1_epi8(3);
  4549. const __m128i mone = _mm_set1_epi8(1);
  4550. const __m128i m32 = _mm_set1_epi8(32);
  4551. const __m128i m2 = _mm_set1_epi8(2);
  4552. __m256 acc = _mm256_setzero_ps();
  4553. const uint32_t *aux;
  4554. for (int i = 0; i < nb; ++i) {
  4555. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4556. const uint8_t * restrict q3 = x[i].qs;
  4557. const int8_t * restrict q8 = y[i].qs;
  4558. // Set up scales
  4559. aux = (const uint32_t *)x[i].scales;
  4560. __m128i scales128 = _mm_set_epi32(
  4561. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4562. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4563. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4564. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4565. scales128 = _mm_sub_epi8(scales128, m32);
  4566. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4567. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4568. const __m128i scales[2] = { scales_0, scales_1 };
  4569. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4570. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4571. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4572. // integer accumulator
  4573. __m128i sumi_0 = _mm_setzero_si128();
  4574. __m128i sumi_1 = _mm_setzero_si128();
  4575. for (int j = 0; j < QK_K/128; ++j) {
  4576. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4577. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4578. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4579. // prepare low and high bits
  4580. const int bit = j << 2;
  4581. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4582. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4583. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4584. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4585. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4586. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4587. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4588. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4589. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4590. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4591. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4592. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4593. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4594. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4595. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4596. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4597. // load Q8 quants from block_q8_K.qs[QK_K]
  4598. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4599. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4600. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4601. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4602. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4603. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4604. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4605. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4606. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4607. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4608. // and 2 if the high bit was set)
  4609. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4610. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4611. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4612. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4613. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4614. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4615. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4616. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4617. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4618. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4619. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4620. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4621. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4622. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4623. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4624. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4625. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4626. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4627. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4628. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4629. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4630. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4631. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4632. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4633. // multiply with scales
  4634. __m128i shuffle = _mm_set1_epi16(0x0100);
  4635. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4636. shuffle = _mm_add_epi16(shuffle, m2);
  4637. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4638. shuffle = _mm_add_epi16(shuffle, m2);
  4639. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4640. shuffle = _mm_add_epi16(shuffle, m2);
  4641. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4642. shuffle = _mm_add_epi16(shuffle, m2);
  4643. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4644. shuffle = _mm_add_epi16(shuffle, m2);
  4645. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4646. shuffle = _mm_add_epi16(shuffle, m2);
  4647. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4648. shuffle = _mm_add_epi16(shuffle, m2);
  4649. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4650. // accumulate
  4651. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4652. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4653. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4654. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4655. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4656. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4657. }
  4658. // multiply with block scale and accumulate
  4659. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4660. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4661. }
  4662. *s = hsum_float_8(acc);
  4663. #elif defined __riscv_v_intrinsic
  4664. uint32_t aux[3];
  4665. uint32_t utmp[4];
  4666. float sumf = 0;
  4667. for (int i = 0; i < nb; ++i) {
  4668. const uint8_t * restrict q3 = x[i].qs;
  4669. const uint8_t * restrict qh = x[i].hmask;
  4670. const int8_t * restrict q8 = y[i].qs;
  4671. memcpy(aux, x[i].scales, 12);
  4672. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4673. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4674. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4675. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4676. int8_t * scale = (int8_t *)utmp;
  4677. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4678. size_t vl = 32;
  4679. uint8_t m = 1;
  4680. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4681. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4682. int sum_t = 0;
  4683. for (int j = 0; j < QK_K; j += 128) {
  4684. vl = 32;
  4685. // load Q3
  4686. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4687. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4688. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4689. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4690. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4691. // compute mask for subtraction
  4692. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4693. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4694. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4695. m <<= 1;
  4696. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4697. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4698. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4699. m <<= 1;
  4700. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4701. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4702. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4703. m <<= 1;
  4704. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4705. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4706. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4707. m <<= 1;
  4708. // load Q8 and take product with Q3
  4709. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4710. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4711. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4712. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4713. vl = 16;
  4714. // retrieve lane to multiply with scale
  4715. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4716. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4717. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4718. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4719. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4720. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4721. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4722. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4723. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4724. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4725. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4726. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4727. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4728. q3 += 32; q8 += 128; scale += 8;
  4729. }
  4730. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4731. sumf += d*sum_t;
  4732. }
  4733. *s = sumf;
  4734. #else
  4735. // scalar version
  4736. // This function is written like this so the compiler can manage to vectorize most of it
  4737. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4738. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4739. // The ideal situation would be if we could just write the code once, and the compiler would
  4740. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4741. // write vectorized versions for AVX, ARM_NEON, etc.
  4742. int8_t aux8[QK_K];
  4743. int16_t aux16[8];
  4744. float sums [8];
  4745. int32_t aux32[8];
  4746. memset(sums, 0, 8*sizeof(float));
  4747. uint32_t auxs[4];
  4748. const int8_t * scales = (const int8_t*)auxs;
  4749. float sumf = 0;
  4750. for (int i = 0; i < nb; ++i) {
  4751. const uint8_t * restrict q3 = x[i].qs;
  4752. const uint8_t * restrict hm = x[i].hmask;
  4753. const int8_t * restrict q8 = y[i].qs;
  4754. memset(aux32, 0, 8*sizeof(int32_t));
  4755. int8_t * restrict a = aux8;
  4756. uint8_t m = 1;
  4757. for (int j = 0; j < QK_K; j += 128) {
  4758. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4759. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4760. a += 32; m <<= 1;
  4761. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4762. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4763. a += 32; m <<= 1;
  4764. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4765. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4766. a += 32; m <<= 1;
  4767. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4768. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4769. a += 32; m <<= 1;
  4770. q3 += 32;
  4771. }
  4772. a = aux8;
  4773. memcpy(auxs, x[i].scales, 12);
  4774. uint32_t tmp = auxs[2];
  4775. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4776. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4777. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4778. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4779. for (int j = 0; j < QK_K/16; ++j) {
  4780. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4781. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4782. q8 += 8; a += 8;
  4783. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4784. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4785. q8 += 8; a += 8;
  4786. }
  4787. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4788. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4789. }
  4790. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4791. *s = sumf;
  4792. #endif
  4793. }
  4794. #else
  4795. void ggml_vec_dot_q3_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  4796. assert(n % QK_K == 0);
  4797. assert(nrc == 1);
  4798. UNUSED(nrc);
  4799. UNUSED(bx);
  4800. UNUSED(by);
  4801. UNUSED(bs);
  4802. const block_q3_K * restrict x = vx;
  4803. const block_q8_K * restrict y = vy;
  4804. const int nb = n / QK_K;
  4805. #ifdef __ARM_NEON
  4806. const int32x4_t vzero = vdupq_n_s32(0);
  4807. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4808. const uint8x16_t mh = vdupq_n_u8(4);
  4809. ggml_int8x16x4_t q3bytes;
  4810. uint16_t aux16[2];
  4811. int8_t * scales = (int8_t *)aux16;
  4812. float sum = 0;
  4813. for (int i = 0; i < nb; ++i) {
  4814. ggml_uint8x16x4_t q3h;
  4815. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4816. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4817. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4818. const uint16_t a = *(const uint16_t *)x[i].scales;
  4819. aux16[0] = a & 0x0f0f;
  4820. aux16[1] = (a >> 4) & 0x0f0f;
  4821. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4822. int32_t isum = -4*(scales[0] * y[i].bsums[0] + scales[2] * y[i].bsums[1] + scales[1] * y[i].bsums[2] + scales[3] * y[i].bsums[3]);
  4823. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4824. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4825. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4826. q3h.val[1] = vandq_u8(mh, htmp);
  4827. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4828. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4829. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4830. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4831. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4832. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4833. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4834. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4835. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4836. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4837. sum += d * isum;
  4838. }
  4839. *s = sum;
  4840. #elif defined __AVX2__
  4841. const __m256i m3 = _mm256_set1_epi8(3);
  4842. const __m256i m1 = _mm256_set1_epi8(1);
  4843. __m256 acc = _mm256_setzero_ps();
  4844. uint64_t aux64;
  4845. uint16_t aux16[2];
  4846. const int8_t * aux8 = (const int8_t *)aux16;
  4847. for (int i = 0; i < nb; ++i) {
  4848. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4849. const uint8_t * restrict q3 = x[i].qs;
  4850. const int8_t * restrict q8 = y[i].qs;
  4851. const uint16_t a = *(const uint16_t *)x[i].scales;
  4852. aux16[0] = a & 0x0f0f;
  4853. aux16[1] = (a >> 4) & 0x0f0f;
  4854. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4855. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4856. memcpy(&aux64, x[i].hmask, 8);
  4857. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4858. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4859. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4860. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4861. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4862. // load low 2 bits
  4863. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4864. // prepare low and high bits
  4865. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4866. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4867. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4868. // load Q8 quants
  4869. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4870. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4871. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4872. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4873. // and 2 if the high bit was set)
  4874. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4875. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4876. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4877. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4878. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4879. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4880. // multiply with scales
  4881. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4882. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4883. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4884. // multiply with block scale and accumulate
  4885. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4886. }
  4887. *s = hsum_float_8(acc);
  4888. #elif defined __AVX__
  4889. const __m128i m3 = _mm_set1_epi8(3);
  4890. const __m128i m1 = _mm_set1_epi8(1);
  4891. __m256 acc = _mm256_setzero_ps();
  4892. uint64_t aux64;
  4893. uint16_t aux16[2];
  4894. const int8_t * aux8 = (const int8_t *)aux16;
  4895. for (int i = 0; i < nb; ++i) {
  4896. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4897. const uint8_t * restrict q3 = x[i].qs;
  4898. const int8_t * restrict q8 = y[i].qs;
  4899. const uint16_t a = *(const uint16_t *)x[i].scales;
  4900. aux16[0] = a & 0x0f0f;
  4901. aux16[1] = (a >> 4) & 0x0f0f;
  4902. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4903. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4904. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4905. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4906. memcpy(&aux64, x[i].hmask, 8);
  4907. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4908. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4909. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4910. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4911. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4912. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4913. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4914. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4915. // load low 2 bits
  4916. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4917. // prepare low and high bits
  4918. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4919. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4920. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4921. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4922. // load Q8 quants
  4923. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4924. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4925. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4926. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4927. // and 2 if the high bit was set)
  4928. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4929. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4930. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4931. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4932. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4933. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4934. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4935. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4936. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4937. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4938. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4939. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4940. // multiply with scales
  4941. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4942. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4943. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4944. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4945. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4946. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4947. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4948. // multiply with block scale and accumulate
  4949. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4950. }
  4951. *s = hsum_float_8(acc);
  4952. #elif defined __riscv_v_intrinsic
  4953. uint16_t aux16[2];
  4954. int8_t * scales = (int8_t *)aux16;
  4955. float sumf = 0;
  4956. for (int i = 0; i < nb; ++i) {
  4957. const uint8_t * restrict q3 = x[i].qs;
  4958. const int8_t * restrict q8 = y[i].qs;
  4959. const uint16_t a = *(const uint16_t *)x[i].scales;
  4960. aux16[0] = a & 0x0f0f;
  4961. aux16[1] = (a >> 4) & 0x0f0f;
  4962. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4963. int32_t isum = -4*(scales[0] * y[i].bsums[0] + scales[2] * y[i].bsums[1] + scales[1] * y[i].bsums[2] + scales[3] * y[i].bsums[3]);
  4964. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4965. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4966. // load qh
  4967. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4968. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4969. size_t vl = 16;
  4970. // extend and combine both qh_x1 and qh_x2
  4971. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4972. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4973. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4974. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4975. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4976. // load Q3
  4977. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4978. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4979. vuint8mf2_t q3h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 2, vl), 0x3, vl), qh_1, vl);
  4980. vuint8mf2_t q3h_2 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 4, vl), 0x3, vl), qh_2, vl);
  4981. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4982. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4983. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4984. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4985. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4986. // load Q8 and take product with Q3
  4987. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4988. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4989. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4990. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4991. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4992. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4993. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4994. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4995. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  4996. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  4997. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  4998. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  4999. sumf += d * isum;
  5000. }
  5001. *s = sumf;
  5002. #else
  5003. int8_t aux8[QK_K];
  5004. int16_t aux16[8];
  5005. float sums [8];
  5006. int32_t aux32[8];
  5007. int32_t scales[4];
  5008. memset(sums, 0, 8*sizeof(float));
  5009. float sumf = 0;
  5010. for (int i = 0; i < nb; ++i) {
  5011. const uint8_t * restrict q3 = x[i].qs;
  5012. const uint8_t * restrict hm = x[i].hmask;
  5013. const int8_t * restrict q8 = y[i].qs;
  5014. int8_t * restrict a = aux8;
  5015. for (int l = 0; l < 8; ++l) {
  5016. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5017. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5018. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5019. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5020. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5021. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5022. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5023. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5024. }
  5025. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5026. scales[1] = (x[i].scales[0] >> 4) - 8;
  5027. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5028. scales[3] = (x[i].scales[1] >> 4) - 8;
  5029. memset(aux32, 0, 8*sizeof(int32_t));
  5030. for (int j = 0; j < QK_K/16; ++j) {
  5031. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5032. q8 += 8; a += 8;
  5033. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5034. q8 += 8; a += 8;
  5035. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5036. }
  5037. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5038. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5039. }
  5040. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5041. *s = sumf;
  5042. #endif
  5043. }
  5044. #endif
  5045. #if QK_K == 256
  5046. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5047. assert(n % QK_K == 0);
  5048. assert(nrc == 1);
  5049. UNUSED(nrc);
  5050. UNUSED(bx);
  5051. UNUSED(by);
  5052. UNUSED(bs);
  5053. const block_q4_K * restrict x = vx;
  5054. const block_q8_K * restrict y = vy;
  5055. const int nb = n / QK_K;
  5056. static const uint32_t kmask1 = 0x3f3f3f3f;
  5057. static const uint32_t kmask2 = 0x0f0f0f0f;
  5058. static const uint32_t kmask3 = 0x03030303;
  5059. uint32_t utmp[4];
  5060. #ifdef __ARM_NEON
  5061. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5062. const int32x4_t mzero = vdupq_n_s32(0);
  5063. ggml_int8x16x2_t q4bytes;
  5064. ggml_int8x16x2_t q8bytes;
  5065. float sumf = 0;
  5066. for (int i = 0; i < nb; ++i) {
  5067. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5068. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5069. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5070. memcpy(utmp, x[i].scales, 12);
  5071. uint32x2_t mins8 = { 0 };
  5072. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5073. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5074. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5075. utmp[0] &= kmask1;
  5076. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5077. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5078. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5079. sumf -= dmin * vaddvq_s32(prod);
  5080. const uint8_t * scales = (const uint8_t *)utmp;
  5081. const uint8_t * restrict q4 = x[i].qs;
  5082. const int8_t * restrict q8 = y[i].qs;
  5083. int32_t sumi1 = 0;
  5084. int32_t sumi2 = 0;
  5085. for (int j = 0; j < QK_K/64; ++j) {
  5086. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5087. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5088. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5089. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5090. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5091. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5092. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5093. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5094. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5095. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5096. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5097. }
  5098. sumf += d * (sumi1 + sumi2);
  5099. }
  5100. *s = sumf;
  5101. #elif defined __AVX2__
  5102. const __m256i m4 = _mm256_set1_epi8(0xF);
  5103. __m256 acc = _mm256_setzero_ps();
  5104. __m128 acc_m = _mm_setzero_ps();
  5105. for (int i = 0; i < nb; ++i) {
  5106. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5107. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5108. memcpy(utmp, x[i].scales, 12);
  5109. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5110. const uint32_t uaux = utmp[1] & kmask1;
  5111. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5112. utmp[2] = uaux;
  5113. utmp[0] &= kmask1;
  5114. const uint8_t * restrict q4 = x[i].qs;
  5115. const int8_t * restrict q8 = y[i].qs;
  5116. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5117. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5118. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5119. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5120. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5121. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5122. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5123. __m256i sumi = _mm256_setzero_si256();
  5124. for (int j = 0; j < QK_K/64; ++j) {
  5125. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5126. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5127. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5128. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5129. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5130. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5131. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5132. p16l = _mm256_madd_epi16(scale_l, p16l);
  5133. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5134. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5135. p16h = _mm256_madd_epi16(scale_h, p16h);
  5136. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5137. sumi = _mm256_add_epi32(sumi, sumj);
  5138. }
  5139. __m256 vd = _mm256_set1_ps(d);
  5140. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5141. }
  5142. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5143. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5144. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5145. #elif defined __AVX__
  5146. const __m128i m4 = _mm_set1_epi8(0xF);
  5147. const __m128i m2 = _mm_set1_epi8(0x2);
  5148. __m256 acc = _mm256_setzero_ps();
  5149. __m128 acc_m = _mm_setzero_ps();
  5150. for (int i = 0; i < nb; ++i) {
  5151. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5152. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5153. const uint8_t * restrict q4 = x[i].qs;
  5154. const int8_t * restrict q8 = y[i].qs;
  5155. memcpy(utmp, x[i].scales, 12);
  5156. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5157. const uint32_t uaux = utmp[1] & kmask1;
  5158. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5159. utmp[2] = uaux;
  5160. utmp[0] &= kmask1;
  5161. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5162. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5163. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5164. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5165. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5166. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5167. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5168. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5169. __m128i sumi_0 = _mm_setzero_si128();
  5170. __m128i sumi_1 = _mm_setzero_si128();
  5171. __m128i shuffle = _mm_set1_epi16(0x0100);
  5172. for (int j = 0; j < QK_K/64; ++j) {
  5173. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5174. shuffle = _mm_add_epi16(shuffle, m2);
  5175. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5176. shuffle = _mm_add_epi16(shuffle, m2);
  5177. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5178. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5179. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5180. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5181. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5182. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5183. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5184. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5185. p16l = _mm_madd_epi16(scale_l, p16l);
  5186. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5187. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5188. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5189. p16l = _mm_madd_epi16(scale_l, p16l);
  5190. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5191. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5192. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5193. p16h = _mm_madd_epi16(scale_h, p16h);
  5194. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5195. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5196. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5197. p16h = _mm_madd_epi16(scale_h, p16h);
  5198. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5199. }
  5200. __m256 vd = _mm256_set1_ps(d);
  5201. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5202. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5203. }
  5204. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5205. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5206. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5207. #elif defined __riscv_v_intrinsic
  5208. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5209. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5210. float sumf = 0;
  5211. for (int i = 0; i < nb; ++i) {
  5212. size_t vl = 8;
  5213. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5214. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5215. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5216. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5217. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5218. memcpy(utmp, x[i].scales, 12);
  5219. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5220. const uint32_t uaux = utmp[1] & kmask1;
  5221. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5222. utmp[2] = uaux;
  5223. utmp[0] &= kmask1;
  5224. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5225. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5226. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5227. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5228. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5229. const uint8_t * restrict q4 = x[i].qs;
  5230. const int8_t * restrict q8 = y[i].qs;
  5231. vl = 32;
  5232. int32_t sum_1 = 0;
  5233. int32_t sum_2 = 0;
  5234. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5235. for (int j = 0; j < QK_K/64; ++j) {
  5236. // load Q4
  5237. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5238. // load Q8 and multiply it with lower Q4 nibble
  5239. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5240. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5241. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5242. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5243. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5244. // load Q8 and multiply it with upper Q4 nibble
  5245. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5246. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5247. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5248. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5249. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5250. q4 += 32; q8 += 64;
  5251. }
  5252. sumf += d*(sum_1 + sum_2);
  5253. }
  5254. *s = sumf;
  5255. #else
  5256. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5257. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5258. int8_t aux8[QK_K];
  5259. int16_t aux16[8];
  5260. float sums [8];
  5261. int32_t aux32[8];
  5262. memset(sums, 0, 8*sizeof(float));
  5263. float sumf = 0;
  5264. for (int i = 0; i < nb; ++i) {
  5265. const uint8_t * restrict q4 = x[i].qs;
  5266. const int8_t * restrict q8 = y[i].qs;
  5267. memset(aux32, 0, 8*sizeof(int32_t));
  5268. int8_t * restrict a = aux8;
  5269. for (int j = 0; j < QK_K/64; ++j) {
  5270. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5271. a += 32;
  5272. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5273. a += 32; q4 += 32;
  5274. }
  5275. memcpy(utmp, x[i].scales, 12);
  5276. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5277. const uint32_t uaux = utmp[1] & kmask1;
  5278. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5279. utmp[2] = uaux;
  5280. utmp[0] &= kmask1;
  5281. int sumi = 0;
  5282. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5283. a = aux8;
  5284. int is = 0;
  5285. for (int j = 0; j < QK_K/32; ++j) {
  5286. int32_t scale = scales[is++];
  5287. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5288. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5289. q8 += 8; a += 8;
  5290. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5291. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5292. q8 += 8; a += 8;
  5293. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5294. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5295. q8 += 8; a += 8;
  5296. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5297. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5298. q8 += 8; a += 8;
  5299. }
  5300. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5301. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5302. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5303. sumf -= dmin * sumi;
  5304. }
  5305. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5306. *s = sumf;
  5307. #endif
  5308. }
  5309. #else
  5310. void ggml_vec_dot_q4_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5311. assert(n % QK_K == 0);
  5312. assert(nrc == 1);
  5313. UNUSED(nrc);
  5314. UNUSED(bx);
  5315. UNUSED(by);
  5316. UNUSED(bs);
  5317. const block_q4_K * restrict x = vx;
  5318. const block_q8_K * restrict y = vy;
  5319. const int nb = n / QK_K;
  5320. #ifdef __ARM_NEON
  5321. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5322. const int32x4_t mzero = vdupq_n_s32(0);
  5323. float sumf = 0;
  5324. ggml_int8x16x2_t q4bytes;
  5325. ggml_int8x16x4_t q8bytes;
  5326. float sum_mins = 0.f;
  5327. uint16_t aux16[2];
  5328. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5329. for (int i = 0; i < nb; ++i) {
  5330. const uint8_t * restrict q4 = x[i].qs;
  5331. const int8_t * restrict q8 = y[i].qs;
  5332. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5333. aux16[0] = a[0] & 0x0f0f;
  5334. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5335. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5336. sum_mins += y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * summi;
  5337. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5338. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5339. q8bytes = ggml_vld1q_s8_x4(q8);
  5340. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5341. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5342. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5343. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5344. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5345. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5346. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5347. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5348. sumf += d * (sumi1 + sumi2);
  5349. }
  5350. *s = sumf - sum_mins;
  5351. #elif defined __AVX2__
  5352. const __m256i m4 = _mm256_set1_epi8(0xF);
  5353. __m256 acc = _mm256_setzero_ps();
  5354. float summs = 0;
  5355. uint16_t aux16[2];
  5356. const uint8_t * scales = (const uint8_t *)aux16;
  5357. for (int i = 0; i < nb; ++i) {
  5358. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5359. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5360. const __m256 vd = _mm256_set1_ps(d);
  5361. const uint16_t * a = (const uint16_t *)x[i].scales;
  5362. aux16[0] = a[0] & 0x0f0f;
  5363. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5364. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5365. const uint8_t * restrict q4 = x[i].qs;
  5366. const int8_t * restrict q8 = y[i].qs;
  5367. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5368. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5369. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5370. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5371. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5372. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5373. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5374. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5375. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5376. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5377. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5378. }
  5379. *s = hsum_float_8(acc) - summs;
  5380. #elif defined __AVX__
  5381. const __m128i m4 = _mm_set1_epi8(0xF);
  5382. __m256 acc = _mm256_setzero_ps();
  5383. float summs = 0;
  5384. uint16_t aux16[2];
  5385. const uint8_t * scales = (const uint8_t *)aux16;
  5386. for (int i = 0; i < nb; ++i) {
  5387. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5388. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5389. const __m256 vd = _mm256_set1_ps(d);
  5390. const uint16_t * a = (const uint16_t *)x[i].scales;
  5391. aux16[0] = a[0] & 0x0f0f;
  5392. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5393. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5394. const uint8_t * restrict q4 = x[i].qs;
  5395. const int8_t * restrict q8 = y[i].qs;
  5396. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5397. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5398. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5399. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5400. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5401. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5402. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5403. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5404. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5405. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5406. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5407. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5408. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5409. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5410. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5411. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5412. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5413. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5414. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5415. }
  5416. *s = hsum_float_8(acc) - summs;
  5417. #elif defined __riscv_v_intrinsic
  5418. uint16_t s16[2];
  5419. const uint8_t * restrict scales = (const uint8_t *)s16;
  5420. float sumf = 0;
  5421. for (int i = 0; i < nb; ++i) {
  5422. const uint8_t * restrict q4 = x[i].qs;
  5423. const int8_t * restrict q8 = y[i].qs;
  5424. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5425. s16[0] = b[0] & 0x0f0f;
  5426. s16[1] = (b[0] >> 4) & 0x0f0f;
  5427. sumf -= y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5428. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5429. size_t vl = 32;
  5430. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5431. // load Q4
  5432. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5433. // load Q8 and multiply it with lower Q4 nibble
  5434. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5435. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5436. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5437. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5438. // load Q8 and multiply it with upper Q4 nibble
  5439. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5440. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5441. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5442. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5443. }
  5444. *s = sumf;
  5445. #else
  5446. uint8_t aux8[QK_K];
  5447. int16_t aux16[16];
  5448. float sums [8];
  5449. memset(sums, 0, 8*sizeof(float));
  5450. uint16_t s16[2];
  5451. const uint8_t * restrict scales = (const uint8_t *)s16;
  5452. float sumf = 0;
  5453. for (int i = 0; i < nb; ++i) {
  5454. const uint8_t * restrict q4 = x[i].qs;
  5455. const int8_t * restrict q8 = y[i].qs;
  5456. uint8_t * restrict a = aux8;
  5457. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5458. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5459. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5460. s16[0] = b[0] & 0x0f0f;
  5461. s16[1] = (b[0] >> 4) & 0x0f0f;
  5462. sumf -= y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5463. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5464. for (int j = 0; j < QK_K/32; ++j) {
  5465. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5466. q8 += 16; a += 16;
  5467. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5468. q8 += 16; a += 16;
  5469. const float dl = d * scales[j];
  5470. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5471. }
  5472. }
  5473. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5474. *s = sumf;
  5475. #endif
  5476. }
  5477. #endif
  5478. #if QK_K == 256
  5479. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5480. assert(n % QK_K == 0);
  5481. assert(nrc == 1);
  5482. UNUSED(nrc);
  5483. UNUSED(bx);
  5484. UNUSED(by);
  5485. UNUSED(bs);
  5486. const block_q5_K * restrict x = vx;
  5487. const block_q8_K * restrict y = vy;
  5488. const int nb = n / QK_K;
  5489. static const uint32_t kmask1 = 0x3f3f3f3f;
  5490. static const uint32_t kmask2 = 0x0f0f0f0f;
  5491. static const uint32_t kmask3 = 0x03030303;
  5492. uint32_t utmp[4];
  5493. #ifdef __ARM_NEON
  5494. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5495. const uint8x16_t mone = vdupq_n_u8(1);
  5496. const uint8x16_t mtwo = vdupq_n_u8(2);
  5497. const int32x4_t mzero = vdupq_n_s32(0);
  5498. ggml_int8x16x4_t q5bytes;
  5499. float sumf = 0;
  5500. for (int i = 0; i < nb; ++i) {
  5501. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5502. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5503. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5504. memcpy(utmp, x[i].scales, 12);
  5505. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5506. const uint32_t uaux = utmp[1] & kmask1;
  5507. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5508. utmp[2] = uaux;
  5509. utmp[0] &= kmask1;
  5510. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5511. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5512. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5513. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5514. int32_t sumi_mins = vaddvq_s32(prod);
  5515. const uint8_t * scales = (const uint8_t *)utmp;
  5516. const uint8_t * restrict q5 = x[i].qs;
  5517. const uint8_t * restrict qh = x[i].qh;
  5518. const int8_t * restrict q8 = y[i].qs;
  5519. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5520. ggml_uint8x16x4_t q5h;
  5521. int32_t sumi = 0;
  5522. for (int j = 0; j < QK_K/64; ++j) {
  5523. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5524. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5525. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5526. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5527. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5528. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5529. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5530. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5531. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5532. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5533. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5534. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5535. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5536. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5537. }
  5538. sumf += d * sumi - dmin * sumi_mins;
  5539. }
  5540. *s = sumf;
  5541. #elif defined __AVX2__
  5542. const __m256i m4 = _mm256_set1_epi8(0xF);
  5543. const __m128i mzero = _mm_setzero_si128();
  5544. const __m256i mone = _mm256_set1_epi8(1);
  5545. __m256 acc = _mm256_setzero_ps();
  5546. float summs = 0.f;
  5547. for (int i = 0; i < nb; ++i) {
  5548. const uint8_t * restrict q5 = x[i].qs;
  5549. const int8_t * restrict q8 = y[i].qs;
  5550. #if QK_K == 256
  5551. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5552. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5553. memcpy(utmp, x[i].scales, 12);
  5554. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5555. const uint32_t uaux = utmp[1] & kmask1;
  5556. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5557. utmp[2] = uaux;
  5558. utmp[0] &= kmask1;
  5559. #else
  5560. // TODO
  5561. const float d = 0, dmin = 0;
  5562. #endif
  5563. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5564. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5565. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5566. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5567. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5568. summs += dmin * _mm_extract_epi32(hsum, 0);
  5569. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5570. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5571. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5572. __m256i hmask = mone;
  5573. __m256i sumi = _mm256_setzero_si256();
  5574. int bit = 0;
  5575. for (int j = 0; j < QK_K/64; ++j) {
  5576. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5577. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5578. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5579. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5580. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5581. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5582. hmask = _mm256_slli_epi16(hmask, 1);
  5583. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5584. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5585. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5586. hmask = _mm256_slli_epi16(hmask, 1);
  5587. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5588. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5589. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5590. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5591. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5592. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5593. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5594. }
  5595. __m256 vd = _mm256_set1_ps(d);
  5596. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5597. }
  5598. *s = hsum_float_8(acc) + summs;
  5599. #elif defined __AVX__
  5600. const __m128i m4 = _mm_set1_epi8(0xF);
  5601. const __m128i mzero = _mm_setzero_si128();
  5602. const __m128i mone = _mm_set1_epi8(1);
  5603. const __m128i m2 = _mm_set1_epi8(2);
  5604. __m256 acc = _mm256_setzero_ps();
  5605. float summs = 0.f;
  5606. for (int i = 0; i < nb; ++i) {
  5607. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5608. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5609. const uint8_t * restrict q5 = x[i].qs;
  5610. const int8_t * restrict q8 = y[i].qs;
  5611. memcpy(utmp, x[i].scales, 12);
  5612. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5613. const uint32_t uaux = utmp[1] & kmask1;
  5614. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5615. utmp[2] = uaux;
  5616. utmp[0] &= kmask1;
  5617. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5618. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5619. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5620. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5621. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5622. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5623. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5624. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5625. summs += dmin * _mm_extract_epi32(hsum, 0);
  5626. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5627. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5628. __m128i hmask = mone;
  5629. __m128i sumi_0 = _mm_setzero_si128();
  5630. __m128i sumi_1 = _mm_setzero_si128();
  5631. int bit = 0;
  5632. __m128i shuffle = _mm_set1_epi16(0x0100);
  5633. for (int j = 0; j < QK_K/64; ++j) {
  5634. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5635. shuffle = _mm_add_epi16(shuffle, m2);
  5636. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5637. shuffle = _mm_add_epi16(shuffle, m2);
  5638. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5639. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5640. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5641. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5642. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5643. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5644. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5645. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5646. hmask = _mm_slli_epi16(hmask, 1);
  5647. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5648. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5649. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5650. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5651. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5652. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5653. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5654. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5655. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5656. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5657. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5658. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5659. hmask = _mm_slli_epi16(hmask, 1);
  5660. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5661. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5662. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5663. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5664. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5665. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5666. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5667. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5668. }
  5669. __m256 vd = _mm256_set1_ps(d);
  5670. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5671. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5672. }
  5673. *s = hsum_float_8(acc) + summs;
  5674. #elif defined __riscv_v_intrinsic
  5675. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5676. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5677. float sumf = 0;
  5678. float sums = 0.0;
  5679. size_t vl;
  5680. for (int i = 0; i < nb; ++i) {
  5681. vl = 8;
  5682. const uint8_t * restrict q5 = x[i].qs;
  5683. const uint8_t * restrict hm = x[i].qh;
  5684. const int8_t * restrict q8 = y[i].qs;
  5685. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5686. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5687. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5688. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5689. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5690. memcpy(utmp, x[i].scales, 12);
  5691. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5692. const uint32_t uaux = utmp[1] & kmask1;
  5693. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5694. utmp[2] = uaux;
  5695. utmp[0] &= kmask1;
  5696. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5697. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5698. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5699. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5700. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5701. vl = 32;
  5702. int32_t aux32 = 0;
  5703. int is = 0;
  5704. uint8_t m = 1;
  5705. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5706. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5707. for (int j = 0; j < QK_K/64; ++j) {
  5708. // load Q5 and Q8
  5709. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5710. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5711. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5712. // compute mask for addition
  5713. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5714. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5715. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5716. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5717. m <<= 1;
  5718. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5719. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5720. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5721. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5722. m <<= 1;
  5723. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5724. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5725. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5726. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5727. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5728. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5729. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5730. q5 += 32; q8 += 64;
  5731. }
  5732. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5733. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5734. }
  5735. *s = sumf+sums;
  5736. #else
  5737. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5738. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5739. int8_t aux8[QK_K];
  5740. int16_t aux16[8];
  5741. float sums [8];
  5742. int32_t aux32[8];
  5743. memset(sums, 0, 8*sizeof(float));
  5744. float sumf = 0;
  5745. for (int i = 0; i < nb; ++i) {
  5746. const uint8_t * restrict q4 = x[i].qs;
  5747. const uint8_t * restrict hm = x[i].qh;
  5748. const int8_t * restrict q8 = y[i].qs;
  5749. memset(aux32, 0, 8*sizeof(int32_t));
  5750. int8_t * restrict a = aux8;
  5751. uint8_t m = 1;
  5752. for (int j = 0; j < QK_K/64; ++j) {
  5753. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5754. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5755. a += 32; m <<= 1;
  5756. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5757. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5758. a += 32; m <<= 1;
  5759. q4 += 32;
  5760. }
  5761. memcpy(utmp, x[i].scales, 12);
  5762. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5763. const uint32_t uaux = utmp[1] & kmask1;
  5764. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5765. utmp[2] = uaux;
  5766. utmp[0] &= kmask1;
  5767. int sumi = 0;
  5768. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5769. a = aux8;
  5770. int is = 0;
  5771. for (int j = 0; j < QK_K/32; ++j) {
  5772. int32_t scale = scales[is++];
  5773. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5774. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5775. q8 += 8; a += 8;
  5776. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5777. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5778. q8 += 8; a += 8;
  5779. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5780. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5781. q8 += 8; a += 8;
  5782. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5783. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5784. q8 += 8; a += 8;
  5785. }
  5786. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5787. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5788. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5789. sumf -= dmin * sumi;
  5790. }
  5791. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5792. *s = sumf;
  5793. #endif
  5794. }
  5795. #else
  5796. void ggml_vec_dot_q5_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5797. assert(n % QK_K == 0);
  5798. assert(nrc == 1);
  5799. UNUSED(nrc);
  5800. UNUSED(bx);
  5801. UNUSED(by);
  5802. UNUSED(bs);
  5803. const block_q5_K * restrict x = vx;
  5804. const block_q8_K * restrict y = vy;
  5805. const int nb = n / QK_K;
  5806. #ifdef __ARM_NEON
  5807. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5808. const uint8x16_t mh = vdupq_n_u8(16);
  5809. const int32x4_t mzero = vdupq_n_s32(0);
  5810. ggml_int8x16x4_t q5bytes;
  5811. ggml_uint8x16x4_t q5h;
  5812. float sumf = 0;
  5813. for (int i = 0; i < nb; ++i) {
  5814. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5815. const int8_t * sc = x[i].scales;
  5816. const uint8_t * restrict q5 = x[i].qs;
  5817. const uint8_t * restrict qh = x[i].qh;
  5818. const int8_t * restrict q8 = y[i].qs;
  5819. const uint8x8_t qhbits = vld1_u8(qh);
  5820. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5821. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5822. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5823. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5824. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5825. q5h.val[2] = vbicq_u8(mh, htmp);
  5826. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5827. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5828. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5829. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5830. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5831. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5832. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5833. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5834. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5835. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5836. }
  5837. *s = sumf;
  5838. #elif defined __AVX2__
  5839. const __m256i m4 = _mm256_set1_epi8(0xF);
  5840. const __m256i mone = _mm256_set1_epi8(1);
  5841. __m256 acc = _mm256_setzero_ps();
  5842. for (int i = 0; i < nb; ++i) {
  5843. const uint8_t * restrict q5 = x[i].qs;
  5844. const int8_t * restrict q8 = y[i].qs;
  5845. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5846. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5847. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5848. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5849. int64_t aux64;
  5850. memcpy(&aux64, x[i].qh, 8);
  5851. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5852. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5853. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5854. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5855. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5856. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5857. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5858. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5859. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5860. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5861. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5862. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5863. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5864. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5865. }
  5866. *s = hsum_float_8(acc);
  5867. #elif defined __AVX__
  5868. const __m128i m4 = _mm_set1_epi8(0xF);
  5869. const __m128i mone = _mm_set1_epi8(1);
  5870. __m256 acc = _mm256_setzero_ps();
  5871. for (int i = 0; i < nb; ++i) {
  5872. const uint8_t * restrict q5 = x[i].qs;
  5873. const int8_t * restrict q8 = y[i].qs;
  5874. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5875. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5876. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5877. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5878. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5879. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5880. int64_t aux64;
  5881. memcpy(&aux64, x[i].qh, 8);
  5882. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5883. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5884. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5885. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5886. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5887. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5888. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5889. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5890. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5891. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5892. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5893. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5894. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5895. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5896. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5897. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5898. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5899. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5900. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5901. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5902. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5903. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5904. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5905. }
  5906. *s = hsum_float_8(acc);
  5907. #elif defined __riscv_v_intrinsic
  5908. float sumf = 0;
  5909. for (int i = 0; i < nb; ++i) {
  5910. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5911. const int8_t * sc = x[i].scales;
  5912. const uint8_t * restrict q5 = x[i].qs;
  5913. const uint8_t * restrict qh = x[i].qh;
  5914. const int8_t * restrict q8 = y[i].qs;
  5915. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5916. // load qh
  5917. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5918. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5919. size_t vl = 16;
  5920. // combine both qh_1 and qh_2
  5921. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5922. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5923. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5924. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5925. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5926. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5927. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5928. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5929. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5930. // load q5
  5931. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5932. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5933. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5934. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5935. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5936. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5937. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5938. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5939. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5940. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5941. // load Q8 and multiply it with Q5
  5942. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5943. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5944. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5945. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5946. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5947. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5948. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5949. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5950. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5951. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5952. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5953. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5954. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5955. }
  5956. *s = sumf;
  5957. #else
  5958. int8_t aux8[QK_K];
  5959. int16_t aux16[16];
  5960. float sums [8];
  5961. memset(sums, 0, 8*sizeof(float));
  5962. float sumf = 0;
  5963. for (int i = 0; i < nb; ++i) {
  5964. const uint8_t * restrict q4 = x[i].qs;
  5965. const uint8_t * restrict hm = x[i].qh;
  5966. const int8_t * restrict q8 = y[i].qs;
  5967. int8_t * restrict a = aux8;
  5968. for (int l = 0; l < 32; ++l) {
  5969. a[l+ 0] = q4[l] & 0xF;
  5970. a[l+32] = q4[l] >> 4;
  5971. }
  5972. for (int is = 0; is < 8; ++is) {
  5973. uint8_t m = 1 << is;
  5974. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5975. }
  5976. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5977. const int8_t * restrict sc = x[i].scales;
  5978. for (int j = 0; j < QK_K/16; ++j) {
  5979. const float dl = d * sc[j];
  5980. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5981. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5982. q8 += 16; a += 16;
  5983. }
  5984. }
  5985. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5986. *s = sumf;
  5987. #endif
  5988. }
  5989. #endif
  5990. #if QK_K == 256
  5991. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  5992. assert(n % QK_K == 0);
  5993. assert(nrc == 1);
  5994. UNUSED(nrc);
  5995. UNUSED(bx);
  5996. UNUSED(by);
  5997. UNUSED(bs);
  5998. const block_q6_K * restrict x = vx;
  5999. const block_q8_K * restrict y = vy;
  6000. const int nb = n / QK_K;
  6001. #ifdef __ARM_NEON
  6002. float sum = 0;
  6003. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6004. const int32x4_t vzero = vdupq_n_s32(0);
  6005. //const int8x16_t m32s = vdupq_n_s8(32);
  6006. const uint8x16_t mone = vdupq_n_u8(3);
  6007. ggml_int8x16x4_t q6bytes;
  6008. ggml_uint8x16x4_t q6h;
  6009. for (int i = 0; i < nb; ++i) {
  6010. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6011. const uint8_t * restrict q6 = x[i].ql;
  6012. const uint8_t * restrict qh = x[i].qh;
  6013. const int8_t * restrict q8 = y[i].qs;
  6014. const int8_t * restrict scale = x[i].scales;
  6015. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6016. const int8x16_t scales = vld1q_s8(scale);
  6017. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6018. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6019. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6020. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6021. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6022. int32_t isum_mins = vaddvq_s32(prod);
  6023. int32_t isum = 0;
  6024. for (int j = 0; j < QK_K/128; ++j) {
  6025. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6026. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6027. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6028. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6029. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6030. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6031. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6032. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6033. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6034. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6035. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6036. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6037. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6038. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6039. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6040. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6041. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6042. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6043. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6044. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6045. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6046. scale += 4;
  6047. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6048. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6049. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6050. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6051. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6052. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6053. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6054. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6055. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6056. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6057. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6058. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6059. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6060. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6061. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6062. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6063. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6064. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6065. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6066. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6067. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6068. scale += 4;
  6069. }
  6070. //sum += isum * d_all * y[i].d;
  6071. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6072. }
  6073. *s = sum;
  6074. #elif defined __AVX2__
  6075. const __m256i m4 = _mm256_set1_epi8(0xF);
  6076. const __m256i m2 = _mm256_set1_epi8(3);
  6077. const __m256i m32s = _mm256_set1_epi8(32);
  6078. __m256 acc = _mm256_setzero_ps();
  6079. for (int i = 0; i < nb; ++i) {
  6080. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6081. const uint8_t * restrict q4 = x[i].ql;
  6082. const uint8_t * restrict qh = x[i].qh;
  6083. const int8_t * restrict q8 = y[i].qs;
  6084. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6085. __m256i sumi = _mm256_setzero_si256();
  6086. int is = 0;
  6087. for (int j = 0; j < QK_K/128; ++j) {
  6088. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6089. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6090. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6091. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6092. is += 4;
  6093. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6094. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6095. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6096. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6097. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6098. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6099. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6100. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6101. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6102. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6103. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6104. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6105. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6106. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6107. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6108. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6109. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6110. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6111. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6112. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6113. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6114. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6115. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6116. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6117. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6118. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6119. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6120. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6121. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6122. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6123. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6124. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6125. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6126. }
  6127. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6128. }
  6129. *s = hsum_float_8(acc);
  6130. #elif defined __AVX__
  6131. const __m128i m4 = _mm_set1_epi8(0xF);
  6132. const __m128i m3 = _mm_set1_epi8(3);
  6133. const __m128i m32s = _mm_set1_epi8(32);
  6134. const __m128i m2 = _mm_set1_epi8(2);
  6135. __m256 acc = _mm256_setzero_ps();
  6136. for (int i = 0; i < nb; ++i) {
  6137. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6138. const uint8_t * restrict q4 = x[i].ql;
  6139. const uint8_t * restrict qh = x[i].qh;
  6140. const int8_t * restrict q8 = y[i].qs;
  6141. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6142. __m128i sumi_0 = _mm_setzero_si128();
  6143. __m128i sumi_1 = _mm_setzero_si128();
  6144. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6145. for (int j = 0; j < QK_K/128; ++j) {
  6146. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6147. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6148. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6149. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6150. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6151. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6152. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6153. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6154. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6155. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6156. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6157. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6158. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6159. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6160. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6161. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6162. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6163. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6164. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6165. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6166. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6167. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6168. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6169. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6170. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6171. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6172. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6173. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6174. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6175. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6176. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6177. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6178. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6179. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6180. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6181. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6182. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6183. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6184. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6185. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6186. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6187. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6188. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6189. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6190. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6191. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6192. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6193. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6194. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6195. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6196. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6197. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6198. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6199. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6200. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6201. shuffle = _mm_add_epi8(shuffle, m2);
  6202. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6203. shuffle = _mm_add_epi8(shuffle, m2);
  6204. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6205. shuffle = _mm_add_epi8(shuffle, m2);
  6206. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6207. shuffle = _mm_add_epi8(shuffle, m2);
  6208. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6209. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6210. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6211. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6212. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6213. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6214. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6215. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6216. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6217. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6218. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6219. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6220. }
  6221. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6222. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6223. }
  6224. *s = hsum_float_8(acc);
  6225. #elif defined __riscv_v_intrinsic
  6226. float sumf = 0;
  6227. for (int i = 0; i < nb; ++i) {
  6228. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6229. const uint8_t * restrict q6 = x[i].ql;
  6230. const uint8_t * restrict qh = x[i].qh;
  6231. const int8_t * restrict q8 = y[i].qs;
  6232. const int8_t * restrict scale = x[i].scales;
  6233. size_t vl;
  6234. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6235. int sum_t = 0;
  6236. int is = 0;
  6237. for (int j = 0; j < QK_K/128; ++j) {
  6238. vl = 32;
  6239. // load qh
  6240. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6241. // load Q6
  6242. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6243. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6244. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6245. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6246. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6247. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6248. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6249. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6250. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6251. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6252. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6253. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6254. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6255. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6256. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6257. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6258. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6259. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6260. // load Q8 and take product
  6261. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6262. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6263. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6264. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6265. vl = 16;
  6266. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6267. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6268. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6269. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6270. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6271. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6272. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6273. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6274. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6275. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6276. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6277. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6278. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6279. q6 += 64; qh += 32; q8 += 128; is=8;
  6280. }
  6281. sumf += d * sum_t;
  6282. }
  6283. *s = sumf;
  6284. #else
  6285. int8_t aux8[QK_K];
  6286. int16_t aux16[8];
  6287. float sums [8];
  6288. int32_t aux32[8];
  6289. memset(sums, 0, 8*sizeof(float));
  6290. float sumf = 0;
  6291. for (int i = 0; i < nb; ++i) {
  6292. const uint8_t * restrict q4 = x[i].ql;
  6293. const uint8_t * restrict qh = x[i].qh;
  6294. const int8_t * restrict q8 = y[i].qs;
  6295. memset(aux32, 0, 8*sizeof(int32_t));
  6296. int8_t * restrict a = aux8;
  6297. for (int j = 0; j < QK_K; j += 128) {
  6298. for (int l = 0; l < 32; ++l) {
  6299. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6300. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6301. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6302. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6303. }
  6304. a += 128;
  6305. q4 += 64;
  6306. qh += 32;
  6307. }
  6308. a = aux8;
  6309. int is = 0;
  6310. for (int j = 0; j < QK_K/16; ++j) {
  6311. int scale = x[i].scales[is++];
  6312. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6313. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6314. q8 += 8; a += 8;
  6315. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6316. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6317. q8 += 8; a += 8;
  6318. }
  6319. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6320. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6321. }
  6322. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6323. *s = sumf;
  6324. #endif
  6325. }
  6326. #else
  6327. void ggml_vec_dot_q6_K_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6328. assert(n % QK_K == 0);
  6329. assert(nrc == 1);
  6330. UNUSED(nrc);
  6331. UNUSED(bx);
  6332. UNUSED(by);
  6333. UNUSED(bs);
  6334. const block_q6_K * restrict x = vx;
  6335. const block_q8_K * restrict y = vy;
  6336. const int nb = n / QK_K;
  6337. #ifdef __ARM_NEON
  6338. float sum = 0;
  6339. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6340. const int8x16_t m32s = vdupq_n_s8(32);
  6341. const int32x4_t vzero = vdupq_n_s32(0);
  6342. const uint8x16_t mone = vdupq_n_u8(3);
  6343. ggml_int8x16x4_t q6bytes;
  6344. ggml_uint8x16x4_t q6h;
  6345. for (int i = 0; i < nb; ++i) {
  6346. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6347. const uint8_t * restrict q6 = x[i].ql;
  6348. const uint8_t * restrict qh = x[i].qh;
  6349. const int8_t * restrict q8 = y[i].qs;
  6350. const int8_t * restrict scale = x[i].scales;
  6351. int32_t isum = 0;
  6352. uint8x16_t qhbits = vld1q_u8(qh);
  6353. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6354. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6355. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6356. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6357. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6358. shifted = vshrq_n_u8(qhbits, 4);
  6359. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6360. shifted = vshrq_n_u8(qhbits, 6);
  6361. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6362. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6363. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6364. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6365. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6366. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6367. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6368. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6369. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6370. sum += isum * d_all * y[i].d;
  6371. }
  6372. *s = sum;
  6373. #elif defined __AVX2__
  6374. const __m256i m4 = _mm256_set1_epi8(0xF);
  6375. const __m256i m2 = _mm256_set1_epi8(3);
  6376. const __m256i m32s = _mm256_set1_epi8(32);
  6377. __m256 acc = _mm256_setzero_ps();
  6378. for (int i = 0; i < nb; ++i) {
  6379. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6380. const uint8_t * restrict q4 = x[i].ql;
  6381. const uint8_t * restrict qh = x[i].qh;
  6382. const int8_t * restrict q8 = y[i].qs;
  6383. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6384. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6385. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6386. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6387. __m256i sumi = _mm256_setzero_si256();
  6388. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6389. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6390. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6391. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6392. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6393. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 6), _mm_srli_epi16(q4bitsH, 4)), m2), 4);
  6394. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6395. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6396. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6397. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6398. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6399. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6400. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6401. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6402. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6403. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6404. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6405. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6406. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6407. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6408. }
  6409. *s = hsum_float_8(acc);
  6410. #elif defined __AVX__
  6411. const __m128i m4 = _mm_set1_epi8(0xF);
  6412. const __m128i m2 = _mm_set1_epi8(3);
  6413. const __m128i m32s = _mm_set1_epi8(32);
  6414. __m256 acc = _mm256_setzero_ps();
  6415. for (int i = 0; i < nb; ++i) {
  6416. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6417. const uint8_t * restrict q4 = x[i].ql;
  6418. const uint8_t * restrict qh = x[i].qh;
  6419. const int8_t * restrict q8 = y[i].qs;
  6420. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6421. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6422. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6423. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6424. __m128i sumi_0 = _mm_setzero_si128();
  6425. __m128i sumi_1 = _mm_setzero_si128();
  6426. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6427. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6428. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6429. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6430. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6431. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6432. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6433. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6434. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6435. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6436. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6437. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6438. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6439. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6440. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6441. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6442. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6443. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6444. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6445. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6446. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6447. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6448. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6449. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6450. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6451. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6452. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6453. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6454. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6455. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6456. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6457. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6458. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6459. }
  6460. *s = hsum_float_8(acc);
  6461. #elif defined __riscv_v_intrinsic
  6462. float sumf = 0;
  6463. for (int i = 0; i < nb; ++i) {
  6464. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6465. const uint8_t * restrict q6 = x[i].ql;
  6466. const uint8_t * restrict qh = x[i].qh;
  6467. const int8_t * restrict q8 = y[i].qs;
  6468. const int8_t * restrict scale = x[i].scales;
  6469. int32_t isum = 0;
  6470. size_t vl = 16;
  6471. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6472. // load Q6
  6473. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6474. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6475. // load qh
  6476. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6477. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6478. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6479. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6480. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6481. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6482. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6483. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6484. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6485. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6486. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6487. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6488. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6489. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6490. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6491. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6492. // load Q8 and take product
  6493. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6494. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6495. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6496. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6497. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6498. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6499. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6500. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6501. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6502. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6503. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6504. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6505. sumf += isum * d_all * y[i].d;
  6506. }
  6507. *s = sumf;
  6508. #else
  6509. int8_t aux8[QK_K];
  6510. int16_t aux16[8];
  6511. float sums [8];
  6512. int32_t aux32[8];
  6513. memset(sums, 0, 8*sizeof(float));
  6514. float sumf = 0;
  6515. for (int i = 0; i < nb; ++i) {
  6516. const uint8_t * restrict q4 = x[i].ql;
  6517. const uint8_t * restrict qh = x[i].qh;
  6518. const int8_t * restrict q8 = y[i].qs;
  6519. memset(aux32, 0, 8*sizeof(int32_t));
  6520. int8_t * restrict a = aux8;
  6521. for (int l = 0; l < 16; ++l) {
  6522. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6523. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6524. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6525. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6526. }
  6527. int is = 0;
  6528. for (int j = 0; j < QK_K/16; ++j) {
  6529. int scale = x[i].scales[is++];
  6530. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6531. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6532. q8 += 8; a += 8;
  6533. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6534. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6535. q8 += 8; a += 8;
  6536. }
  6537. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6538. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6539. }
  6540. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6541. *s = sumf;
  6542. #endif
  6543. }
  6544. #endif
  6545. #if defined (__AVX2__) || defined (__ARM_NEON)
  6546. static const int8_t keven_signs_q2xs[1024] = {
  6547. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6548. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6549. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6550. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6551. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6552. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6553. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6554. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6555. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6556. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6557. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6558. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6559. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6560. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6561. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6562. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6563. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6564. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6565. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6566. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6567. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6568. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6569. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6570. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6571. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6572. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6573. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6574. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6575. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6576. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6577. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6578. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6579. };
  6580. #endif
  6581. void ggml_vec_dot_iq2_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6582. assert(n % QK_K == 0);
  6583. assert(nrc == 1);
  6584. UNUSED(nrc);
  6585. UNUSED(bx);
  6586. UNUSED(by);
  6587. UNUSED(bs);
  6588. const block_iq2_xxs * restrict x = vx;
  6589. const block_q8_K * restrict y = vy;
  6590. const int nb = n / QK_K;
  6591. #if defined(__ARM_NEON)
  6592. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6593. uint32_t aux32[4];
  6594. const uint8_t * aux8 = (const uint8_t *)aux32;
  6595. ggml_int8x16x4_t q2u;
  6596. ggml_int8x16x4_t q2s;
  6597. ggml_int8x16x4_t q8b;
  6598. float sumf = 0;
  6599. for (int i = 0; i < nb; ++i) {
  6600. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6601. const uint16_t * restrict q2 = x[i].qs;
  6602. const int8_t * restrict q8 = y[i].qs;
  6603. float sumf1 = 0, sumf2 = 0;
  6604. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6605. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6606. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6607. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6608. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6609. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6610. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6611. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6612. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6613. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6614. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6615. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6616. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6617. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6618. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6619. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]), q2u.val[1], q8b.val[1]);
  6620. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]), q2u.val[3], q8b.val[3]);
  6621. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6622. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6623. }
  6624. sumf += d*(sumf1 + sumf2);
  6625. }
  6626. *s = 0.25f * sumf;
  6627. #elif defined(__AVX2__)
  6628. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6629. uint32_t aux32[4];
  6630. const uint8_t * aux8 = (const uint8_t *)aux32;
  6631. __m256 accumf = _mm256_setzero_ps();
  6632. for (int i = 0; i < nb; ++i) {
  6633. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6634. const uint16_t * restrict q2 = x[i].qs;
  6635. const int8_t * restrict q8 = y[i].qs;
  6636. __m256i sumi1 = _mm256_setzero_si256();
  6637. __m256i sumi2 = _mm256_setzero_si256();
  6638. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6639. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6640. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6641. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6642. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6643. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6644. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6645. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6646. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6647. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6648. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6649. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6650. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6651. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6652. const uint16_t ls1 = aux32[1] >> 28;
  6653. const uint16_t ls2 = aux32[3] >> 28;
  6654. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6655. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6656. sumi1 = _mm256_add_epi32(sumi1, p1);
  6657. sumi2 = _mm256_add_epi32(sumi2, p2);
  6658. }
  6659. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6660. }
  6661. *s = 0.125f * hsum_float_8(accumf);
  6662. #else
  6663. uint32_t aux32[2];
  6664. const uint8_t * aux8 = (const uint8_t *)aux32;
  6665. float sumf = 0.f;
  6666. for (int i = 0; i < nb; ++i) {
  6667. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6668. const uint16_t * restrict q2 = x[i].qs;
  6669. const int8_t * restrict q8 = y[i].qs;
  6670. int32_t bsum = 0;
  6671. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6672. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6673. q2 += 4;
  6674. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6675. int32_t sumi = 0;
  6676. for (int l = 0; l < 4; ++l) {
  6677. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6678. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6679. for (int j = 0; j < 8; ++j) {
  6680. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6681. }
  6682. q8 += 8;
  6683. }
  6684. bsum += sumi * ls;
  6685. }
  6686. sumf += d * bsum;
  6687. }
  6688. *s = 0.125f * sumf;
  6689. #endif
  6690. }
  6691. void ggml_vec_dot_iq2_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6692. assert(n % QK_K == 0);
  6693. assert(nrc == 1);
  6694. UNUSED(nrc);
  6695. UNUSED(bx);
  6696. UNUSED(by);
  6697. UNUSED(bs);
  6698. const block_iq2_xs * restrict x = vx;
  6699. const block_q8_K * restrict y = vy;
  6700. const int nb = n / QK_K;
  6701. #if defined(__ARM_NEON)
  6702. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6703. ggml_int8x16x4_t q2u;
  6704. ggml_int8x16x4_t q2s;
  6705. ggml_int8x16x4_t q8b;
  6706. int32x4x4_t scales32;
  6707. float sumf = 0;
  6708. for (int i = 0; i < nb; ++i) {
  6709. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6710. const uint16_t * restrict q2 = x[i].qs;
  6711. const int8_t * restrict q8 = y[i].qs;
  6712. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6713. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6714. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6715. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6716. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6717. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6718. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6719. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6720. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6721. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6722. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6723. int32x4_t sumi = vdupq_n_s32(0);
  6724. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6725. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6726. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6727. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6728. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6729. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6730. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6731. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6732. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6733. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6734. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6735. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6736. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6737. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6738. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6739. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6740. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6741. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6742. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6743. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6744. q2 += 8;
  6745. }
  6746. sumf += d*vaddvq_s32(sumi);
  6747. }
  6748. *s = 0.125f * sumf;
  6749. #elif defined(__AVX2__)
  6750. const __m256i mone = _mm256_set1_epi8(1);
  6751. static const char block_sign_shuffle_mask_1[32] = {
  6752. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6753. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6754. };
  6755. static const char block_sign_shuffle_mask_2[32] = {
  6756. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6757. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6758. };
  6759. static const uint8_t bit_selector_mask_bytes[32] = {
  6760. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6761. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6762. };
  6763. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6764. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6765. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6766. #if QK_K == 64
  6767. static const uint8_t k_bit_helper[16] = {
  6768. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6769. };
  6770. const __m128i bit_helper = _mm_loadu_si128((const __m128i*)k_bit_helper);
  6771. const __m128i m511 = _mm_set1_epi16(511);
  6772. typedef union {
  6773. __m128i vec_index;
  6774. uint16_t index[8];
  6775. } index_t;
  6776. index_t idx;
  6777. __m256 accumf = _mm256_setzero_ps();
  6778. for (int i = 0; i < nb; ++i) {
  6779. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6780. const __m128i q2_data = _mm_loadu_si128((const __m128i*)x[i].qs);
  6781. idx.vec_index = _mm_and_si128(q2_data, m511);
  6782. const __m128i partial_sign_bits = _mm_srli_epi16(q2_data, 9);
  6783. const __m128i partial_sign_bits_upper = _mm_srli_epi16(q2_data, 13);
  6784. const __m128i partial_sign_bits_for_counting = _mm_xor_si128(partial_sign_bits, partial_sign_bits_upper);
  6785. const __m128i odd_bits = _mm_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6786. const __m128i full_sign_bits = _mm_or_si128(partial_sign_bits, odd_bits);
  6787. const __m256i full_signs = MM256_SET_M128I(full_sign_bits, full_sign_bits);
  6788. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  6789. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)(y[i].qs+32));
  6790. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[idx.index[3]], iq2xs_grid[idx.index[2]],
  6791. iq2xs_grid[idx.index[1]], iq2xs_grid[idx.index[0]]);
  6792. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[idx.index[7]], iq2xs_grid[idx.index[6]],
  6793. iq2xs_grid[idx.index[5]], iq2xs_grid[idx.index[4]]);
  6794. __m256i signs;
  6795. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_1);
  6796. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6797. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6798. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_2);
  6799. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6800. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6801. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6802. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6803. const __m256i sc1 = MM256_SET_M128I(_mm_set1_epi16(2*(x[i].scales[0] >> 4)+1), _mm_set1_epi16(2*(x[i].scales[0] & 0xf)+1));
  6804. const __m256i sc2 = MM256_SET_M128I(_mm_set1_epi16(2*(x[i].scales[1] >> 4)+1), _mm_set1_epi16(2*(x[i].scales[1] & 0xf)+1));
  6805. const __m256i sum = _mm256_add_epi32(_mm256_madd_epi16(sc1, dot1), _mm256_madd_epi16(sc2, dot2));
  6806. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sum), accumf);
  6807. }
  6808. *s = 0.125f * hsum_float_8(accumf);
  6809. #else
  6810. static const uint8_t k_bit_helper[32] = {
  6811. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6812. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6813. };
  6814. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  6815. const __m256i m511 = _mm256_set1_epi16(511);
  6816. const __m128i m4 = _mm_set1_epi8(0xf);
  6817. const __m128i m1 = _mm_set1_epi8(1);
  6818. uint64_t aux64;
  6819. // somewhat hacky, but gives a significant boost in performance
  6820. __m256i aux_gindex;
  6821. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6822. __m256 accumf = _mm256_setzero_ps();
  6823. for (int i = 0; i < nb; ++i) {
  6824. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6825. const uint16_t * restrict q2 = x[i].qs;
  6826. const int8_t * restrict q8 = y[i].qs;
  6827. memcpy(&aux64, x[i].scales, 8);
  6828. __m128i stmp = _mm_set1_epi64x(aux64);
  6829. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6830. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6831. __m256i sumi1 = _mm256_setzero_si256();
  6832. __m256i sumi2 = _mm256_setzero_si256();
  6833. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  6834. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  6835. aux_gindex = _mm256_and_si256(q2_data, m511);
  6836. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  6837. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  6838. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  6839. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6840. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  6841. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6842. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6843. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6844. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6845. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  6846. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  6847. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  6848. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  6849. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  6850. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  6851. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  6852. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  6853. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  6854. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  6855. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  6856. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  6857. __m256i signs;
  6858. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  6859. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6860. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6861. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  6862. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6863. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6864. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  6865. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6866. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  6867. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  6868. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6869. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  6870. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6871. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6872. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  6873. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  6874. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6875. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6876. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  6877. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  6878. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6879. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6880. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  6881. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  6882. }
  6883. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6884. }
  6885. *s = 0.125f * hsum_float_8(accumf);
  6886. #endif
  6887. #else
  6888. float sumf = 0.f;
  6889. for (int i = 0; i < nb; ++i) {
  6890. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6891. const uint16_t * restrict q2 = x[i].qs;
  6892. const uint8_t * restrict sc = x[i].scales;
  6893. const int8_t * restrict q8 = y[i].qs;
  6894. int32_t bsum = 0;
  6895. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6896. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6897. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6898. int32_t sumi = 0;
  6899. for (int l = 0; l < 2; ++l) {
  6900. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6901. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6902. for (int j = 0; j < 8; ++j) {
  6903. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6904. }
  6905. q8 += 8;
  6906. }
  6907. bsum += sumi * ls1;
  6908. sumi = 0;
  6909. for (int l = 2; l < 4; ++l) {
  6910. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6911. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6912. for (int j = 0; j < 8; ++j) {
  6913. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6914. }
  6915. q8 += 8;
  6916. }
  6917. bsum += sumi * ls2;
  6918. q2 += 4;
  6919. }
  6920. sumf += d * bsum;
  6921. }
  6922. *s = 0.125f * sumf;
  6923. #endif
  6924. }
  6925. void ggml_vec_dot_iq2_s_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  6926. assert(n % QK_K == 0);
  6927. assert(nrc == 1);
  6928. UNUSED(nrc);
  6929. UNUSED(bx);
  6930. UNUSED(by);
  6931. UNUSED(bs);
  6932. const block_iq2_s * restrict x = vx;
  6933. const block_q8_K * restrict y = vy;
  6934. const int nb = n / QK_K;
  6935. #if defined(__ARM_NEON)
  6936. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  6937. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  6938. };
  6939. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  6940. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  6941. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  6942. const uint8x16_t m1 = vdupq_n_u8(1);
  6943. const int32x4_t vzero = vdupq_n_s32(0);
  6944. uint8x16x2_t vs;
  6945. ggml_int8x16x4_t q2s;
  6946. ggml_int8x16x4_t q8b;
  6947. float sumf = 0;
  6948. for (int i = 0; i < nb; ++i) {
  6949. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6950. const uint8_t * restrict qs = x[i].qs;
  6951. const uint8_t * restrict qh = x[i].qh;
  6952. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  6953. const int8_t * restrict q8 = y[i].qs;
  6954. int sumi1 = 0, sumi2 = 0;
  6955. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6956. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6957. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  6958. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  6959. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  6960. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  6961. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  6962. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  6963. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  6964. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  6965. qs += 8;
  6966. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  6967. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  6968. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  6969. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  6970. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  6971. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  6972. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  6973. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  6974. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  6975. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  6976. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  6977. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  6978. signs += 4;
  6979. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  6980. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  6981. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  6982. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  6983. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  6984. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  6985. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  6986. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  6987. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  6988. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  6989. }
  6990. sumf += d*(sumi1 + sumi2);
  6991. }
  6992. *s = 0.125f * sumf;
  6993. #elif defined(__AVX2__)
  6994. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  6995. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  6996. };
  6997. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6998. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6999. };
  7000. const __m128i m4 = _mm_set1_epi8(0xf);
  7001. const __m128i m1 = _mm_set1_epi8(1);
  7002. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7003. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7004. uint64_t aux64;
  7005. __m256 accumf = _mm256_setzero_ps();
  7006. for (int i = 0; i < nb; ++i) {
  7007. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7008. const uint8_t * restrict qs = x[i].qs;
  7009. const uint8_t * restrict qh = x[i].qh;
  7010. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7011. const int8_t * restrict q8 = y[i].qs;
  7012. memcpy(&aux64, x[i].scales, 8);
  7013. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7014. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7015. __m256i sumi1 = _mm256_setzero_si256();
  7016. __m256i sumi2 = _mm256_setzero_si256();
  7017. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7018. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7019. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7020. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7021. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7022. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7023. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7024. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7025. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7026. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7027. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7028. qs += 8;
  7029. __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
  7030. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7031. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7032. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7033. aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
  7034. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7035. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7036. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7037. signs += 4;
  7038. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7039. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7040. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7041. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7042. sumi1 = _mm256_add_epi32(sumi1, p1);
  7043. sumi2 = _mm256_add_epi32(sumi2, p2);
  7044. }
  7045. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7046. }
  7047. *s = 0.125f * hsum_float_8(accumf);
  7048. #else
  7049. float sumf = 0;
  7050. for (int i = 0; i < nb; i++) {
  7051. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7052. const int8_t * q8 = y[i].qs;
  7053. const uint8_t * qs = x[i].qs;
  7054. const uint8_t * qh = x[i].qh;
  7055. const uint8_t * signs = qs + QK_K/8;
  7056. int bsum = 0;
  7057. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7058. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7059. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7060. int sumi1 = 0, sumi2 = 0;
  7061. for (int l = 0; l < 2; ++l) {
  7062. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7063. for (int j = 0; j < 8; ++j) {
  7064. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7065. }
  7066. q8 += 8;
  7067. }
  7068. for (int l = 2; l < 4; ++l) {
  7069. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7070. for (int j = 0; j < 8; ++j) {
  7071. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7072. }
  7073. q8 += 8;
  7074. }
  7075. bsum += ls1 * sumi1 + ls2 * sumi2;
  7076. qs += 4;
  7077. signs += 4;
  7078. }
  7079. sumf += d * bsum;
  7080. }
  7081. *s = 0.125f * sumf;
  7082. #endif
  7083. }
  7084. void ggml_vec_dot_iq3_xxs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7085. assert(n % QK_K == 0);
  7086. assert(nrc == 1);
  7087. UNUSED(nrc);
  7088. UNUSED(bx);
  7089. UNUSED(by);
  7090. UNUSED(bs);
  7091. const block_iq3_xxs * restrict x = vx;
  7092. const block_q8_K * restrict y = vy;
  7093. const int nb = n / QK_K;
  7094. #if defined(__ARM_NEON)
  7095. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7096. uint32_t aux32[2];
  7097. ggml_int8x16x4_t q3s;
  7098. ggml_int8x16x4_t q8b;
  7099. float sumf = 0;
  7100. for (int i = 0; i < nb; ++i) {
  7101. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7102. const uint8_t * restrict q3 = x[i].qs;
  7103. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7104. const int8_t * restrict q8 = y[i].qs;
  7105. float sumf1 = 0, sumf2 = 0;
  7106. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7107. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7108. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7109. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7110. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7111. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7112. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7113. q3 += 16;
  7114. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7115. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7116. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7117. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7118. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7119. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7120. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7121. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7122. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  7123. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  7124. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7125. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7126. }
  7127. sumf += d*(sumf1 + sumf2);
  7128. }
  7129. *s = 0.5f * sumf;
  7130. #elif defined(__AVX2__)
  7131. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7132. uint32_t aux32[2];
  7133. __m256 accumf = _mm256_setzero_ps();
  7134. for (int i = 0; i < nb; ++i) {
  7135. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7136. const uint8_t * restrict q3 = x[i].qs;
  7137. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7138. const int8_t * restrict q8 = y[i].qs;
  7139. __m256i sumi1 = _mm256_setzero_si256();
  7140. __m256i sumi2 = _mm256_setzero_si256();
  7141. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7142. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7143. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7144. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7145. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7146. q3 += 8;
  7147. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7148. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7149. q3 += 8;
  7150. memcpy(aux32, gas, 8); gas += 8;
  7151. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7152. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7153. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7154. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7155. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7156. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7157. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7158. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7159. const uint16_t ls1 = aux32[0] >> 28;
  7160. const uint16_t ls2 = aux32[1] >> 28;
  7161. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7162. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7163. sumi1 = _mm256_add_epi32(sumi1, p1);
  7164. sumi2 = _mm256_add_epi32(sumi2, p2);
  7165. }
  7166. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7167. }
  7168. *s = 0.25f * hsum_float_8(accumf);
  7169. #else
  7170. uint32_t aux32;
  7171. float sumf = 0.f;
  7172. for (int i = 0; i < nb; ++i) {
  7173. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7174. const uint8_t * restrict q3 = x[i].qs;
  7175. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7176. const int8_t * restrict q8 = y[i].qs;
  7177. int32_t bsum = 0;
  7178. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7179. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  7180. const uint32_t ls = 2*(aux32 >> 28) + 1;
  7181. int32_t sumi = 0;
  7182. for (int l = 0; l < 4; ++l) {
  7183. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  7184. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  7185. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  7186. for (int j = 0; j < 4; ++j) {
  7187. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  7188. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  7189. }
  7190. q8 += 8;
  7191. }
  7192. q3 += 8;
  7193. bsum += sumi * ls;
  7194. }
  7195. sumf += d * bsum;
  7196. }
  7197. *s = 0.25f * sumf;
  7198. #endif
  7199. }
  7200. void ggml_vec_dot_iq3_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7201. assert(n % QK_K == 0);
  7202. assert(nrc == 1);
  7203. UNUSED(nrc);
  7204. UNUSED(bx);
  7205. UNUSED(by);
  7206. UNUSED(bs);
  7207. const block_iq3_s * restrict x = vx;
  7208. const block_q8_K * restrict y = vy;
  7209. const int nb = n / QK_K;
  7210. #if defined(__ARM_NEON)
  7211. typedef union {
  7212. uint16x8_t vec_index;
  7213. uint16_t index[8];
  7214. } vec_index_t;
  7215. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7216. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7217. };
  7218. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7219. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  7220. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7221. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7222. const int16x8_t hshift = vld1q_s16(k_shift);
  7223. const uint16x8_t m256 = vdupq_n_u16(256);
  7224. const uint8x16_t m1 = vdupq_n_u8(1);
  7225. uint8x16x2_t vs;
  7226. ggml_int8x16x4_t q3s;
  7227. ggml_int8x16x4_t q8b;
  7228. vec_index_t idx;
  7229. #if QK_K == 256
  7230. uint32_t scales32[2];
  7231. const uint8_t * scales8 = (const uint8_t *)scales32;
  7232. #endif
  7233. float sumf = 0;
  7234. for (int i = 0; i < nb; ++i) {
  7235. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7236. const uint8_t * restrict qs = x[i].qs;
  7237. const uint8_t * restrict qh = x[i].qh;
  7238. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7239. const int8_t * restrict q8 = y[i].qs;
  7240. #if QK_K == 256
  7241. memcpy(scales32, x[i].scales, 4);
  7242. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  7243. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  7244. #endif
  7245. int sumi1 = 0, sumi2 = 0;
  7246. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7247. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7248. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  7249. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  7250. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7251. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7252. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7253. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7254. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  7255. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7256. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7257. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7258. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7259. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7260. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7261. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7262. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7263. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7264. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  7265. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  7266. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7267. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7268. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7269. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7270. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7271. signs += 4;
  7272. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  7273. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  7274. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[0], q8b.val[0]), q3s.val[1], q8b.val[1]);
  7275. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q3s.val[2], q8b.val[2]), q3s.val[3], q8b.val[3]);
  7276. #if QK_K == 256
  7277. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  7278. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  7279. #else
  7280. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  7281. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32/2] >> 4));
  7282. #endif
  7283. }
  7284. sumf += d*(sumi1 + sumi2);
  7285. }
  7286. *s = sumf;
  7287. #elif defined(__AVX2__)
  7288. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7289. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7290. };
  7291. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7292. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7293. };
  7294. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7295. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7296. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  7297. const __m256i idx_mask = _mm256_set1_epi32(256);
  7298. typedef union {
  7299. __m256i vec[2];
  7300. uint32_t index[16];
  7301. } index_t;
  7302. index_t idx;
  7303. __m256 accumf = _mm256_setzero_ps();
  7304. for (int i = 0; i < nb; ++i) {
  7305. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7306. const uint8_t * restrict qs = x[i].qs;
  7307. const uint8_t * restrict qh = x[i].qh;
  7308. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7309. const int8_t * restrict q8 = y[i].qs;
  7310. __m256i sumi1 = _mm256_setzero_si256();
  7311. __m256i sumi2 = _mm256_setzero_si256();
  7312. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7313. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7314. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7315. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  7316. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  7317. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  7318. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  7319. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  7320. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  7321. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  7322. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  7323. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  7324. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  7325. const __m256i q2_1 = _mm256_set_epi32(
  7326. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  7327. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  7328. );
  7329. const __m256i q2_2 = _mm256_set_epi32(
  7330. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  7331. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  7332. );
  7333. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7334. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7335. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7336. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7337. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7338. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7339. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7340. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7341. signs += 4;
  7342. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7343. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7344. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  7345. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  7346. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7347. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7348. sumi1 = _mm256_add_epi32(sumi1, p1);
  7349. sumi2 = _mm256_add_epi32(sumi2, p2);
  7350. }
  7351. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7352. }
  7353. *s = hsum_float_8(accumf);
  7354. #else
  7355. float sumf = 0.f;
  7356. for (int i = 0; i < nb; ++i) {
  7357. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7358. const uint8_t * restrict qs = x[i].qs;
  7359. const uint8_t * restrict qh = x[i].qh;
  7360. const uint8_t * restrict signs = x[i].signs;
  7361. const int8_t * restrict q8 = y[i].qs;
  7362. int32_t bsum = 0;
  7363. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7364. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  7365. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  7366. int32_t sumi = 0;
  7367. for (int l = 0; l < 4; ++l) {
  7368. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  7369. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  7370. for (int j = 0; j < 4; ++j) {
  7371. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7372. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7373. }
  7374. q8 += 8;
  7375. }
  7376. qs += 8;
  7377. signs += 4;
  7378. bsum += sumi * ls1;
  7379. sumi = 0;
  7380. for (int l = 0; l < 4; ++l) {
  7381. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  7382. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  7383. for (int j = 0; j < 4; ++j) {
  7384. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7385. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7386. }
  7387. q8 += 8;
  7388. }
  7389. qs += 8;
  7390. signs += 4;
  7391. bsum += sumi * ls2;
  7392. }
  7393. sumf += d * bsum;
  7394. }
  7395. *s = sumf;
  7396. #endif
  7397. }
  7398. #ifdef __AVX2__
  7399. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  7400. const __m256i ax = _mm256_sign_epi8(x, x);
  7401. const __m256i sy = _mm256_sign_epi8(y, x);
  7402. return _mm256_maddubs_epi16(ax, sy);
  7403. }
  7404. #endif
  7405. void ggml_vec_dot_iq1_s_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7406. assert(n % QK_K == 0);
  7407. assert(nrc == 1);
  7408. UNUSED(nrc);
  7409. UNUSED(bx);
  7410. UNUSED(by);
  7411. UNUSED(bs);
  7412. const block_iq1_s * restrict x = vx;
  7413. const block_q8_K * restrict y = vy;
  7414. const int nb = n / QK_K;
  7415. #if defined __ARM_NEON
  7416. ggml_int8x16x4_t q1b;
  7417. ggml_int8x16x4_t q8b;
  7418. float sumf = 0;
  7419. for (int i = 0; i < nb; ++i) {
  7420. const int8_t * q8 = y[i].qs;
  7421. const uint8_t * qs = x[i].qs;
  7422. const uint16_t * qh = x[i].qh;
  7423. int sumi1 = 0, sumi2 = 0, sumi3 = 0;
  7424. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7425. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  7426. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  7427. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  7428. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  7429. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  7430. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  7431. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  7432. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  7433. qs += 8;
  7434. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7435. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[0], q8b.val[0]), q1b.val[1], q8b.val[1]);
  7436. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q1b.val[2], q8b.val[2]), q1b.val[3], q8b.val[3]);
  7437. const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7438. const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7439. sumi1 += vaddvq_s32(p1) * ls1;
  7440. sumi2 += vaddvq_s32(p2) * ls2;
  7441. sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
  7442. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
  7443. }
  7444. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
  7445. }
  7446. *s = sumf;
  7447. #elif defined __AVX2__
  7448. __m256 accum = _mm256_setzero_ps();
  7449. float accum1 = 0;
  7450. for (int i = 0; i < nb; ++i) {
  7451. const int8_t * q8 = y[i].qs;
  7452. const uint8_t * qs = x[i].qs;
  7453. const uint16_t * qh = x[i].qh;
  7454. __m256i sumi = _mm256_setzero_si256();
  7455. int sumi1 = 0;
  7456. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7457. const __m256i q1b_1 = _mm256_set_epi64x(iq1s_grid[qs[3] | ((qh[ib+0] >> 1) & 0x700)], iq1s_grid[qs[2] | ((qh[ib+0] << 2) & 0x700)],
  7458. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  7459. const __m256i q1b_2 = _mm256_set_epi64x(iq1s_grid[qs[7] | ((qh[ib+1] >> 1) & 0x700)], iq1s_grid[qs[6] | ((qh[ib+1] << 2) & 0x700)],
  7460. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  7461. qs += 8;
  7462. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7463. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7464. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7465. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7466. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7467. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7468. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
  7469. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
  7470. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  7471. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  7472. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  7473. }
  7474. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  7475. accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
  7476. accum1 += d * sumi1;
  7477. }
  7478. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  7479. #else
  7480. float sumf = 0;
  7481. for (int i = 0; i < nb; i++) {
  7482. const int8_t * q8 = y[i].qs;
  7483. const uint8_t * qs = x[i].qs;
  7484. const uint16_t * qh = x[i].qh;
  7485. int sumi = 0, sumi1 = 0;
  7486. for (int ib = 0; ib < QK_K/32; ++ib) {
  7487. const int ls = 2*((qh[ib] >> 12) & 7) + 1;
  7488. const int delta = qh[ib] & 0x8000 ? -1 : 1;
  7489. int lsum = 0;
  7490. for (int l = 0; l < 4; ++l) {
  7491. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  7492. for (int j = 0; j < 8; ++j) {
  7493. lsum += q8[j] * grid[j];
  7494. }
  7495. q8 += 8;
  7496. }
  7497. sumi += ls * lsum;
  7498. sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
  7499. qs += 4;
  7500. }
  7501. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
  7502. }
  7503. *s = sumf;
  7504. #endif
  7505. }
  7506. void ggml_vec_dot_iq1_m_q8_K (int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7507. assert(n % QK_K == 0);
  7508. assert(nrc == 1);
  7509. UNUSED(nrc);
  7510. UNUSED(bx);
  7511. UNUSED(by);
  7512. UNUSED(bs);
  7513. const block_iq1_m * restrict x = vx;
  7514. const block_q8_K * restrict y = vy;
  7515. const int nb = n / QK_K;
  7516. #if QK_K != 64
  7517. iq1m_scale_t scale;
  7518. #endif
  7519. #if defined __ARM_NEON
  7520. #if QK_K == 64
  7521. const int32x4_t mask = vdupq_n_s32(0xf);
  7522. #else
  7523. const int32x4_t mask = vdupq_n_s32(0x7);
  7524. #endif
  7525. const int32x4_t mone = vdupq_n_s32(1);
  7526. const int32x4_t mzero = vdupq_n_s32(0);
  7527. ggml_int8x16x4_t deltas;
  7528. deltas.val[0] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(+1));
  7529. deltas.val[1] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(+1));
  7530. deltas.val[2] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(-1));
  7531. deltas.val[3] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(-1));
  7532. ggml_int8x16x4_t q1b;
  7533. ggml_int8x16x4_t q8b;
  7534. uint32_t aux32;
  7535. const uint8_t * aux8 = (const uint8_t *)&aux32;
  7536. float sumf = 0;
  7537. for (int i = 0; i < nb; ++i) {
  7538. const int8_t * q8 = y[i].qs;
  7539. const uint8_t * qs = x[i].qs;
  7540. const uint8_t * qh = x[i].qh;
  7541. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7542. #if QK_K != 64
  7543. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7544. #endif
  7545. int32x4_t sumi1 = mzero;
  7546. int32x4_t sumi2 = mzero;
  7547. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7548. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[0] << 8) & 0x700)))),
  7549. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[0] << 4) & 0x700)))));
  7550. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[1] << 8) & 0x700)))),
  7551. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[1] << 4) & 0x700)))));
  7552. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[2] << 8) & 0x700)))),
  7553. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[2] << 4) & 0x700)))));
  7554. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[3] << 8) & 0x700)))),
  7555. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[3] << 4) & 0x700)))));
  7556. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7557. const int32x4_t p1 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[0], q8b.val[0]), ggml_vdotq_s32(mzero, q1b.val[1], q8b.val[1]));
  7558. const int32x4_t p2 = vpaddq_s32(ggml_vdotq_s32(mzero, q1b.val[2], q8b.val[2]), ggml_vdotq_s32(mzero, q1b.val[3], q8b.val[3]));
  7559. const int32x4_t p12 = vpaddq_s32(p1, p2);
  7560. const uint32_t * qh32 = (const uint32_t *)qh; // we are 4-byte aligned, so we can do that
  7561. aux32 = ((qh32[0] >> 3) & 0x01010101) | ((qh32[0] >> 6) & 0x02020202);
  7562. const int32x4_t p3 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[0]], q8b.val[0]), ggml_vdotq_s32(mzero, deltas.val[aux8[1]], q8b.val[1]));
  7563. const int32x4_t p4 = vpaddq_s32(ggml_vdotq_s32(mzero, deltas.val[aux8[2]], q8b.val[2]), ggml_vdotq_s32(mzero, deltas.val[aux8[3]], q8b.val[3]));
  7564. const int32x4_t p34 = vpaddq_s32(p3, p4);
  7565. #if QK_K == 64
  7566. int32x4_t scales_4 = ggml_vld1q_u32(sc[0] >> 0, sc[0] >> 4, sc[0] >> 8, sc[0] >> 12);
  7567. #else
  7568. int32x4_t scales_4 = ggml_vld1q_u32(sc[ib/2] >> 0, sc[ib/2] >> 3, sc[ib/2] >> 6, sc[ib/2] >> 9);
  7569. #endif
  7570. scales_4 = vaddq_s32(vshlq_n_s32(vandq_s32(scales_4, mask), 1), mone);
  7571. sumi1 = vmlaq_s32(sumi1, scales_4, p12);
  7572. sumi2 = vmlaq_s32(sumi2, scales_4, p34);
  7573. qs += 8; qh += 4;
  7574. }
  7575. #if QK_K == 64
  7576. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7577. #else
  7578. sumf += y[i].d * GGML_FP16_TO_FP32(scale.f16) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7579. #endif
  7580. }
  7581. *s = sumf;
  7582. #elif defined __AVX2__
  7583. #if QK_K == 64
  7584. const __m256i mask = _mm256_set1_epi16(0xf);
  7585. #else
  7586. const __m256i mask = _mm256_set1_epi16(0x7);
  7587. #endif
  7588. const __m256i mone = _mm256_set1_epi16(1);
  7589. __m256 accum1 = _mm256_setzero_ps();
  7590. __m256 accum2 = _mm256_setzero_ps();
  7591. for (int i = 0; i < nb; ++i) {
  7592. const int8_t * q8 = y[i].qs;
  7593. const uint8_t * qs = x[i].qs;
  7594. const uint8_t * qh = x[i].qh;
  7595. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7596. #if QK_K != 64
  7597. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7598. #endif
  7599. __m256i sumi1 = _mm256_setzero_si256();
  7600. __m256i sumi2 = _mm256_setzero_si256();
  7601. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7602. const __m256i q1b_1 = _mm256_set_epi64x(
  7603. iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)],
  7604. iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]
  7605. );
  7606. const __m256i q1b_2 = _mm256_set_epi64x(
  7607. iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)],
  7608. iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]
  7609. );
  7610. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7611. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7612. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7613. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7614. const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7615. qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7616. qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7617. qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7618. const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7619. qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7620. qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7621. qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7622. const __m256i dot3 = mul_add_epi8(delta1, q8b_1);
  7623. const __m256i dot4 = mul_add_epi8(delta2, q8b_2);
  7624. #if QK_K == 64
  7625. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 4), _mm_set1_epi16(sc[0] >> 0));
  7626. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[0] >> 12), _mm_set1_epi16(sc[0] >> 8));
  7627. #else
  7628. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 3), _mm_set1_epi16(sc[ib/2] >> 0));
  7629. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 9), _mm_set1_epi16(sc[ib/2] >> 6));
  7630. #endif
  7631. scale1 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale1, mask), 1), mone);
  7632. scale2 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale2, mask), 1), mone);
  7633. const __m256i p1 = _mm256_madd_epi16(dot1, scale1);
  7634. const __m256i p2 = _mm256_madd_epi16(dot2, scale2);
  7635. const __m256i p3 = _mm256_madd_epi16(dot3, scale1);
  7636. const __m256i p4 = _mm256_madd_epi16(dot4, scale2);
  7637. sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2));
  7638. sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4));
  7639. qs += 8; qh += 4;
  7640. }
  7641. #if QK_K == 64
  7642. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(x[i].d));
  7643. #else
  7644. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(scale.f16));
  7645. #endif
  7646. accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1);
  7647. accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2);
  7648. }
  7649. *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
  7650. #else
  7651. int sum1[2], sum2[2], delta[4];
  7652. float sumf = 0;
  7653. for (int i = 0; i < nb; i++) {
  7654. const int8_t * q8 = y[i].qs;
  7655. const uint8_t * qs = x[i].qs;
  7656. const uint8_t * qh = x[i].qh;
  7657. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7658. #if QK_K != 64
  7659. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7660. #endif
  7661. int sumi1 = 0, sumi2 = 0;
  7662. for (int ib = 0; ib < QK_K/32; ++ib) {
  7663. delta[0] = qh[0] & 0x08 ? -1 : 1;
  7664. delta[1] = qh[0] & 0x80 ? -1 : 1;
  7665. delta[2] = qh[1] & 0x08 ? -1 : 1;
  7666. delta[3] = qh[1] & 0x80 ? -1 : 1;
  7667. sum1[0] = sum1[1] = sum2[0] = sum2[1] = 0;
  7668. for (int l = 0; l < 4; ++l) {
  7669. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((uint16_t)qh[l/2] << (8 - 4*(l%2))) & 0x700)));
  7670. int lsum1 = 0, lsum2 = 0;
  7671. for (int j = 0; j < 8; ++j) {
  7672. lsum1 += q8[j] * grid[j];
  7673. lsum2 += q8[j];
  7674. }
  7675. q8 += 8;
  7676. sum1[l/2] += lsum1;
  7677. sum2[l/2] += lsum2*delta[l];
  7678. }
  7679. #if QK_K == 64
  7680. const int ls1 = 2*((sc[0] >> (8*(ib%2)+0)) & 0xf) + 1;
  7681. const int ls2 = 2*((sc[0] >> (8*(ib%2)+4)) & 0xf) + 1;
  7682. #else
  7683. const int ls1 = 2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1;
  7684. const int ls2 = 2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1;
  7685. #endif
  7686. sumi1 += sum1[0] * ls1 + sum1[1] * ls2;
  7687. sumi2 += sum2[0] * ls1 + sum2[1] * ls2;
  7688. qs += 4;
  7689. qh += 2;
  7690. }
  7691. #if QK_K == 64
  7692. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7693. #else
  7694. sumf += GGML_FP16_TO_FP32(scale.f16) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7695. #endif
  7696. }
  7697. *s = sumf;
  7698. #endif
  7699. }
  7700. void ggml_vec_dot_iq4_nl_q8_0(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7701. assert(nrc == 1);
  7702. UNUSED(nrc);
  7703. UNUSED(bx);
  7704. UNUSED(by);
  7705. UNUSED(bs);
  7706. assert(n % QK4_NL == 0);
  7707. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  7708. const block_iq4_nl * restrict x = vx;
  7709. const block_q8_0 * restrict y = vy;
  7710. const int nb = n / QK4_NL;
  7711. #if defined __ARM_NEON
  7712. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7713. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7714. uint8x16x2_t q4bits;
  7715. int8x16x4_t q4b;
  7716. int8x16x4_t q8b;
  7717. int32x4_t prod_1, prod_2;
  7718. float sumf = 0;
  7719. for (int ib = 0; ib < nb; ib += 2) {
  7720. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  7721. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  7722. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  7723. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  7724. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  7725. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  7726. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7727. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7728. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7729. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7730. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7731. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7732. sumf +=
  7733. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  7734. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  7735. }
  7736. *s = sumf;
  7737. #elif defined __AVX2__
  7738. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7739. const __m128i m4b = _mm_set1_epi8(0x0f);
  7740. const __m256i mone = _mm256_set1_epi16(1);
  7741. __m256 accum1 = _mm256_setzero_ps();
  7742. __m256 accum2 = _mm256_setzero_ps();
  7743. for (int ib = 0; ib < nb; ib += 2) {
  7744. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  7745. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  7746. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  7747. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  7748. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7749. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7750. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7751. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7752. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7753. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7754. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  7755. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  7756. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  7757. _mm256_cvtepi32_ps(p_1), accum1);
  7758. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  7759. _mm256_cvtepi32_ps(p_2), accum2);
  7760. y += 2;
  7761. x += 2;
  7762. }
  7763. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  7764. #else
  7765. float sumf = 0;
  7766. for (int ib = 0; ib < nb; ++ib) {
  7767. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  7768. int sumi1 = 0, sumi2 = 0;
  7769. for (int j = 0; j < QK4_NL/2; ++j) {
  7770. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  7771. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  7772. }
  7773. sumf += d * (sumi1 + sumi2);
  7774. }
  7775. *s = sumf;
  7776. #endif
  7777. }
  7778. void ggml_vec_dot_iq4_xs_q8_K(int n, float * restrict s, size_t bs, const void * restrict vx, size_t bx, const void * restrict vy, size_t by, int nrc) {
  7779. assert(nrc == 1);
  7780. UNUSED(nrc);
  7781. UNUSED(bx);
  7782. UNUSED(by);
  7783. UNUSED(bs);
  7784. assert(n % QK_K == 0);
  7785. #if QK_K == 64
  7786. ggml_vec_dot_iq4_nl_q8_0(n, s, bs, vx, bx, vy, by, nrc);
  7787. #else
  7788. const block_iq4_xs * restrict x = vx;
  7789. const block_q8_K * restrict y = vy;
  7790. const int nb = n / QK_K;
  7791. #if defined __ARM_NEON
  7792. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7793. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7794. ggml_uint8x16x2_t q4bits;
  7795. ggml_int8x16x4_t q4b;
  7796. ggml_int8x16x4_t q8b;
  7797. int32x4_t prod_1, prod_2;
  7798. float sumf = 0;
  7799. for (int ibl = 0; ibl < nb; ++ibl) {
  7800. const int8_t * q8 = y[ibl].qs;
  7801. const uint8_t * q4 = x[ibl].qs;
  7802. uint16_t h = x[ibl].scales_h;
  7803. int sumi1 = 0, sumi2 = 0;
  7804. for (int ib = 0; ib < QK_K/64; ++ib) {
  7805. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  7806. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7807. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7808. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7809. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7810. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7811. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7812. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7813. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  7814. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  7815. h >>= 4;
  7816. sumi1 += vaddvq_s32(prod_1) * ls1;
  7817. sumi2 += vaddvq_s32(prod_2) * ls2;
  7818. }
  7819. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  7820. }
  7821. *s = sumf;
  7822. #elif defined __AVX2__
  7823. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7824. const __m128i m4b = _mm_set1_epi8(0x0f);
  7825. __m256 accum = _mm256_setzero_ps();
  7826. for (int ibl = 0; ibl < nb; ++ibl) {
  7827. const uint8_t * qs = x[ibl].qs;
  7828. const int8_t * q8 = y[ibl].qs;
  7829. uint16_t sh = x[ibl].scales_h;
  7830. __m256i sumi1 = _mm256_setzero_si256();
  7831. __m256i sumi2 = _mm256_setzero_si256();
  7832. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7833. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7834. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7835. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7836. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7837. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7838. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7839. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7840. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7841. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7842. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7843. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  7844. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  7845. sh >>= 4;
  7846. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  7847. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  7848. sumi1 = _mm256_add_epi32(p_1, sumi1);
  7849. sumi2 = _mm256_add_epi32(p_2, sumi2);
  7850. }
  7851. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  7852. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  7853. }
  7854. *s = hsum_float_8(accum);
  7855. #else
  7856. float sumf = 0;
  7857. for (int ibl = 0; ibl < nb; ++ibl) {
  7858. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  7859. uint16_t h = x[ibl].scales_h;
  7860. const uint8_t * qs = x[ibl].qs;
  7861. const int8_t * q8 = y[ibl].qs;
  7862. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7863. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  7864. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  7865. h >>= 4;
  7866. const float d1 = d4d8*(ls1 - 32);
  7867. const float d2 = d4d8*(ls2 - 32);
  7868. int sumi1 = 0, sumi2 = 0;
  7869. for (int j = 0; j < 16; ++j) {
  7870. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7871. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7872. }
  7873. sumf += d1 * (sumi1 + sumi2);
  7874. qs += 16;
  7875. q8 += 32;
  7876. sumi1 = sumi2 = 0;
  7877. for (int j = 0; j < 16; ++j) {
  7878. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7879. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7880. }
  7881. sumf += d2 * (sumi1 + sumi2);
  7882. qs += 16;
  7883. q8 += 32;
  7884. }
  7885. }
  7886. *s = sumf;
  7887. #endif
  7888. #endif
  7889. }
  7890. // ================================ IQ2 quantization =============================================
  7891. typedef struct {
  7892. uint64_t * grid;
  7893. int * map;
  7894. uint16_t * neighbours;
  7895. } iq2_entry_t;
  7896. static iq2_entry_t iq2_data[4] = {
  7897. {NULL, NULL, NULL},
  7898. {NULL, NULL, NULL},
  7899. {NULL, NULL, NULL},
  7900. {NULL, NULL, NULL},
  7901. };
  7902. static inline int iq2_data_index(enum ggml_type type) {
  7903. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  7904. return type == GGML_TYPE_IQ2_XXS ? 0 :
  7905. type == GGML_TYPE_IQ2_XS ? 1 :
  7906. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
  7907. }
  7908. static inline int iq2_grid_size(enum ggml_type type) {
  7909. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  7910. return type == GGML_TYPE_IQ2_XXS ? 256 :
  7911. type == GGML_TYPE_IQ2_XS ? 512 :
  7912. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
  7913. }
  7914. static int iq2_compare_func(const void * left, const void * right) {
  7915. const int * l = (const int *)left;
  7916. const int * r = (const int *)right;
  7917. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7918. }
  7919. void iq2xs_init_impl(enum ggml_type type) {
  7920. const int gindex = iq2_data_index(type);
  7921. const int grid_size = iq2_grid_size(type);
  7922. if (iq2_data[gindex].grid) {
  7923. return;
  7924. }
  7925. static const uint16_t kgrid_2bit_256[256] = {
  7926. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  7927. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  7928. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  7929. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  7930. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  7931. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  7932. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  7933. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  7934. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  7935. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  7936. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  7937. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  7938. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  7939. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  7940. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  7941. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  7942. };
  7943. static const uint16_t kgrid_2bit_512[512] = {
  7944. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  7945. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  7946. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  7947. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  7948. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  7949. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  7950. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  7951. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  7952. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  7953. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  7954. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  7955. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  7956. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  7957. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  7958. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  7959. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  7960. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  7961. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  7962. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  7963. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  7964. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  7965. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  7966. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  7967. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  7968. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  7969. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  7970. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  7971. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  7972. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  7973. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  7974. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  7975. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  7976. };
  7977. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  7978. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  7979. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  7980. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  7981. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  7982. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  7983. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  7984. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  7985. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  7986. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  7987. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  7988. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  7989. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  7990. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  7991. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  7992. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  7993. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  7994. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  7995. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  7996. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  7997. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  7998. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  7999. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  8000. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  8001. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  8002. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  8003. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  8004. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  8005. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  8006. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  8007. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  8008. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  8009. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  8010. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  8011. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  8012. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  8013. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  8014. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  8015. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  8016. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  8017. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  8018. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  8019. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  8020. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  8021. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  8022. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  8023. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  8024. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  8025. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  8026. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  8027. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  8028. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  8029. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  8030. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  8031. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  8032. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  8033. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  8034. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  8035. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  8036. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  8037. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  8038. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  8039. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  8040. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  8041. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  8042. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  8043. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  8044. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  8045. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  8046. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  8047. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  8048. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  8049. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  8050. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  8051. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  8052. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  8053. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  8054. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  8055. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  8056. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  8057. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  8058. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  8059. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  8060. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  8061. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  8062. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  8063. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  8064. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  8065. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  8066. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  8067. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  8068. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  8069. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  8070. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  8071. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  8072. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  8073. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  8074. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  8075. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  8076. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  8077. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  8078. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  8079. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  8080. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  8081. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  8082. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  8083. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  8084. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  8085. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  8086. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  8087. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  8088. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  8089. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  8090. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  8091. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  8092. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  8093. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  8094. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  8095. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  8096. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  8097. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  8098. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  8099. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  8100. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  8101. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  8102. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  8103. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  8104. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  8105. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  8106. };
  8107. static const uint16_t kgrid_2bit_1024[1024] = {
  8108. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8109. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  8110. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  8111. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  8112. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  8113. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  8114. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  8115. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  8116. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  8117. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  8118. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  8119. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  8120. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  8121. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  8122. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  8123. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  8124. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  8125. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  8126. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  8127. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  8128. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  8129. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  8130. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  8131. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  8132. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  8133. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  8134. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  8135. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  8136. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  8137. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  8138. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  8139. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  8140. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  8141. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  8142. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  8143. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  8144. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  8145. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  8146. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  8147. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  8148. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  8149. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  8150. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  8151. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  8152. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  8153. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  8154. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  8155. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  8156. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  8157. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  8158. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  8159. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  8160. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  8161. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  8162. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  8163. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  8164. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  8165. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  8166. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  8167. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  8168. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  8169. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  8170. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  8171. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  8172. };
  8173. const int kmap_size = 43692;
  8174. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  8175. const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  8176. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  8177. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  8178. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
  8179. uint64_t * kgrid_q2xs;
  8180. int * kmap_q2xs;
  8181. uint16_t * kneighbors_q2xs;
  8182. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8183. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  8184. for (int k = 0; k < grid_size; ++k) {
  8185. int8_t * pos = (int8_t *)(the_grid + k);
  8186. for (int i = 0; i < 8; ++i) {
  8187. int l = (kgrid[k] >> 2*i) & 0x3;
  8188. pos[i] = 2*l + 1;
  8189. }
  8190. }
  8191. kgrid_q2xs = the_grid;
  8192. iq2_data[gindex].grid = the_grid;
  8193. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  8194. iq2_data[gindex].map = kmap_q2xs;
  8195. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  8196. uint64_t aux64;
  8197. uint8_t * aux8 = (uint8_t *)&aux64;
  8198. for (int i = 0; i < grid_size; ++i) {
  8199. aux64 = kgrid_q2xs[i];
  8200. uint16_t index = 0;
  8201. for (int k=0; k<8; ++k) {
  8202. uint16_t q = (aux8[k] - 1)/2;
  8203. index |= (q << 2*k);
  8204. }
  8205. kmap_q2xs[index] = i;
  8206. }
  8207. int8_t pos[8];
  8208. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8209. int num_neighbors = 0, num_not_in_map = 0;
  8210. for (int i = 0; i < kmap_size; ++i) {
  8211. if (kmap_q2xs[i] >= 0) continue;
  8212. ++num_not_in_map;
  8213. for (int k = 0; k < 8; ++k) {
  8214. int l = (i >> 2*k) & 0x3;
  8215. pos[k] = 2*l + 1;
  8216. }
  8217. for (int j = 0; j < grid_size; ++j) {
  8218. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8219. int d2 = 0;
  8220. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8221. dist2[2*j+0] = d2;
  8222. dist2[2*j+1] = j;
  8223. }
  8224. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8225. int n = 0; int d2 = dist2[0];
  8226. int nhave = 1;
  8227. for (int j = 0; j < grid_size; ++j) {
  8228. if (dist2[2*j] > d2) {
  8229. if (nhave == nwant) break;
  8230. d2 = dist2[2*j];
  8231. ++nhave;
  8232. }
  8233. ++n;
  8234. }
  8235. num_neighbors += n;
  8236. }
  8237. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8238. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8239. iq2_data[gindex].neighbours = kneighbors_q2xs;
  8240. int counter = 0;
  8241. for (int i = 0; i < kmap_size; ++i) {
  8242. if (kmap_q2xs[i] >= 0) continue;
  8243. for (int k = 0; k < 8; ++k) {
  8244. int l = (i >> 2*k) & 0x3;
  8245. pos[k] = 2*l + 1;
  8246. }
  8247. for (int j = 0; j < grid_size; ++j) {
  8248. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8249. int d2 = 0;
  8250. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8251. dist2[2*j+0] = d2;
  8252. dist2[2*j+1] = j;
  8253. }
  8254. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8255. kmap_q2xs[i] = -(counter + 1);
  8256. int d2 = dist2[0];
  8257. uint16_t * start = &kneighbors_q2xs[counter++];
  8258. int n = 0, nhave = 1;
  8259. for (int j = 0; j < grid_size; ++j) {
  8260. if (dist2[2*j] > d2) {
  8261. if (nhave == nwant) break;
  8262. d2 = dist2[2*j];
  8263. ++nhave;
  8264. }
  8265. kneighbors_q2xs[counter++] = dist2[2*j+1];
  8266. ++n;
  8267. }
  8268. *start = n;
  8269. }
  8270. free(dist2);
  8271. }
  8272. void iq2xs_free_impl(enum ggml_type type) {
  8273. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M || type == GGML_TYPE_IQ2_S);
  8274. const int gindex = iq2_data_index(type);
  8275. if (iq2_data[gindex].grid) {
  8276. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  8277. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  8278. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  8279. }
  8280. }
  8281. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  8282. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8283. int num_neighbors = neighbours[0];
  8284. GGML_ASSERT(num_neighbors > 0);
  8285. float best_d2 = FLT_MAX;
  8286. int grid_index = -1;
  8287. for (int j = 1; j <= num_neighbors; ++j) {
  8288. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8289. float d2 = 0;
  8290. for (int i = 0; i < 8; ++i) {
  8291. float q = pg[i];
  8292. float diff = scale*q - xval[i];
  8293. d2 += weight[i]*diff*diff;
  8294. }
  8295. if (d2 < best_d2) {
  8296. best_d2 = d2; grid_index = neighbours[j];
  8297. }
  8298. }
  8299. GGML_ASSERT(grid_index >= 0);
  8300. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8301. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  8302. return grid_index;
  8303. }
  8304. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  8305. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  8306. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8307. const int * kmap_q2xs = iq2_data[gindex].map;
  8308. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8309. GGML_ASSERT(quant_weights && "missing quantization weights");
  8310. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8311. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8312. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8313. GGML_ASSERT(n%QK_K == 0);
  8314. const int kMaxQ = 3;
  8315. const int64_t nbl = n/QK_K;
  8316. block_iq2_xxs * y = vy;
  8317. float scales[QK_K/32];
  8318. float weight[32];
  8319. float xval[32];
  8320. int8_t L[32];
  8321. int8_t Laux[32];
  8322. float waux[32];
  8323. uint8_t block_signs[4];
  8324. uint32_t q2[2*(QK_K/32)];
  8325. for (int ibl = 0; ibl < nbl; ++ibl) {
  8326. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8327. memset(q2, 0, QK_K/4);
  8328. float max_scale = 0;
  8329. const float * xbl = x + QK_K*ibl;
  8330. float sumx2 = 0;
  8331. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8332. float sigma2 = sumx2/QK_K;
  8333. for (int ib = 0; ib < QK_K/32; ++ib) {
  8334. const float * xb = xbl + 32*ib;
  8335. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8336. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8337. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8338. for (int k = 0; k < 4; ++k) {
  8339. int nflip = 0;
  8340. uint8_t s = 0;
  8341. for (int i = 0; i < 8; ++i) {
  8342. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8343. else {
  8344. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8345. }
  8346. }
  8347. if (nflip%2) {
  8348. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8349. for (int i = 1; i < 8; ++i) {
  8350. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8351. if (ax < min) {
  8352. min = ax; imin = i;
  8353. }
  8354. }
  8355. xval[8*k+imin] = -xval[8*k+imin];
  8356. s ^= (1 << imin);
  8357. }
  8358. block_signs[k] = s & 127;
  8359. }
  8360. float max = xval[0];
  8361. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8362. if (!max) {
  8363. scales[ib] = 0;
  8364. memset(L, 0, 32);
  8365. continue;
  8366. }
  8367. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  8368. float eff_max = scale*kMaxQ;
  8369. float best = 0;
  8370. for (int is = -6; is <= 6; ++is) {
  8371. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  8372. float this_scale = 1/id;
  8373. for (int k = 0; k < 4; ++k) {
  8374. for (int i = 0; i < 8; ++i) {
  8375. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8376. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8377. }
  8378. uint16_t u = 0;
  8379. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8380. int grid_index = kmap_q2xs[u];
  8381. if (grid_index < 0) {
  8382. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8383. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8384. }
  8385. }
  8386. float sumqx = 0, sumq2 = 0;
  8387. for (int i = 0; i < 32; ++i) {
  8388. float w = weight[i];
  8389. float q = 2*Laux[i] + 1;
  8390. sumqx += w*xval[i]*q;
  8391. sumq2 += w*q*q;
  8392. }
  8393. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8394. scale = sumqx/sumq2; best = scale*sumqx;
  8395. memcpy(L, Laux, 32);
  8396. }
  8397. }
  8398. if (scale > 0) {
  8399. float id = 1/scale;
  8400. for (int k = 0; k < 4; ++k) {
  8401. uint16_t u = 0;
  8402. for (int i = 0; i < 8; ++i) {
  8403. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8404. l = MAX(0, MIN(kMaxQ-1, l));
  8405. u |= (l << 2*i);
  8406. }
  8407. int grid_index = kmap_q2xs[u];
  8408. if (grid_index < 0) {
  8409. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8410. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8411. }
  8412. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  8413. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  8414. }
  8415. float sumqx = 0, sumq2 = 0;
  8416. for (int i = 0; i < 32; ++i) {
  8417. float w = weight[i];
  8418. float q = 2*L[i] + 1;
  8419. sumqx += w*xval[i]*q;
  8420. sumq2 += w*q*q;
  8421. }
  8422. if (sumq2 > 0) scale = sumqx/sumq2;
  8423. }
  8424. if (scale < 0) {
  8425. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8426. // and correspondingly flip quant signs.
  8427. scale = -scale;
  8428. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8429. }
  8430. for (int k = 0; k < 4; ++k) {
  8431. uint16_t u = 0;
  8432. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8433. int grid_index = kmap_q2xs[u];
  8434. if (grid_index < 0) {
  8435. printf("Oops: found point %u not on grid:", u);
  8436. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8437. printf("\n");
  8438. GGML_ASSERT(false);
  8439. }
  8440. q2[2*ib+0] |= (grid_index << 8*k);
  8441. q2[2*ib+1] |= (block_signs[k] << 7*k);
  8442. }
  8443. GGML_ASSERT(scale >= 0);
  8444. scales[ib] = scale;
  8445. max_scale = MAX(max_scale, scale);
  8446. }
  8447. if (!max_scale) {
  8448. memset(y[ibl].qs, 0, QK_K/4);
  8449. continue;
  8450. }
  8451. float d = max_scale/31;
  8452. y[ibl].d = GGML_FP32_TO_FP16(d);
  8453. float id = 1/d;
  8454. for (int ib = 0; ib < QK_K/32; ++ib) {
  8455. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8456. l = MAX(0, MIN(15, l));
  8457. q2[2*ib+1] |= ((uint32_t)l << 28);
  8458. }
  8459. memcpy(y[ibl].qs, q2, QK_K/4);
  8460. }
  8461. }
  8462. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  8463. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  8464. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8465. const int * kmap_q2xs = iq2_data[gindex].map;
  8466. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8467. GGML_ASSERT(quant_weights && "missing quantization weights");
  8468. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8469. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8470. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8471. GGML_ASSERT(n%QK_K == 0);
  8472. const int kMaxQ = 3;
  8473. const int64_t nbl = n/QK_K;
  8474. block_iq2_xs * y = vy;
  8475. float scales[QK_K/16];
  8476. float weight[16];
  8477. float xval[16];
  8478. int8_t L[16];
  8479. int8_t Laux[16];
  8480. float waux[16];
  8481. bool is_on_grid[2];
  8482. bool is_on_grid_aux[2];
  8483. uint8_t block_signs[2];
  8484. uint16_t q2[2*(QK_K/16)];
  8485. for (int ibl = 0; ibl < nbl; ++ibl) {
  8486. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8487. memset(q2, 0, QK_K/4);
  8488. memset(y[ibl].scales, 0, QK_K/32);
  8489. float max_scale = 0;
  8490. const float * xbl = x + QK_K*ibl;
  8491. float sumx2 = 0;
  8492. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8493. float sigma2 = sumx2/QK_K;
  8494. for (int ib = 0; ib < QK_K/16; ++ib) {
  8495. const float * xb = xbl + 16*ib;
  8496. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  8497. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8498. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  8499. for (int k = 0; k < 2; ++k) {
  8500. int nflip = 0;
  8501. uint8_t s = 0;
  8502. for (int i = 0; i < 8; ++i) {
  8503. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8504. else {
  8505. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8506. }
  8507. }
  8508. if (nflip%2) {
  8509. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8510. for (int i = 1; i < 8; ++i) {
  8511. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8512. if (ax < min) {
  8513. min = ax; imin = i;
  8514. }
  8515. }
  8516. xval[8*k+imin] = -xval[8*k+imin];
  8517. s ^= (1 << imin);
  8518. }
  8519. block_signs[k] = s & 127;
  8520. }
  8521. float max = xval[0];
  8522. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  8523. if (!max) {
  8524. scales[ib] = 0;
  8525. memset(L, 0, 16);
  8526. continue;
  8527. }
  8528. float best = 0;
  8529. float scale = max/(2*kMaxQ-1);
  8530. is_on_grid[0] = is_on_grid[1] = true;
  8531. for (int is = -9; is <= 9; ++is) {
  8532. float id = (2*kMaxQ-1+is*0.1f)/max;
  8533. float this_scale = 1/id;
  8534. for (int k = 0; k < 2; ++k) {
  8535. for (int i = 0; i < 8; ++i) {
  8536. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8537. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8538. }
  8539. uint16_t u = 0;
  8540. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8541. int grid_index = kmap_q2xs[u];
  8542. is_on_grid_aux[k] = true;
  8543. if (grid_index < 0) {
  8544. is_on_grid_aux[k] = false;
  8545. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8546. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8547. }
  8548. }
  8549. float sumqx = 0, sumq2 = 0;
  8550. for (int i = 0; i < 16; ++i) {
  8551. float w = weight[i];
  8552. float q = 2*Laux[i] + 1;
  8553. sumqx += w*xval[i]*q;
  8554. sumq2 += w*q*q;
  8555. }
  8556. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8557. scale = sumqx/sumq2; best = scale*sumqx;
  8558. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  8559. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8560. }
  8561. }
  8562. int n_not_ongrid = 0;
  8563. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8564. if (n_not_ongrid > 0 && scale > 0) {
  8565. float id = 1/scale;
  8566. for (int k = 0; k < 2; ++k) {
  8567. if (is_on_grid[k]) continue;
  8568. uint16_t u = 0;
  8569. for (int i = 0; i < 8; ++i) {
  8570. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8571. l = MAX(0, MIN(kMaxQ-1, l));
  8572. u |= (l << 2*i);
  8573. L[8*k + i] = l;
  8574. }
  8575. int grid_index = kmap_q2xs[u];
  8576. if (grid_index < 0) {
  8577. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8578. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8579. }
  8580. }
  8581. float sumqx = 0, sumq2 = 0;
  8582. for (int i = 0; i < 16; ++i) {
  8583. float w = weight[i];
  8584. float q = 2*L[i] + 1;
  8585. sumqx += w*xval[i]*q;
  8586. sumq2 += w*q*q;
  8587. }
  8588. if (sumq2 > 0) scale = sumqx/sumq2;
  8589. }
  8590. if (scale < 0) {
  8591. scale = -scale;
  8592. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8593. }
  8594. for (int k = 0; k < 2; ++k) {
  8595. uint16_t u = 0;
  8596. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8597. int grid_index = kmap_q2xs[u];
  8598. if (grid_index < 0) {
  8599. printf("Oops: found point %u not on grid:", u);
  8600. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8601. printf("\n");
  8602. GGML_ASSERT(false);
  8603. }
  8604. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  8605. }
  8606. GGML_ASSERT(scale >= 0);
  8607. scales[ib] = scale;
  8608. max_scale = MAX(max_scale, scale);
  8609. }
  8610. if (!max_scale) {
  8611. memset(y[ibl].qs, 0, QK_K/4);
  8612. continue;
  8613. }
  8614. float d = max_scale/31;
  8615. y[ibl].d = GGML_FP32_TO_FP16(d);
  8616. float id = 1/d;
  8617. for (int ib = 0; ib < QK_K/16; ++ib) {
  8618. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8619. l = MAX(0, MIN(15, l));
  8620. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  8621. else y[ibl].scales[ib/2] |= (l << 4);
  8622. }
  8623. memcpy(y[ibl].qs, q2, QK_K/4);
  8624. }
  8625. }
  8626. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  8627. GGML_ASSERT(n_per_row%QK_K == 0);
  8628. int64_t nblock = n_per_row/QK_K;
  8629. char * qrow = (char *)dst;
  8630. for (int64_t row = 0; row < nrow; ++row) {
  8631. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  8632. src += n_per_row;
  8633. qrow += nblock*sizeof(block_iq2_xxs);
  8634. }
  8635. return nrow * nblock * sizeof(block_iq2_xxs);
  8636. }
  8637. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  8638. GGML_ASSERT(n_per_row%QK_K == 0);
  8639. int64_t nblock = n_per_row/QK_K;
  8640. char * qrow = (char *)dst;
  8641. for (int64_t row = 0; row < nrow; ++row) {
  8642. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  8643. src += n_per_row;
  8644. qrow += nblock*sizeof(block_iq2_xs);
  8645. }
  8646. return nrow * nblock * sizeof(block_iq2_xs);
  8647. }
  8648. //
  8649. // ============================================= 3-bit using D4 lattice
  8650. //
  8651. typedef struct {
  8652. uint32_t * grid;
  8653. int * map;
  8654. uint16_t * neighbours;
  8655. } iq3_entry_t;
  8656. static iq3_entry_t iq3_data[2] = {
  8657. {NULL, NULL, NULL},
  8658. {NULL, NULL, NULL},
  8659. };
  8660. static inline int iq3_data_index(int grid_size) {
  8661. (void)grid_size;
  8662. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8663. return grid_size == 256 ? 0 : 1;
  8664. }
  8665. static int iq3_compare_func(const void * left, const void * right) {
  8666. const int * l = (const int *)left;
  8667. const int * r = (const int *)right;
  8668. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8669. }
  8670. void iq3xs_init_impl(int grid_size) {
  8671. const int gindex = iq3_data_index(grid_size);
  8672. if (iq3_data[gindex].grid) {
  8673. return;
  8674. }
  8675. static const uint16_t kgrid_256[256] = {
  8676. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  8677. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  8678. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  8679. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  8680. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  8681. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  8682. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  8683. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  8684. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  8685. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  8686. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  8687. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  8688. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  8689. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  8690. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  8691. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  8692. };
  8693. static const uint16_t kgrid_512[512] = {
  8694. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  8695. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  8696. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  8697. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  8698. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  8699. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  8700. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  8701. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  8702. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  8703. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  8704. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  8705. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  8706. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  8707. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  8708. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  8709. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  8710. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  8711. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  8712. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  8713. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  8714. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  8715. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  8716. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  8717. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  8718. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  8719. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  8720. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  8721. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  8722. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  8723. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  8724. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  8725. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  8726. };
  8727. const int kmap_size = 4096;
  8728. const int nwant = grid_size == 256 ? 2 : 3;
  8729. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  8730. uint32_t * kgrid_q3xs;
  8731. int * kmap_q3xs;
  8732. uint16_t * kneighbors_q3xs;
  8733. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8734. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  8735. for (int k = 0; k < grid_size; ++k) {
  8736. int8_t * pos = (int8_t *)(the_grid + k);
  8737. for (int i = 0; i < 4; ++i) {
  8738. int l = (kgrid[k] >> 3*i) & 0x7;
  8739. pos[i] = 2*l + 1;
  8740. }
  8741. }
  8742. kgrid_q3xs = the_grid;
  8743. iq3_data[gindex].grid = the_grid;
  8744. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  8745. iq3_data[gindex].map = kmap_q3xs;
  8746. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  8747. uint32_t aux32;
  8748. uint8_t * aux8 = (uint8_t *)&aux32;
  8749. for (int i = 0; i < grid_size; ++i) {
  8750. aux32 = kgrid_q3xs[i];
  8751. uint16_t index = 0;
  8752. for (int k=0; k<4; ++k) {
  8753. uint16_t q = (aux8[k] - 1)/2;
  8754. index |= (q << 3*k);
  8755. }
  8756. kmap_q3xs[index] = i;
  8757. }
  8758. int8_t pos[4];
  8759. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8760. int num_neighbors = 0, num_not_in_map = 0;
  8761. for (int i = 0; i < kmap_size; ++i) {
  8762. if (kmap_q3xs[i] >= 0) continue;
  8763. ++num_not_in_map;
  8764. for (int k = 0; k < 4; ++k) {
  8765. int l = (i >> 3*k) & 0x7;
  8766. pos[k] = 2*l + 1;
  8767. }
  8768. for (int j = 0; j < grid_size; ++j) {
  8769. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8770. int d2 = 0;
  8771. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8772. dist2[2*j+0] = d2;
  8773. dist2[2*j+1] = j;
  8774. }
  8775. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8776. int n = 0; int d2 = dist2[0];
  8777. int nhave = 1;
  8778. for (int j = 0; j < grid_size; ++j) {
  8779. if (dist2[2*j] > d2) {
  8780. if (nhave == nwant) break;
  8781. d2 = dist2[2*j];
  8782. ++nhave;
  8783. }
  8784. ++n;
  8785. }
  8786. num_neighbors += n;
  8787. }
  8788. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8789. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8790. iq3_data[gindex].neighbours = kneighbors_q3xs;
  8791. int counter = 0;
  8792. for (int i = 0; i < kmap_size; ++i) {
  8793. if (kmap_q3xs[i] >= 0) continue;
  8794. for (int k = 0; k < 4; ++k) {
  8795. int l = (i >> 3*k) & 0x7;
  8796. pos[k] = 2*l + 1;
  8797. }
  8798. for (int j = 0; j < grid_size; ++j) {
  8799. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8800. int d2 = 0;
  8801. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8802. dist2[2*j+0] = d2;
  8803. dist2[2*j+1] = j;
  8804. }
  8805. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8806. kmap_q3xs[i] = -(counter + 1);
  8807. int d2 = dist2[0];
  8808. uint16_t * start = &kneighbors_q3xs[counter++];
  8809. int n = 0, nhave = 1;
  8810. for (int j = 0; j < grid_size; ++j) {
  8811. if (dist2[2*j] > d2) {
  8812. if (nhave == nwant) break;
  8813. d2 = dist2[2*j];
  8814. ++nhave;
  8815. }
  8816. kneighbors_q3xs[counter++] = dist2[2*j+1];
  8817. ++n;
  8818. }
  8819. *start = n;
  8820. }
  8821. free(dist2);
  8822. }
  8823. void iq3xs_free_impl(int grid_size) {
  8824. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8825. const int gindex = iq3_data_index(grid_size);
  8826. if (iq3_data[gindex].grid) {
  8827. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  8828. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  8829. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  8830. }
  8831. }
  8832. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  8833. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8834. int num_neighbors = neighbours[0];
  8835. GGML_ASSERT(num_neighbors > 0);
  8836. float best_d2 = FLT_MAX;
  8837. int grid_index = -1;
  8838. for (int j = 1; j <= num_neighbors; ++j) {
  8839. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8840. float d2 = 0;
  8841. for (int i = 0; i < 4; ++i) {
  8842. float q = pg[i];
  8843. float diff = scale*q - xval[i];
  8844. d2 += weight[i]*diff*diff;
  8845. }
  8846. if (d2 < best_d2) {
  8847. best_d2 = d2; grid_index = neighbours[j];
  8848. }
  8849. }
  8850. GGML_ASSERT(grid_index >= 0);
  8851. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8852. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  8853. return grid_index;
  8854. }
  8855. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int64_t n,
  8856. const float * restrict quant_weights) {
  8857. const int gindex = iq3_data_index(grid_size);
  8858. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  8859. const int * kmap_q3xs = iq3_data[gindex].map;
  8860. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  8861. //GGML_ASSERT(quant_weights && "missing quantization weights");
  8862. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  8863. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  8864. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  8865. GGML_ASSERT(n%QK_K == 0);
  8866. const int kMaxQ = 8;
  8867. const int64_t nbl = n/QK_K;
  8868. ggml_fp16_t * dh;
  8869. uint8_t * qs;
  8870. int block_size;
  8871. if (grid_size == 256) {
  8872. block_iq3_xxs * y = vy;
  8873. dh = &y->d;
  8874. qs = y->qs;
  8875. block_size = sizeof(block_iq3_xxs);
  8876. } else {
  8877. block_iq3_s * y = vy;
  8878. dh = &y->d;
  8879. qs = y->qs;
  8880. block_size = sizeof(block_iq3_s);
  8881. }
  8882. int quant_size = block_size - sizeof(ggml_fp16_t);
  8883. float scales[QK_K/32];
  8884. float weight[32];
  8885. float xval[32];
  8886. int8_t L[32];
  8887. int8_t Laux[32];
  8888. float waux[32];
  8889. bool is_on_grid[8];
  8890. bool is_on_grid_aux[8];
  8891. uint8_t block_signs[8];
  8892. uint8_t q3[3*(QK_K/8)+QK_K/32];
  8893. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  8894. uint8_t * qh = q3 + 3*(QK_K/8);
  8895. for (int ibl = 0; ibl < nbl; ++ibl) {
  8896. dh[0] = GGML_FP32_TO_FP16(0.f);
  8897. memset(q3, 0, 3*QK_K/8+QK_K/32);
  8898. float max_scale = 0;
  8899. const float * xbl = x + QK_K*ibl;
  8900. float sumx2 = 0;
  8901. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8902. float sigma2 = 2*sumx2/QK_K;
  8903. for (int ib = 0; ib < QK_K/32; ++ib) {
  8904. const float * xb = xbl + 32*ib;
  8905. if (quant_weights) {
  8906. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8907. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8908. } else {
  8909. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  8910. }
  8911. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8912. for (int k = 0; k < 4; ++k) {
  8913. int nflip = 0;
  8914. uint8_t s = 0;
  8915. for (int i = 0; i < 8; ++i) {
  8916. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8917. else {
  8918. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8919. }
  8920. }
  8921. if (nflip%2) {
  8922. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8923. for (int i = 1; i < 8; ++i) {
  8924. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8925. if (ax < min) {
  8926. min = ax; imin = i;
  8927. }
  8928. }
  8929. xval[8*k+imin] = -xval[8*k+imin];
  8930. s ^= (1 << imin);
  8931. }
  8932. block_signs[k] = s & 127;
  8933. }
  8934. float max = xval[0];
  8935. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8936. if (!max) {
  8937. scales[ib] = 0;
  8938. memset(L, 0, 32);
  8939. continue;
  8940. }
  8941. float best = 0;
  8942. float scale = max/(2*kMaxQ-1);
  8943. for (int is = -15; is <= 15; ++is) {
  8944. float id = (2*kMaxQ-1+is*0.2f)/max;
  8945. float this_scale = 1/id;
  8946. for (int k = 0; k < 8; ++k) {
  8947. for (int i = 0; i < 4; ++i) {
  8948. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8949. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8950. }
  8951. uint16_t u = 0;
  8952. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  8953. int grid_index = kmap_q3xs[u];
  8954. is_on_grid_aux[k] = true;
  8955. if (grid_index < 0) {
  8956. is_on_grid_aux[k] = false;
  8957. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8958. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  8959. }
  8960. }
  8961. float sumqx = 0, sumq2 = 0;
  8962. for (int i = 0; i < 32; ++i) {
  8963. float w = weight[i];
  8964. float q = 2*Laux[i] + 1;
  8965. sumqx += w*xval[i]*q;
  8966. sumq2 += w*q*q;
  8967. }
  8968. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8969. scale = sumqx/sumq2; best = scale*sumqx;
  8970. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  8971. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8972. }
  8973. }
  8974. int n_not_ongrid = 0;
  8975. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8976. if (n_not_ongrid > 0 && scale > 0) {
  8977. float id = 1/scale;
  8978. for (int k = 0; k < 8; ++k) {
  8979. if (is_on_grid[k]) continue;
  8980. uint16_t u = 0;
  8981. for (int i = 0; i < 4; ++i) {
  8982. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8983. l = MAX(0, MIN(kMaxQ-1, l));
  8984. u |= (l << 3*i);
  8985. }
  8986. int grid_index = kmap_q3xs[u];
  8987. if (grid_index < 0) {
  8988. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8989. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  8990. }
  8991. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  8992. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  8993. }
  8994. float sumqx = 0, sumq2 = 0;
  8995. for (int i = 0; i < 32; ++i) {
  8996. float w = weight[i];
  8997. float q = 2*L[i] + 1;
  8998. sumqx += w*xval[i]*q;
  8999. sumq2 += w*q*q;
  9000. }
  9001. if (sumq2 > 0) scale = sumqx/sumq2;
  9002. }
  9003. if (scale < 0) {
  9004. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9005. // and correspondingly flip quant signs.
  9006. scale = -scale;
  9007. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  9008. }
  9009. for (int k = 0; k < 8; ++k) {
  9010. uint16_t u = 0;
  9011. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9012. int grid_index = kmap_q3xs[u];
  9013. if (grid_index < 0) {
  9014. printf("Oops: found point %u not on grid:", u);
  9015. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9016. printf("\n");
  9017. GGML_ASSERT(false);
  9018. }
  9019. if (grid_size == 256) {
  9020. q3[8*ib+k] = grid_index;
  9021. } else {
  9022. q3[8*ib+k] = grid_index & 255;
  9023. qh[ib] |= ((grid_index >> 8) << k);
  9024. }
  9025. }
  9026. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  9027. GGML_ASSERT(scale >= 0);
  9028. scales[ib] = scale;
  9029. max_scale = MAX(max_scale, scale);
  9030. }
  9031. if (!max_scale) {
  9032. memset(qs, 0, quant_size);
  9033. dh += block_size/sizeof(ggml_fp16_t);
  9034. qs += block_size;
  9035. continue;
  9036. }
  9037. float d = max_scale/31;
  9038. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  9039. float id = 1/d;
  9040. for (int ib = 0; ib < QK_K/32; ++ib) {
  9041. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9042. l = MAX(0, MIN(15, l));
  9043. scales_and_signs[ib] |= ((uint32_t)l << 28);
  9044. }
  9045. memcpy(qs, q3, quant_size);
  9046. dh += block_size/sizeof(ggml_fp16_t);
  9047. qs += block_size;
  9048. }
  9049. }
  9050. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9051. GGML_ASSERT(n_per_row%QK_K == 0);
  9052. int64_t nblock = n_per_row/QK_K;
  9053. char * qrow = (char *)dst;
  9054. for (int64_t row = 0; row < nrow; ++row) {
  9055. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  9056. src += n_per_row;
  9057. qrow += nblock*sizeof(block_iq3_xxs);
  9058. }
  9059. return nrow * nblock * sizeof(block_iq3_xxs);
  9060. }
  9061. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int64_t k) {
  9062. assert(k % QK_K == 0);
  9063. block_iq3_xxs * restrict y = vy;
  9064. quantize_row_iq3_xxs_reference(x, y, k);
  9065. }
  9066. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int64_t k) {
  9067. assert(k % QK_K == 0);
  9068. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  9069. }
  9070. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  9071. const float * restrict quant_weights,
  9072. float * scales,
  9073. float * weight,
  9074. float * xval,
  9075. int8_t * L,
  9076. int8_t * Laux,
  9077. float * waux,
  9078. bool * is_on_grid,
  9079. bool * is_on_grid_aux,
  9080. uint8_t * block_signs) {
  9081. const int gindex = iq3_data_index(512);
  9082. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9083. const int * kmap_q3xs = iq3_data[gindex].map;
  9084. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9085. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9086. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9087. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9088. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9089. GGML_ASSERT(n%QK_K == 0);
  9090. const int kMaxQ = 8;
  9091. const int64_t nbl = n/QK_K;
  9092. block_iq3_s * y = vy;
  9093. const int bs4 = block_size/4;
  9094. const int bs8 = block_size/8;
  9095. for (int ibl = 0; ibl < nbl; ++ibl) {
  9096. memset(&y[ibl], 0, sizeof(block_iq3_s));
  9097. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9098. uint8_t * qs = y[ibl].qs;
  9099. uint8_t * qh = y[ibl].qh;
  9100. uint8_t * signs = y[ibl].signs;
  9101. float max_scale = 0;
  9102. const float * xbl = x + QK_K*ibl;
  9103. float sumx2 = 0;
  9104. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9105. float sigma2 = 2*sumx2/QK_K;
  9106. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9107. const float * xb = xbl + block_size*ib;
  9108. if (quant_weights) {
  9109. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9110. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9111. } else {
  9112. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9113. }
  9114. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  9115. for (int k = 0; k < bs8; ++k) {
  9116. uint8_t s = 0;
  9117. for (int i = 0; i < 8; ++i) {
  9118. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9119. else {
  9120. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9121. }
  9122. }
  9123. block_signs[k] = s;
  9124. }
  9125. float max = xval[0];
  9126. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  9127. if (!max) {
  9128. scales[ib] = 0;
  9129. continue;
  9130. }
  9131. float best = 0;
  9132. float scale = max/(2*kMaxQ-1);
  9133. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  9134. for (int is = -9; is <= 9; ++is) {
  9135. float id = (2*kMaxQ-1+is*0.2f)/max;
  9136. float this_scale = 1/id;
  9137. for (int k = 0; k < bs4; ++k) {
  9138. for (int i = 0; i < 4; ++i) {
  9139. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9140. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9141. }
  9142. uint16_t u = 0;
  9143. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9144. int grid_index = kmap_q3xs[u];
  9145. is_on_grid_aux[k] = true;
  9146. if (grid_index < 0) {
  9147. is_on_grid_aux[k] = false;
  9148. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9149. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9150. }
  9151. }
  9152. float sumqx = 0, sumq2 = 0;
  9153. for (int i = 0; i < block_size; ++i) {
  9154. float w = weight[i];
  9155. float q = 2*Laux[i] + 1;
  9156. sumqx += w*xval[i]*q;
  9157. sumq2 += w*q*q;
  9158. }
  9159. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9160. scale = sumqx/sumq2; best = scale*sumqx;
  9161. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  9162. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9163. }
  9164. }
  9165. int n_not_ongrid = 0;
  9166. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9167. if (n_not_ongrid > 0 && scale > 0) {
  9168. float id = 1/scale;
  9169. for (int k = 0; k < bs4; ++k) {
  9170. //if (is_on_grid[k]) continue;
  9171. uint16_t u = 0;
  9172. for (int i = 0; i < 4; ++i) {
  9173. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9174. l = MAX(0, MIN(kMaxQ-1, l));
  9175. u |= (l << 3*i);
  9176. }
  9177. int grid_index = kmap_q3xs[u];
  9178. if (grid_index < 0) {
  9179. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9180. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9181. }
  9182. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9183. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9184. }
  9185. float sumqx = 0, sumq2 = 0;
  9186. for (int i = 0; i < block_size; ++i) {
  9187. float w = weight[i];
  9188. float q = 2*L[i] + 1;
  9189. sumqx += w*xval[i]*q;
  9190. sumq2 += w*q*q;
  9191. }
  9192. if (sumq2 > 0) scale = sumqx/sumq2;
  9193. }
  9194. if (scale < 0) {
  9195. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9196. // and correspondingly flip quant signs.
  9197. scale = -scale;
  9198. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  9199. }
  9200. for (int k = 0; k < bs4; ++k) {
  9201. uint16_t u = 0;
  9202. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9203. int grid_index = kmap_q3xs[u];
  9204. if (grid_index < 0) {
  9205. printf("Oops: found point %u not on grid:", u);
  9206. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9207. printf("\n");
  9208. GGML_ASSERT(false);
  9209. }
  9210. qs[k] = grid_index & 255;
  9211. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  9212. }
  9213. qs += bs4;
  9214. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  9215. signs += bs8;
  9216. GGML_ASSERT(scale >= 0);
  9217. scales[ib] = scale;
  9218. max_scale = MAX(max_scale, scale);
  9219. }
  9220. if (!max_scale) {
  9221. continue;
  9222. }
  9223. float d = max_scale/31;
  9224. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  9225. float id = 1/d;
  9226. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  9227. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  9228. l1 = MAX(0, MIN(15, l1));
  9229. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  9230. l2 = MAX(0, MIN(15, l2));
  9231. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  9232. }
  9233. }
  9234. }
  9235. #define IQ3S_BLOCK_SIZE 32
  9236. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9237. GGML_ASSERT(n_per_row%QK_K == 0);
  9238. int64_t nblock = n_per_row/QK_K;
  9239. float scales[QK_K/IQ3S_BLOCK_SIZE];
  9240. float weight[IQ3S_BLOCK_SIZE];
  9241. float xval[IQ3S_BLOCK_SIZE];
  9242. int8_t L[IQ3S_BLOCK_SIZE];
  9243. int8_t Laux[IQ3S_BLOCK_SIZE];
  9244. float waux[IQ3S_BLOCK_SIZE];
  9245. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  9246. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  9247. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  9248. char * qrow = (char *)dst;
  9249. for (int64_t row = 0; row < nrow; ++row) {
  9250. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  9251. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  9252. src += n_per_row;
  9253. qrow += nblock*sizeof(block_iq3_s);
  9254. }
  9255. return nrow * nblock * sizeof(block_iq3_s);
  9256. }
  9257. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int64_t k) {
  9258. assert(k % QK_K == 0);
  9259. block_iq3_s * restrict y = vy;
  9260. quantize_row_iq3_s_reference(x, y, k);
  9261. }
  9262. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int64_t k) {
  9263. assert(k % QK_K == 0);
  9264. quantize_iq3_s(x, y, 1, k, NULL);
  9265. }
  9266. // =================================== 1.5 bpw ===================================================
  9267. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9268. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  9269. int num_neighbors = neighbours[0];
  9270. GGML_ASSERT(num_neighbors > 0);
  9271. float best_score = 0;
  9272. int grid_index = -1;
  9273. for (int j = 1; j <= num_neighbors; ++j) {
  9274. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9275. float sumqx = 0, sumq2 = 0;
  9276. for (int i = 0; i < 8; ++i) {
  9277. float q = (pg[i] - 3)/2;
  9278. float w = weight[i];
  9279. sumqx += w*q*xval[i];
  9280. sumq2 += w*q*q;
  9281. }
  9282. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9283. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  9284. grid_index = neighbours[j];
  9285. }
  9286. }
  9287. if (grid_index < 0) {
  9288. for (int i = 0; i < ngrid; ++i) {
  9289. const int8_t * grid_i = (const int8_t *)(grid + i);
  9290. float sumqx = 0, sumq2 = 0;
  9291. for (int j = 0; j < 8; ++j) {
  9292. float w = weight[j];
  9293. float q = (grid_i[j] - 3)/2;
  9294. sumqx += w*q*xval[j];
  9295. sumq2 += w*q*q;
  9296. }
  9297. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9298. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  9299. grid_index = i;
  9300. }
  9301. }
  9302. }
  9303. if (grid_index < 0) {
  9304. printf("Oops, did not find grid point\n");
  9305. printf("Have %d neighbours\n", num_neighbors);
  9306. for (int j = 1; j <= num_neighbors; ++j) {
  9307. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9308. float sumqx = 0, sumq2 = 0;
  9309. for (int i = 0; i < 8; ++i) {
  9310. float q = (pg[i] - 3)/2;
  9311. float w = weight[i];
  9312. sumqx += w*q*xval[i];
  9313. sumq2 += w*q*q;
  9314. }
  9315. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9316. }
  9317. }
  9318. GGML_ASSERT(grid_index >= 0);
  9319. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9320. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  9321. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9322. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9323. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9324. return grid_index;
  9325. }
  9326. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9327. const float * restrict xval, const float * restrict weight, float scale, const float * restrict xg, int8_t * restrict L, int ngrid) {
  9328. int num_neighbors = neighbours[0];
  9329. GGML_ASSERT(num_neighbors > 0);
  9330. float best_score = FLT_MAX;
  9331. int grid_index = -1;
  9332. for (int j = 1; j <= num_neighbors; ++j) {
  9333. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9334. float d2 = 0;
  9335. for (int i = 0; i < 8; ++i) {
  9336. float q = xg[(pg[i] - 1)/2];
  9337. float w = weight[i];
  9338. float diff = scale*q - xval[i];
  9339. d2 += w*diff*diff;
  9340. }
  9341. if (d2 < best_score) {
  9342. best_score = d2;
  9343. grid_index = neighbours[j];
  9344. }
  9345. }
  9346. if (grid_index < 0) {
  9347. for (int i = 0; i < ngrid; ++i) {
  9348. const int8_t * grid_i = (const int8_t *)(grid + i);
  9349. float d2 = 0;
  9350. for (int j = 0; j < 8; ++j) {
  9351. float w = weight[j];
  9352. float q = xg[(grid_i[j] - 1)/2];
  9353. float diff = scale*q - xval[i];
  9354. d2 += w*diff*diff;
  9355. }
  9356. if (d2 < best_score) {
  9357. best_score = d2;
  9358. grid_index = i;
  9359. }
  9360. }
  9361. }
  9362. if (grid_index < 0) {
  9363. printf("Oops, did not find grid point\n");
  9364. printf("Have %d neighbours\n", num_neighbors);
  9365. for (int j = 1; j <= num_neighbors; ++j) {
  9366. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9367. float sumqx = 0, sumq2 = 0;
  9368. for (int i = 0; i < 8; ++i) {
  9369. float q = xg[(pg[i] - 1)/2];
  9370. float w = weight[i];
  9371. sumqx += w*q*xval[i];
  9372. sumq2 += w*q*q;
  9373. }
  9374. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9375. }
  9376. }
  9377. GGML_ASSERT(grid_index >= 0);
  9378. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9379. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9380. return grid_index;
  9381. }
  9382. static int iq1_sort_helper(const void * left, const void * right) {
  9383. const float * l = left;
  9384. const float * r = right;
  9385. return *l < *r ? -1 : *l > *r ? 1 : 0;
  9386. }
  9387. #define IQ1S_BLOCK_SIZE 32
  9388. #define IQ1M_BLOCK_SIZE 16
  9389. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  9390. float * scales,
  9391. float * weight,
  9392. float * sumx,
  9393. float * sumw,
  9394. float * pairs,
  9395. int8_t * L,
  9396. uint16_t * index,
  9397. int8_t * shifts) {
  9398. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  9399. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9400. const int * kmap_q2xs = iq2_data[gindex].map;
  9401. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9402. GGML_ASSERT(quant_weights && "missing quantization weights");
  9403. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9404. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9405. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9406. GGML_ASSERT(n%QK_K == 0);
  9407. block_iq1_s * y = vy;
  9408. const int64_t nbl = n/QK_K;
  9409. const int block_size = IQ1S_BLOCK_SIZE;
  9410. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  9411. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  9412. int * idx = (int *)(pairs + 1);
  9413. for (int ibl = 0; ibl < nbl; ++ibl) {
  9414. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9415. memset(y[ibl].qs, 0, QK_K/8);
  9416. memset(y[ibl].qh, 0, QK_K/16);
  9417. float max_scale = 0;
  9418. const float * xbl = x + QK_K*ibl;
  9419. float sumx2 = 0;
  9420. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9421. float sigma2 = 2*sumx2/QK_K;
  9422. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9423. const float * xb = xbl + block_size*ib;
  9424. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9425. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9426. float max = fabsf(xb[0]);
  9427. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9428. if (!max) {
  9429. scales[ib] = 0;
  9430. memset(L, 1, block_size);
  9431. continue;
  9432. }
  9433. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9434. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9435. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9436. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9437. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9438. // for each possible and score for each split.
  9439. for (int j = 0; j < block_size; ++j) {
  9440. pairs[2*j] = xb[j];
  9441. idx[2*j] = j;
  9442. }
  9443. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9444. {
  9445. sumx[0] = sumw[0] = 0;
  9446. for (int j = 0; j < block_size; ++j) {
  9447. int i = idx[2*j];
  9448. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  9449. sumw[j+1] = sumw[j] + weight[i];
  9450. }
  9451. }
  9452. float best_score = 0, scale = max;
  9453. int besti1 = -1, besti2 = -1, best_shift = 0;
  9454. for (int i1 = 0; i1 <= block_size; ++i1) {
  9455. for (int i2 = i1; i2 <= block_size; ++i2) {
  9456. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
  9457. float sumq2 = (sumw[i1] - sumw[0])*x_p[0]*x_p[0] + (sumw[i2] - sumw[i1])*x_p[1]*x_p[1] + (sumw[block_size] - sumw[i2])*x_p[2]*x_p[2];
  9458. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9459. scale = sumqx/sumq2; best_score = scale*sumqx;
  9460. besti1 = i1; besti2 = i2; best_shift = 1;
  9461. }
  9462. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
  9463. sumq2 = (sumw[i1] - sumw[0])*x_m[0]*x_m[0] + (sumw[i2] - sumw[i1])*x_m[1]*x_m[1] + (sumw[block_size] - sumw[i2])*x_m[2]*x_m[2];
  9464. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9465. scale = sumqx/sumq2; best_score = scale*sumqx;
  9466. besti1 = i1; besti2 = i2; best_shift = -1;
  9467. }
  9468. }
  9469. }
  9470. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  9471. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9472. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9473. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9474. if (scale < 0) {
  9475. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9476. scale = -scale; best_shift = -best_shift;
  9477. }
  9478. bool all_on_grid = true;
  9479. const float * xx = best_shift == 1 ? x_p : x_m;
  9480. for (int k = 0; k < block_size/8; ++k) {
  9481. uint16_t u = 0;
  9482. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9483. int grid_index = kmap_q2xs[u];
  9484. if (grid_index < 0) {
  9485. all_on_grid = false;
  9486. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9487. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9488. GGML_ASSERT(grid_index >= 0);
  9489. }
  9490. index[k] = grid_index;
  9491. }
  9492. if (!all_on_grid) {
  9493. float sumqx = 0, sumq2 = 0;
  9494. for (int k = 0; k < block_size/8; ++k) {
  9495. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9496. for (int j = 0; j < 8; ++j) {
  9497. float w = weight[8*k + j];
  9498. float q = xx[(pg[j] - 1)/2];
  9499. sumqx += w*q*xb[8*k+j];
  9500. sumq2 += w*q*q;
  9501. }
  9502. }
  9503. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  9504. }
  9505. uint16_t h = 0;
  9506. for (int k = 0; k < block_size/8; ++k) {
  9507. y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
  9508. h |= (index[k] >> 8) << 3*k;
  9509. }
  9510. y[ibl].qh[ib] = h;
  9511. GGML_ASSERT(scale >= 0);
  9512. scales[ib] = scale;
  9513. shifts[ib] = best_shift;
  9514. max_scale = MAX(max_scale, scale);
  9515. }
  9516. if (!max_scale) {
  9517. continue;
  9518. }
  9519. float d = max_scale/15;
  9520. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
  9521. float id = 1/d;
  9522. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9523. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9524. l = MAX(0, MIN(7, l));
  9525. if (shifts[ib] == -1) l |= 8;
  9526. y[ibl].qh[ib] |= (l << 12);
  9527. }
  9528. }
  9529. }
  9530. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9531. GGML_ASSERT(n_per_row%QK_K == 0);
  9532. float scales[QK_K/IQ1S_BLOCK_SIZE];
  9533. float weight[IQ1S_BLOCK_SIZE];
  9534. int8_t L[IQ1S_BLOCK_SIZE];
  9535. float sumx[IQ1S_BLOCK_SIZE+1];
  9536. float sumw[IQ1S_BLOCK_SIZE+1];
  9537. float pairs[2*IQ1S_BLOCK_SIZE];
  9538. uint16_t index[IQ1S_BLOCK_SIZE/8];
  9539. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  9540. int64_t nblock = n_per_row/QK_K;
  9541. char * qrow = (char *)dst;
  9542. for (int64_t row = 0; row < nrow; ++row) {
  9543. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
  9544. src += n_per_row;
  9545. qrow += nblock*sizeof(block_iq1_s);
  9546. }
  9547. return nrow * nblock * sizeof(block_iq1_s);
  9548. }
  9549. static void quantize_row_iq1_m_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights,
  9550. float * scales,
  9551. float * weight,
  9552. float * pairs,
  9553. int8_t * L,
  9554. uint16_t * index,
  9555. int8_t * shifts) {
  9556. const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
  9557. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9558. const int * kmap_q2xs = iq2_data[gindex].map;
  9559. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9560. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9561. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9562. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9563. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9564. GGML_ASSERT(n%QK_K == 0);
  9565. block_iq1_m * y = vy;
  9566. const int64_t nbl = n/QK_K;
  9567. const int block_size = IQ1M_BLOCK_SIZE;
  9568. const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
  9569. const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
  9570. const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
  9571. int * idx = (int *)(pairs + 1);
  9572. float sumqx[4], sumq2[4];
  9573. iq1m_scale_t s;
  9574. const float * xx;
  9575. for (int ibl = 0; ibl < nbl; ++ibl) {
  9576. #if QK_K == 64
  9577. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9578. #endif
  9579. memset(y[ibl].qs, 0, QK_K/8);
  9580. memset(y[ibl].qh, 0, QK_K/16);
  9581. memset(y[ibl].scales, 0, QK_K/32);
  9582. float max_scale = 0;
  9583. const float * xbl = x + QK_K*ibl;
  9584. float sumx2 = 0;
  9585. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9586. float sigma2 = 2*sumx2/QK_K;
  9587. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9588. const float * xb = xbl + block_size*ib;
  9589. if (quant_weights) {
  9590. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9591. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9592. } else {
  9593. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9594. }
  9595. float max = fabsf(xb[0]);
  9596. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9597. if (!max) {
  9598. scales[ib] = 0;
  9599. memset(L, 1, block_size);
  9600. continue;
  9601. }
  9602. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9603. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9604. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9605. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9606. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9607. // for each possible and score for each split.
  9608. for (int j = 0; j < block_size; ++j) {
  9609. pairs[2*j] = xb[j];
  9610. idx[2*j] = j;
  9611. }
  9612. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9613. float best_score = 0, scale = max;
  9614. int besti1 = -1, besti2 = -1, best_k = -1;
  9615. // 0: +, +
  9616. // 1: +, -
  9617. // 2: -, +
  9618. // 3: -, -
  9619. for (int i1 = 0; i1 <= block_size; ++i1) {
  9620. for (int i2 = i1; i2 <= block_size; ++i2) {
  9621. memset(sumqx, 0, 4*sizeof(float));
  9622. memset(sumq2, 0, 4*sizeof(float));
  9623. for (int j = 0; j < i1; ++j) {
  9624. int i = idx[2*j];
  9625. if (i < block_size/2) {
  9626. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9627. sumqx[1] += weight[i]*x_p[0]*xb[i];
  9628. sumqx[2] += weight[i]*x_m[0]*xb[i];
  9629. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9630. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9631. sumq2[1] += weight[i]*x_p[0]*x_p[0];
  9632. sumq2[2] += weight[i]*x_m[0]*x_m[0];
  9633. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9634. } else {
  9635. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9636. sumqx[2] += weight[i]*x_p[0]*xb[i];
  9637. sumqx[1] += weight[i]*x_m[0]*xb[i];
  9638. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9639. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9640. sumq2[2] += weight[i]*x_p[0]*x_p[0];
  9641. sumq2[1] += weight[i]*x_m[0]*x_m[0];
  9642. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9643. }
  9644. }
  9645. for (int j = i1; j < i2; ++j) {
  9646. int i = idx[2*j];
  9647. if (i < block_size/2) {
  9648. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9649. sumqx[1] += weight[i]*x_p[1]*xb[i];
  9650. sumqx[2] += weight[i]*x_m[1]*xb[i];
  9651. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9652. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9653. sumq2[1] += weight[i]*x_p[1]*x_p[1];
  9654. sumq2[2] += weight[i]*x_m[1]*x_m[1];
  9655. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9656. } else {
  9657. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9658. sumqx[2] += weight[i]*x_p[1]*xb[i];
  9659. sumqx[1] += weight[i]*x_m[1]*xb[i];
  9660. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9661. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9662. sumq2[2] += weight[i]*x_p[1]*x_p[1];
  9663. sumq2[1] += weight[i]*x_m[1]*x_m[1];
  9664. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9665. }
  9666. }
  9667. for (int j = i2; j < block_size; ++j) {
  9668. int i = idx[2*j];
  9669. if (i < block_size/2) {
  9670. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9671. sumqx[1] += weight[i]*x_p[2]*xb[i];
  9672. sumqx[2] += weight[i]*x_m[2]*xb[i];
  9673. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9674. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9675. sumq2[1] += weight[i]*x_p[2]*x_p[2];
  9676. sumq2[2] += weight[i]*x_m[2]*x_m[2];
  9677. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9678. } else {
  9679. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9680. sumqx[2] += weight[i]*x_p[2]*xb[i];
  9681. sumqx[1] += weight[i]*x_m[2]*xb[i];
  9682. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9683. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9684. sumq2[2] += weight[i]*x_p[2]*x_p[2];
  9685. sumq2[1] += weight[i]*x_m[2]*x_m[2];
  9686. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9687. }
  9688. }
  9689. for (int k = 0; k < 4; ++k) {
  9690. if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
  9691. scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
  9692. besti1 = i1; besti2 = i2; best_k = k;
  9693. }
  9694. }
  9695. }
  9696. }
  9697. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_k >= 0);
  9698. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9699. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9700. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9701. if (scale < 0) {
  9702. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9703. scale = -scale;
  9704. best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
  9705. }
  9706. bool all_on_grid = true;
  9707. for (int k = 0; k < block_size/8; ++k) {
  9708. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9709. else xx = best_k%2 == 0 ? x_p : x_m;
  9710. uint16_t u = 0;
  9711. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9712. int grid_index = kmap_q2xs[u];
  9713. if (grid_index < 0) {
  9714. all_on_grid = false;
  9715. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9716. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9717. GGML_ASSERT(grid_index >= 0);
  9718. }
  9719. index[k] = grid_index;
  9720. }
  9721. if (!all_on_grid) {
  9722. float sumqx_f = 0, sumq2_f = 0;
  9723. for (int k = 0; k < block_size/8; ++k) {
  9724. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9725. else xx = best_k%2 == 0 ? x_p : x_m;
  9726. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9727. for (int j = 0; j < 8; ++j) {
  9728. float w = weight[8*k + j];
  9729. float q = xx[(pg[j] - 1)/2];
  9730. sumqx_f += w*q*xb[8*k+j];
  9731. sumq2_f += w*q*q;
  9732. }
  9733. }
  9734. if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
  9735. }
  9736. y[ibl].qs[2*ib + 0] = index[0] & 255;
  9737. y[ibl].qs[2*ib + 1] = index[1] & 255;
  9738. y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
  9739. GGML_ASSERT(scale >= 0);
  9740. scales[ib] = scale;
  9741. shifts[ib] = best_k;
  9742. max_scale = MAX(max_scale, scale);
  9743. }
  9744. if (!max_scale) {
  9745. continue;
  9746. }
  9747. uint16_t * sc = (uint16_t *)y[ibl].scales;
  9748. #if QK_K == 64
  9749. float d = max_scale/31;
  9750. #else
  9751. float d = max_scale/15;
  9752. #endif
  9753. float id = 1/d;
  9754. float sumqx_f = 0, sumq2_f = 0;
  9755. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9756. int l = nearest_int(0.5f*(id*scales[ib+0]-1));
  9757. #if QK_K == 64
  9758. l = MAX(0, MIN(15, l));
  9759. sc[ib/4] |= (l << 4*(ib%4));
  9760. #else
  9761. l = MAX(0, MIN(7, l));
  9762. sc[ib/4] |= (l << 3*(ib%4));
  9763. #endif
  9764. y[ibl].qh[ib] |= masks[shifts[ib]];
  9765. const float * xb = xbl + block_size*ib;
  9766. if (quant_weights) {
  9767. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9768. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9769. } else {
  9770. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9771. }
  9772. for (int k = 0; k < block_size/8; ++k) {
  9773. if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
  9774. else xx = shifts[ib]%2 == 0 ? x_p : x_m;
  9775. const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
  9776. for (int j = 0; j < 8; ++j) {
  9777. float w = weight[8*k + j];
  9778. float q = xx[(pg[j] - 1)/2]*(2*l+1);
  9779. sumqx_f += w*q*xb[8*k+j];
  9780. sumq2_f += w*q*q;
  9781. }
  9782. }
  9783. }
  9784. if (sumq2_f > 0) d = sumqx_f/sumq2_f;
  9785. s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
  9786. #if QK_K == 64
  9787. y[ibl].d = s.f16;
  9788. #else
  9789. sc[0] |= ((s.u16 & 0x000f) << 12);
  9790. sc[1] |= ((s.u16 & 0x00f0) << 8);
  9791. sc[2] |= ((s.u16 & 0x0f00) << 4);
  9792. sc[3] |= ((s.u16 & 0xf000) << 0);
  9793. #endif
  9794. }
  9795. }
  9796. size_t quantize_iq1_m(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9797. GGML_ASSERT(n_per_row%QK_K == 0);
  9798. float scales[QK_K/IQ1M_BLOCK_SIZE];
  9799. float weight[IQ1M_BLOCK_SIZE];
  9800. int8_t L[IQ1M_BLOCK_SIZE];
  9801. float pairs[2*IQ1M_BLOCK_SIZE];
  9802. uint16_t index[IQ1M_BLOCK_SIZE/8];
  9803. int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
  9804. int64_t nblock = n_per_row/QK_K;
  9805. char * qrow = (char *)dst;
  9806. for (int64_t row = 0; row < nrow; ++row) {
  9807. quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
  9808. src += n_per_row;
  9809. qrow += nblock*sizeof(block_iq1_m);
  9810. }
  9811. return nrow * nblock * sizeof(block_iq1_m);
  9812. }
  9813. // ============================ 4-bit non-linear quants
  9814. static inline int best_index_int8(int n, const int8_t * val, float x) {
  9815. if (x <= val[0]) return 0;
  9816. if (x >= val[n-1]) return n-1;
  9817. int ml = 0, mu = n-1;
  9818. while (mu-ml > 1) {
  9819. int mav = (ml+mu)/2;
  9820. if (x < val[mav]) mu = mav; else ml = mav;
  9821. }
  9822. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  9823. }
  9824. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  9825. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  9826. float * scales, float * weight, uint8_t * L,
  9827. const int8_t * values,
  9828. const float * quant_weights,
  9829. const int ntry) {
  9830. float sigma2 = 0;
  9831. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  9832. sigma2 *= 2.f/super_block_size;
  9833. memset(q4, 0, super_block_size/2);
  9834. dh[0] = GGML_FP32_TO_FP16(0.f);
  9835. float max_scale = 0, amax_scale = 0;
  9836. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9837. const float * xb = x + ib*block_size;
  9838. uint8_t * Lb = L + ib*block_size;
  9839. if (quant_weights) {
  9840. const float * qw = quant_weights + ib*block_size;
  9841. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  9842. } else {
  9843. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  9844. }
  9845. float amax = 0, max = 0;
  9846. for (int j = 0; j < block_size; ++j) {
  9847. float ax = fabsf(xb[j]);
  9848. if (ax > amax) {
  9849. amax = ax; max = xb[j];
  9850. }
  9851. }
  9852. if (!amax) {
  9853. scales[ib] = 0;
  9854. continue;
  9855. }
  9856. float d = ntry > 0 ? -max/values[0] : max/values[0];
  9857. float id = 1/d;
  9858. float sumqx = 0, sumq2 = 0;
  9859. for (int j = 0; j < block_size; ++j) {
  9860. float al = id*xb[j];
  9861. int l = best_index_int8(16, values, al);
  9862. Lb[j] = l;
  9863. float q = values[l];
  9864. float w = weight[j];
  9865. sumqx += w*q*xb[j];
  9866. sumq2 += w*q*q;
  9867. }
  9868. d = sumqx/sumq2;
  9869. float best = d*sumqx;
  9870. for (int itry = -ntry; itry <= ntry; ++itry) {
  9871. id = (itry + values[0])/max;
  9872. sumqx = sumq2 = 0;
  9873. for (int j = 0; j < block_size; ++j) {
  9874. float al = id*xb[j];
  9875. int l = best_index_int8(16, values, al);
  9876. float q = values[l];
  9877. float w = weight[j];
  9878. sumqx += w*q*xb[j];
  9879. sumq2 += w*q*q;
  9880. }
  9881. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9882. d = sumqx/sumq2; best = d * sumqx;
  9883. }
  9884. }
  9885. scales[ib] = d;
  9886. float abs_d = fabsf(d);
  9887. if (abs_d > amax_scale) {
  9888. amax_scale = abs_d; max_scale = d;
  9889. }
  9890. }
  9891. if (super_block_size/block_size > 1) {
  9892. int nb = super_block_size/block_size;
  9893. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  9894. float d = -max_scale/32;
  9895. dh[0] = GGML_FP32_TO_FP16(d);
  9896. float id = d ? 1/d : 0.f;
  9897. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9898. int l = nearest_int(id*scales[ib]);
  9899. l = MAX(-32, MIN(31, l));
  9900. float dl = d * l;
  9901. float idl = dl ? 1/dl : 0.f;
  9902. uint8_t * Lb = L + ib*block_size;
  9903. const float * xb = x + ib*block_size;
  9904. for (int j = 0; j < block_size; ++j) {
  9905. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  9906. }
  9907. l += 32;
  9908. uint8_t l_l = l & 0xf;
  9909. uint8_t l_h = l >> 4;
  9910. if (ib%2 == 0) scales_l[ib/2] = l_l;
  9911. else scales_l[ib/2] |= (l_l << 4);
  9912. scales_h[ib/8] |= (l_h << 2*(ib%8));
  9913. }
  9914. } else {
  9915. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  9916. if (ntry > 0) {
  9917. float id = scales[0] ? 1/scales[0] : 0;
  9918. for (int j = 0; j < super_block_size; ++j) {
  9919. L[j] = best_index_int8(16, values, id*x[j]);
  9920. }
  9921. }
  9922. }
  9923. for (int i = 0; i < super_block_size/32; ++i) {
  9924. for (int j = 0; j < 16; ++j) {
  9925. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  9926. }
  9927. }
  9928. }
  9929. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9930. GGML_ASSERT(n_per_row%QK4_NL == 0);
  9931. int64_t nblock = n_per_row/QK4_NL;
  9932. char * qrow = (char *)dst;
  9933. uint8_t L[QK4_NL];
  9934. float weight[QK4_NL];
  9935. uint16_t unused_h;
  9936. uint8_t * unused_l = NULL;
  9937. float scale;
  9938. for (int64_t row = 0; row < nrow; ++row) {
  9939. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  9940. for (int ibl = 0; ibl < nblock; ++ibl) {
  9941. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  9942. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9943. &scale, weight, L, kvalues_iq4nl, qw, 7);
  9944. }
  9945. src += n_per_row;
  9946. qrow += nblock*sizeof(block_iq4_nl);
  9947. }
  9948. return nrow * nblock * sizeof(block_iq4_nl);
  9949. }
  9950. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int64_t k) {
  9951. GGML_ASSERT(k%QK4_NL == 0);
  9952. int64_t nblock = k/QK4_NL;
  9953. uint8_t L[QK4_NL];
  9954. float weight[QK4_NL];
  9955. uint16_t unused_h;
  9956. uint8_t * unused_l = NULL;
  9957. float scale;
  9958. block_iq4_nl * iq4 = (block_iq4_nl *)vy;
  9959. for (int ibl = 0; ibl < nblock; ++ibl) {
  9960. quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9961. &scale, weight, L, kvalues_iq4nl, NULL, -1);
  9962. }
  9963. }
  9964. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int64_t k) {
  9965. assert(k % QK4_NL == 0);
  9966. quantize_row_iq4_nl(x, y, k);
  9967. }
  9968. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  9969. #if QK_K == 64
  9970. return quantize_iq4_nl(src, dst, nrow, n_per_row, quant_weights);
  9971. #else
  9972. GGML_ASSERT(n_per_row%QK_K == 0);
  9973. int64_t nblock = n_per_row/QK_K;
  9974. char * qrow = (char *)dst;
  9975. uint8_t L[QK_K];
  9976. float weight[32];
  9977. float scales[QK_K/32];
  9978. for (int64_t row = 0; row < nrow; ++row) {
  9979. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  9980. for (int ibl = 0; ibl < nblock; ++ibl) {
  9981. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  9982. quantize_row_iq4_nl_impl(QK_K, 32, src + QK_K*ibl, &iq4[ibl].d, iq4[ibl].qs, &iq4[ibl].scales_h, iq4[ibl].scales_l,
  9983. scales, weight, L, kvalues_iq4nl, qw, 7);
  9984. }
  9985. src += n_per_row;
  9986. qrow += nblock*sizeof(block_iq4_xs);
  9987. }
  9988. return nrow * nblock * sizeof(block_iq4_xs);
  9989. #endif
  9990. }
  9991. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int64_t k) {
  9992. assert(k % QK_K == 0);
  9993. block_iq4_xs * restrict y = vy;
  9994. quantize_row_iq4_xs_reference(x, y, k);
  9995. }
  9996. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int64_t k) {
  9997. assert(k % QK_K == 0);
  9998. quantize_iq4_xs(x, y, 1, k, NULL);
  9999. }
  10000. // =============================== 2.5625 bpw
  10001. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int64_t n, const float * restrict quant_weights) {
  10002. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  10003. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  10004. const int * kmap_q2xs = iq2_data[gindex].map;
  10005. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  10006. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  10007. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  10008. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  10009. GGML_ASSERT(n%QK_K == 0);
  10010. const int kMaxQ = 3;
  10011. const int64_t nbl = n/QK_K;
  10012. block_iq2_s * y = vy;
  10013. float scales[QK_K/16];
  10014. float weight[16];
  10015. float xval[16];
  10016. int8_t L[16];
  10017. int8_t Laux[16];
  10018. float waux[16];
  10019. bool is_on_grid[2];
  10020. bool is_on_grid_aux[2];
  10021. uint8_t block_signs[2];
  10022. for (int ibl = 0; ibl < nbl; ++ibl) {
  10023. memset(&y[ibl], 0, sizeof(block_iq2_s));
  10024. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  10025. float max_scale = 0;
  10026. const float * xbl = x + QK_K*ibl;
  10027. float sumx2 = 0;
  10028. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10029. float sigma2 = 2*sumx2/QK_K;
  10030. for (int ib = 0; ib < QK_K/16; ++ib) {
  10031. const float * xb = xbl + 16*ib;
  10032. if (quant_weights) {
  10033. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  10034. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10035. } else {
  10036. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  10037. }
  10038. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  10039. for (int k = 0; k < 2; ++k) {
  10040. uint8_t s = 0;
  10041. for (int i = 0; i < 8; ++i) {
  10042. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  10043. else {
  10044. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  10045. }
  10046. }
  10047. block_signs[k] = s;
  10048. }
  10049. float max = xval[0];
  10050. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  10051. if (!max) {
  10052. scales[ib] = 0;
  10053. continue;
  10054. }
  10055. float best = 0;
  10056. float scale = max/(2*kMaxQ-1);
  10057. is_on_grid[0] = is_on_grid[1] = true;
  10058. for (int is = -9; is <= 9; ++is) {
  10059. float id = (2*kMaxQ-1+is*0.1f)/max;
  10060. float this_scale = 1/id;
  10061. for (int k = 0; k < 2; ++k) {
  10062. for (int i = 0; i < 8; ++i) {
  10063. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10064. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  10065. }
  10066. uint16_t u = 0;
  10067. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  10068. int grid_index = kmap_q2xs[u];
  10069. is_on_grid_aux[k] = true;
  10070. if (grid_index < 0) {
  10071. is_on_grid_aux[k] = false;
  10072. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10073. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  10074. }
  10075. }
  10076. float sumqx = 0, sumq2 = 0;
  10077. for (int i = 0; i < 16; ++i) {
  10078. float w = weight[i];
  10079. float q = 2*Laux[i] + 1;
  10080. sumqx += w*xval[i]*q;
  10081. sumq2 += w*q*q;
  10082. }
  10083. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10084. scale = sumqx/sumq2; best = scale*sumqx;
  10085. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  10086. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  10087. }
  10088. }
  10089. int n_not_ongrid = 0;
  10090. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  10091. if (n_not_ongrid > 0 && scale > 0) {
  10092. float id = 1/scale;
  10093. for (int k = 0; k < 2; ++k) {
  10094. if (is_on_grid[k]) continue;
  10095. uint16_t u = 0;
  10096. for (int i = 0; i < 8; ++i) {
  10097. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10098. l = MAX(0, MIN(kMaxQ-1, l));
  10099. u |= (l << 2*i);
  10100. L[8*k + i] = l;
  10101. }
  10102. int grid_index = kmap_q2xs[u];
  10103. if (grid_index < 0) {
  10104. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10105. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  10106. }
  10107. }
  10108. float sumqx = 0, sumq2 = 0;
  10109. for (int i = 0; i < 16; ++i) {
  10110. float w = weight[i];
  10111. float q = 2*L[i] + 1;
  10112. sumqx += w*xval[i]*q;
  10113. sumq2 += w*q*q;
  10114. }
  10115. if (sumq2 > 0) scale = sumqx/sumq2;
  10116. }
  10117. if (scale < 0) {
  10118. scale = -scale;
  10119. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  10120. }
  10121. for (int k = 0; k < 2; ++k) {
  10122. uint16_t u = 0;
  10123. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  10124. int grid_index = kmap_q2xs[u];
  10125. if (grid_index < 0) {
  10126. printf("Oops: found point %u not on grid:", u);
  10127. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  10128. printf("\n");
  10129. GGML_ASSERT(false);
  10130. }
  10131. const int i8 = 2*ib + k;
  10132. y[ibl].qs[i8] = grid_index & 255;
  10133. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  10134. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  10135. }
  10136. GGML_ASSERT(scale >= 0);
  10137. scales[ib] = scale;
  10138. max_scale = MAX(max_scale, scale);
  10139. }
  10140. if (!max_scale) {
  10141. continue;
  10142. }
  10143. float d = max_scale/31;
  10144. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  10145. float id = 1/d;
  10146. for (int ib = 0; ib < QK_K/16; ++ib) {
  10147. int l = nearest_int(0.5f*(id*scales[ib]-1));
  10148. l = MAX(0, MIN(15, l));
  10149. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  10150. else y[ibl].scales[ib/2] |= (l << 4);
  10151. }
  10152. }
  10153. }
  10154. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int64_t nrow, int64_t n_per_row, const float * quant_weights) {
  10155. GGML_ASSERT(n_per_row%QK_K == 0);
  10156. int64_t nblock = n_per_row/QK_K;
  10157. char * qrow = (char *)dst;
  10158. for (int64_t row = 0; row < nrow; ++row) {
  10159. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  10160. src += n_per_row;
  10161. qrow += nblock*sizeof(block_iq2_s);
  10162. }
  10163. return nrow * nblock * sizeof(block_iq2_s);
  10164. }
  10165. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int64_t k) {
  10166. assert(k % QK_K == 0);
  10167. quantize_iq2_s(x, y, 1, k, NULL);
  10168. }
  10169. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int64_t k) {
  10170. assert(k % QK_K == 0);
  10171. block_iq2_s * restrict y = vy;
  10172. quantize_row_iq2_s_reference(x, y, k);
  10173. }