ggml-quants.c 512 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610
  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. #ifdef __ARM_NEON
  14. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  15. //
  16. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  17. //
  18. #include <arm_neon.h>
  19. #else
  20. #ifdef __wasm_simd128__
  21. #include <wasm_simd128.h>
  22. #else
  23. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  24. #include <altivec.h>
  25. #undef bool
  26. #define bool _Bool
  27. #else
  28. #if defined(_MSC_VER) || defined(__MINGW32__)
  29. #include <intrin.h>
  30. #else
  31. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  32. #if !defined(__riscv)
  33. #include <immintrin.h>
  34. #endif
  35. #endif
  36. #endif
  37. #endif
  38. #endif
  39. #endif
  40. #ifdef __riscv_v_intrinsic
  41. #include <riscv_vector.h>
  42. #endif
  43. #undef MIN
  44. #undef MAX
  45. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  46. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  47. #define UNUSED GGML_UNUSED
  48. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  49. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  50. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  51. // multiply int8_t, add results pairwise twice
  52. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  53. // Get absolute values of x vectors
  54. const __m128i ax = _mm_sign_epi8(x, x);
  55. // Sign the values of the y vectors
  56. const __m128i sy = _mm_sign_epi8(y, x);
  57. // Perform multiplication and create 16-bit values
  58. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  59. const __m128i ones = _mm_set1_epi16(1);
  60. return _mm_madd_epi16(ones, dot);
  61. }
  62. #if __AVX__ || __AVX2__ || __AVX512F__
  63. // horizontally add 8 floats
  64. static inline float hsum_float_8(const __m256 x) {
  65. __m128 res = _mm256_extractf128_ps(x, 1);
  66. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  67. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  68. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  69. return _mm_cvtss_f32(res);
  70. }
  71. // horizontally add 8 int32_t
  72. static inline int hsum_i32_8(const __m256i a) {
  73. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  74. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  75. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  76. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  77. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  78. }
  79. // horizontally add 4 int32_t
  80. static inline int hsum_i32_4(const __m128i a) {
  81. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  82. const __m128i sum64 = _mm_add_epi32(hi64, a);
  83. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  84. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  85. }
  86. #if defined(__AVX2__) || defined(__AVX512F__)
  87. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  88. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  89. uint32_t x32;
  90. memcpy(&x32, x, sizeof(uint32_t));
  91. const __m256i shuf_mask = _mm256_set_epi64x(
  92. 0x0303030303030303, 0x0202020202020202,
  93. 0x0101010101010101, 0x0000000000000000);
  94. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  95. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  96. bytes = _mm256_or_si256(bytes, bit_mask);
  97. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  98. }
  99. // Unpack 32 4-bit fields into 32 bytes
  100. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  101. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  102. {
  103. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  104. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  105. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  106. return _mm256_and_si256(lowMask, bytes);
  107. }
  108. // add int16_t pairwise and return as float vector
  109. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  110. const __m256i ones = _mm256_set1_epi16(1);
  111. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  112. return _mm256_cvtepi32_ps(summed_pairs);
  113. }
  114. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  115. #if defined(__AVXVNNI__) || defined(__AVX512VNNI__)
  116. const __m256i zero = _mm256_setzero_si256();
  117. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  118. return _mm256_cvtepi32_ps(summed_pairs);
  119. #else
  120. // Perform multiplication and create 16-bit values
  121. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  122. return sum_i16_pairs_float(dot);
  123. #endif
  124. }
  125. // multiply int8_t, add results pairwise twice and return as float vector
  126. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  127. #if __AVXVNNIINT8__
  128. const __m256i zero = _mm256_setzero_si256();
  129. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  130. return _mm256_cvtepi32_ps(summed_pairs);
  131. #else
  132. // Get absolute values of x vectors
  133. const __m256i ax = _mm256_sign_epi8(x, x);
  134. // Sign the values of the y vectors
  135. const __m256i sy = _mm256_sign_epi8(y, x);
  136. return mul_sum_us8_pairs_float(ax, sy);
  137. #endif
  138. }
  139. static inline __m128i packNibbles( __m256i bytes )
  140. {
  141. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  142. #if __AVX512F__
  143. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  144. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  145. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  146. #else
  147. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  148. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  149. __m256i low = _mm256_and_si256( lowByte, bytes );
  150. high = _mm256_srli_epi16( high, 4 );
  151. bytes = _mm256_or_si256( low, high );
  152. // Compress uint16_t lanes into bytes
  153. __m128i r0 = _mm256_castsi256_si128( bytes );
  154. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  155. return _mm_packus_epi16( r0, r1 );
  156. #endif
  157. }
  158. #elif defined(__AVX__)
  159. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  160. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  161. uint32_t x32;
  162. memcpy(&x32, x, sizeof(uint32_t));
  163. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  164. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  165. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  166. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  167. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  168. bytesl = _mm_or_si128(bytesl, bit_mask);
  169. bytesh = _mm_or_si128(bytesh, bit_mask);
  170. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  171. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  172. return MM256_SET_M128I(bytesh, bytesl);
  173. }
  174. // Unpack 32 4-bit fields into 32 bytes
  175. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  176. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  177. {
  178. // Load 16 bytes from memory
  179. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  180. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  181. const __m128i lowMask = _mm_set1_epi8(0xF);
  182. tmpl = _mm_and_si128(lowMask, tmpl);
  183. tmph = _mm_and_si128(lowMask, tmph);
  184. return MM256_SET_M128I(tmph, tmpl);
  185. }
  186. // add int16_t pairwise and return as float vector
  187. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  188. const __m128i ones = _mm_set1_epi16(1);
  189. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  190. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  191. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  192. return _mm256_cvtepi32_ps(summed_pairs);
  193. }
  194. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  195. const __m128i axl = _mm256_castsi256_si128(ax);
  196. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  197. const __m128i syl = _mm256_castsi256_si128(sy);
  198. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  199. // Perform multiplication and create 16-bit values
  200. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  201. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  202. return sum_i16_pairs_float(doth, dotl);
  203. }
  204. // multiply int8_t, add results pairwise twice and return as float vector
  205. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  206. const __m128i xl = _mm256_castsi256_si128(x);
  207. const __m128i xh = _mm256_extractf128_si256(x, 1);
  208. const __m128i yl = _mm256_castsi256_si128(y);
  209. const __m128i yh = _mm256_extractf128_si256(y, 1);
  210. // Get absolute values of x vectors
  211. const __m128i axl = _mm_sign_epi8(xl, xl);
  212. const __m128i axh = _mm_sign_epi8(xh, xh);
  213. // Sign the values of the y vectors
  214. const __m128i syl = _mm_sign_epi8(yl, xl);
  215. const __m128i syh = _mm_sign_epi8(yh, xh);
  216. // Perform multiplication and create 16-bit values
  217. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  218. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  219. return sum_i16_pairs_float(doth, dotl);
  220. }
  221. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  222. {
  223. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  224. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  225. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  226. __m128i low = _mm_and_si128( lowByte, bytes1 );
  227. high = _mm_srli_epi16( high, 4 );
  228. bytes1 = _mm_or_si128( low, high );
  229. high = _mm_andnot_si128( lowByte, bytes2 );
  230. low = _mm_and_si128( lowByte, bytes2 );
  231. high = _mm_srli_epi16( high, 4 );
  232. bytes2 = _mm_or_si128( low, high );
  233. return _mm_packus_epi16( bytes1, bytes2);
  234. }
  235. #endif
  236. #elif defined(__SSSE3__)
  237. // horizontally add 4x4 floats
  238. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  239. __m128 res_0 =_mm_hadd_ps(a, b);
  240. __m128 res_1 =_mm_hadd_ps(c, d);
  241. __m128 res =_mm_hadd_ps(res_0, res_1);
  242. res =_mm_hadd_ps(res, res);
  243. res =_mm_hadd_ps(res, res);
  244. return _mm_cvtss_f32(res);
  245. }
  246. #endif // __AVX__ || __AVX2__ || __AVX512F__
  247. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  248. #if defined(__ARM_NEON)
  249. #ifdef _MSC_VER
  250. #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
  251. #else
  252. #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
  253. #endif
  254. #if !defined(__aarch64__)
  255. // 64-bit compatibility
  256. // vaddvq_s16
  257. // vpaddq_s16
  258. // vpaddq_s32
  259. // vaddvq_s32
  260. // vaddvq_f32
  261. // vmaxvq_f32
  262. // vcvtnq_s32_f32
  263. // vzip1_u8
  264. // vzip2_u8
  265. inline static int32_t vaddvq_s16(int16x8_t v) {
  266. return
  267. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  268. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  269. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  270. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  271. }
  272. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  273. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  274. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  275. return vcombine_s16(a0, b0);
  276. }
  277. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  278. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  279. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  280. return vcombine_s32(a0, b0);
  281. }
  282. inline static int32_t vaddvq_s32(int32x4_t v) {
  283. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  284. }
  285. inline static float vaddvq_f32(float32x4_t v) {
  286. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  287. }
  288. inline static float vmaxvq_f32(float32x4_t v) {
  289. return
  290. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  291. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  292. }
  293. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  294. int32x4_t res;
  295. res[0] = roundf(vgetq_lane_f32(v, 0));
  296. res[1] = roundf(vgetq_lane_f32(v, 1));
  297. res[2] = roundf(vgetq_lane_f32(v, 2));
  298. res[3] = roundf(vgetq_lane_f32(v, 3));
  299. return res;
  300. }
  301. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  302. uint8x8_t res;
  303. res[0] = a[0]; res[1] = b[0];
  304. res[2] = a[1]; res[3] = b[1];
  305. res[4] = a[2]; res[5] = b[2];
  306. res[6] = a[3]; res[7] = b[3];
  307. return res;
  308. }
  309. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  310. uint8x8_t res;
  311. res[0] = a[4]; res[1] = b[4];
  312. res[2] = a[5]; res[3] = b[5];
  313. res[4] = a[6]; res[5] = b[6];
  314. res[6] = a[7]; res[7] = b[7];
  315. return res;
  316. }
  317. // vld1q_s16_x2
  318. // vld1q_u8_x2
  319. // vld1q_u8_x4
  320. // vld1q_s8_x2
  321. // vld1q_s8_x4
  322. // TODO: double-check these work correctly
  323. typedef struct ggml_int16x8x2_t {
  324. int16x8_t val[2];
  325. } ggml_int16x8x2_t;
  326. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  327. ggml_int16x8x2_t res;
  328. res.val[0] = vld1q_s16(ptr + 0);
  329. res.val[1] = vld1q_s16(ptr + 8);
  330. return res;
  331. }
  332. typedef struct ggml_uint8x16x2_t {
  333. uint8x16_t val[2];
  334. } ggml_uint8x16x2_t;
  335. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  336. ggml_uint8x16x2_t res;
  337. res.val[0] = vld1q_u8(ptr + 0);
  338. res.val[1] = vld1q_u8(ptr + 16);
  339. return res;
  340. }
  341. typedef struct ggml_uint8x16x4_t {
  342. uint8x16_t val[4];
  343. } ggml_uint8x16x4_t;
  344. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  345. ggml_uint8x16x4_t res;
  346. res.val[0] = vld1q_u8(ptr + 0);
  347. res.val[1] = vld1q_u8(ptr + 16);
  348. res.val[2] = vld1q_u8(ptr + 32);
  349. res.val[3] = vld1q_u8(ptr + 48);
  350. return res;
  351. }
  352. typedef struct ggml_int8x16x2_t {
  353. int8x16_t val[2];
  354. } ggml_int8x16x2_t;
  355. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  356. ggml_int8x16x2_t res;
  357. res.val[0] = vld1q_s8(ptr + 0);
  358. res.val[1] = vld1q_s8(ptr + 16);
  359. return res;
  360. }
  361. typedef struct ggml_int8x16x4_t {
  362. int8x16_t val[4];
  363. } ggml_int8x16x4_t;
  364. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  365. ggml_int8x16x4_t res;
  366. res.val[0] = vld1q_s8(ptr + 0);
  367. res.val[1] = vld1q_s8(ptr + 16);
  368. res.val[2] = vld1q_s8(ptr + 32);
  369. res.val[3] = vld1q_s8(ptr + 48);
  370. return res;
  371. }
  372. // NOTE: not tested
  373. inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
  374. int8x16_t res;
  375. res[ 0] = a[b[ 0]];
  376. res[ 1] = a[b[ 1]];
  377. res[ 2] = a[b[ 2]];
  378. res[ 3] = a[b[ 3]];
  379. res[ 4] = a[b[ 4]];
  380. res[ 5] = a[b[ 5]];
  381. res[ 6] = a[b[ 6]];
  382. res[ 7] = a[b[ 7]];
  383. res[ 8] = a[b[ 8]];
  384. res[ 9] = a[b[ 9]];
  385. res[10] = a[b[10]];
  386. res[11] = a[b[11]];
  387. res[12] = a[b[12]];
  388. res[13] = a[b[13]];
  389. res[14] = a[b[14]];
  390. res[15] = a[b[15]];
  391. return res;
  392. }
  393. // NOTE: not tested
  394. inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
  395. uint8x16_t res;
  396. res[ 0] = a[b[ 0]];
  397. res[ 1] = a[b[ 1]];
  398. res[ 2] = a[b[ 2]];
  399. res[ 3] = a[b[ 3]];
  400. res[ 4] = a[b[ 4]];
  401. res[ 5] = a[b[ 5]];
  402. res[ 6] = a[b[ 6]];
  403. res[ 7] = a[b[ 7]];
  404. res[ 8] = a[b[ 8]];
  405. res[ 9] = a[b[ 9]];
  406. res[10] = a[b[10]];
  407. res[11] = a[b[11]];
  408. res[12] = a[b[12]];
  409. res[13] = a[b[13]];
  410. res[14] = a[b[14]];
  411. res[15] = a[b[15]];
  412. return res;
  413. }
  414. #else
  415. #define ggml_int16x8x2_t int16x8x2_t
  416. #define ggml_uint8x16x2_t uint8x16x2_t
  417. #define ggml_uint8x16x4_t uint8x16x4_t
  418. #define ggml_int8x16x2_t int8x16x2_t
  419. #define ggml_int8x16x4_t int8x16x4_t
  420. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  421. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  422. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  423. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  424. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  425. #define ggml_vqtbl1q_s8 vqtbl1q_s8
  426. #define ggml_vqtbl1q_u8 vqtbl1q_u8
  427. #endif
  428. #if !defined(__ARM_FEATURE_DOTPROD)
  429. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  430. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  431. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  432. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  433. }
  434. #else
  435. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  436. #endif
  437. #endif
  438. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  439. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  440. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  441. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  442. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  443. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  444. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  445. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  446. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  447. // precomputed tables for expanding 8bits to 8 bytes:
  448. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  449. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  450. #endif
  451. // reference implementation for deterministic creation of model files
  452. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  453. static const int qk = QK4_0;
  454. assert(k % qk == 0);
  455. const int nb = k / qk;
  456. for (int i = 0; i < nb; i++) {
  457. float amax = 0.0f; // absolute max
  458. float max = 0.0f;
  459. for (int j = 0; j < qk; j++) {
  460. const float v = x[i*qk + j];
  461. if (amax < fabsf(v)) {
  462. amax = fabsf(v);
  463. max = v;
  464. }
  465. }
  466. const float d = max / -8;
  467. const float id = d ? 1.0f/d : 0.0f;
  468. y[i].d = GGML_FP32_TO_FP16(d);
  469. for (int j = 0; j < qk/2; ++j) {
  470. const float x0 = x[i*qk + 0 + j]*id;
  471. const float x1 = x[i*qk + qk/2 + j]*id;
  472. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  473. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  474. y[i].qs[j] = xi0;
  475. y[i].qs[j] |= xi1 << 4;
  476. }
  477. }
  478. }
  479. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  480. quantize_row_q4_0_reference(x, y, k);
  481. }
  482. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  483. const int qk = QK4_1;
  484. assert(k % qk == 0);
  485. const int nb = k / qk;
  486. for (int i = 0; i < nb; i++) {
  487. float min = FLT_MAX;
  488. float max = -FLT_MAX;
  489. for (int j = 0; j < qk; j++) {
  490. const float v = x[i*qk + j];
  491. if (v < min) min = v;
  492. if (v > max) max = v;
  493. }
  494. const float d = (max - min) / ((1 << 4) - 1);
  495. const float id = d ? 1.0f/d : 0.0f;
  496. y[i].d = GGML_FP32_TO_FP16(d);
  497. y[i].m = GGML_FP32_TO_FP16(min);
  498. for (int j = 0; j < qk/2; ++j) {
  499. const float x0 = (x[i*qk + 0 + j] - min)*id;
  500. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  501. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  502. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  503. y[i].qs[j] = xi0;
  504. y[i].qs[j] |= xi1 << 4;
  505. }
  506. }
  507. }
  508. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  509. quantize_row_q4_1_reference(x, y, k);
  510. }
  511. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  512. static const int qk = QK5_0;
  513. assert(k % qk == 0);
  514. const int nb = k / qk;
  515. for (int i = 0; i < nb; i++) {
  516. float amax = 0.0f; // absolute max
  517. float max = 0.0f;
  518. for (int j = 0; j < qk; j++) {
  519. const float v = x[i*qk + j];
  520. if (amax < fabsf(v)) {
  521. amax = fabsf(v);
  522. max = v;
  523. }
  524. }
  525. const float d = max / -16;
  526. const float id = d ? 1.0f/d : 0.0f;
  527. y[i].d = GGML_FP32_TO_FP16(d);
  528. uint32_t qh = 0;
  529. for (int j = 0; j < qk/2; ++j) {
  530. const float x0 = x[i*qk + 0 + j]*id;
  531. const float x1 = x[i*qk + qk/2 + j]*id;
  532. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  533. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  534. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  535. // get the 5-th bit and store it in qh at the right position
  536. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  537. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  538. }
  539. memcpy(&y[i].qh, &qh, sizeof(qh));
  540. }
  541. }
  542. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  543. quantize_row_q5_0_reference(x, y, k);
  544. }
  545. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  546. const int qk = QK5_1;
  547. assert(k % qk == 0);
  548. const int nb = k / qk;
  549. for (int i = 0; i < nb; i++) {
  550. float min = FLT_MAX;
  551. float max = -FLT_MAX;
  552. for (int j = 0; j < qk; j++) {
  553. const float v = x[i*qk + j];
  554. if (v < min) min = v;
  555. if (v > max) max = v;
  556. }
  557. const float d = (max - min) / ((1 << 5) - 1);
  558. const float id = d ? 1.0f/d : 0.0f;
  559. y[i].d = GGML_FP32_TO_FP16(d);
  560. y[i].m = GGML_FP32_TO_FP16(min);
  561. uint32_t qh = 0;
  562. for (int j = 0; j < qk/2; ++j) {
  563. const float x0 = (x[i*qk + 0 + j] - min)*id;
  564. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  565. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  566. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  567. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  568. // get the 5-th bit and store it in qh at the right position
  569. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  570. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  571. }
  572. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  573. }
  574. }
  575. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  576. quantize_row_q5_1_reference(x, y, k);
  577. }
  578. // reference implementation for deterministic creation of model files
  579. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  580. assert(k % QK8_0 == 0);
  581. const int nb = k / QK8_0;
  582. for (int i = 0; i < nb; i++) {
  583. float amax = 0.0f; // absolute max
  584. for (int j = 0; j < QK8_0; j++) {
  585. const float v = x[i*QK8_0 + j];
  586. amax = MAX(amax, fabsf(v));
  587. }
  588. const float d = amax / ((1 << 7) - 1);
  589. const float id = d ? 1.0f/d : 0.0f;
  590. y[i].d = GGML_FP32_TO_FP16(d);
  591. for (int j = 0; j < QK8_0; ++j) {
  592. const float x0 = x[i*QK8_0 + j]*id;
  593. y[i].qs[j] = roundf(x0);
  594. }
  595. }
  596. }
  597. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  598. assert(QK8_0 == 32);
  599. assert(k % QK8_0 == 0);
  600. const int nb = k / QK8_0;
  601. block_q8_0 * restrict y = vy;
  602. #if defined(__ARM_NEON)
  603. for (int i = 0; i < nb; i++) {
  604. float32x4_t srcv [8];
  605. float32x4_t asrcv[8];
  606. float32x4_t amaxv[8];
  607. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  608. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  609. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  610. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  611. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  612. const float amax = vmaxvq_f32(amaxv[0]);
  613. const float d = amax / ((1 << 7) - 1);
  614. const float id = d ? 1.0f/d : 0.0f;
  615. y[i].d = GGML_FP32_TO_FP16(d);
  616. for (int j = 0; j < 8; j++) {
  617. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  618. const int32x4_t vi = vcvtnq_s32_f32(v);
  619. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  620. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  621. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  622. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  623. }
  624. }
  625. #elif defined(__wasm_simd128__)
  626. for (int i = 0; i < nb; i++) {
  627. v128_t srcv [8];
  628. v128_t asrcv[8];
  629. v128_t amaxv[8];
  630. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  631. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  632. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  633. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  634. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  635. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  636. wasm_f32x4_extract_lane(amaxv[0], 1)),
  637. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  638. wasm_f32x4_extract_lane(amaxv[0], 3)));
  639. const float d = amax / ((1 << 7) - 1);
  640. const float id = d ? 1.0f/d : 0.0f;
  641. y[i].d = GGML_FP32_TO_FP16(d);
  642. for (int j = 0; j < 8; j++) {
  643. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  644. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  645. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  646. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  647. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  648. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  649. }
  650. }
  651. #elif defined(__AVX2__) || defined(__AVX__)
  652. for (int i = 0; i < nb; i++) {
  653. // Load elements into 4 AVX vectors
  654. __m256 v0 = _mm256_loadu_ps( x );
  655. __m256 v1 = _mm256_loadu_ps( x + 8 );
  656. __m256 v2 = _mm256_loadu_ps( x + 16 );
  657. __m256 v3 = _mm256_loadu_ps( x + 24 );
  658. x += 32;
  659. // Compute max(abs(e)) for the block
  660. const __m256 signBit = _mm256_set1_ps( -0.0f );
  661. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  662. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  663. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  664. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  665. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  666. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  667. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  668. const float maxScalar = _mm_cvtss_f32( max4 );
  669. // Quantize these floats
  670. const float d = maxScalar / 127.f;
  671. y[i].d = GGML_FP32_TO_FP16(d);
  672. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  673. const __m256 mul = _mm256_set1_ps( id );
  674. // Apply the multiplier
  675. v0 = _mm256_mul_ps( v0, mul );
  676. v1 = _mm256_mul_ps( v1, mul );
  677. v2 = _mm256_mul_ps( v2, mul );
  678. v3 = _mm256_mul_ps( v3, mul );
  679. // Round to nearest integer
  680. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  681. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  682. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  683. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  684. // Convert floats to integers
  685. __m256i i0 = _mm256_cvtps_epi32( v0 );
  686. __m256i i1 = _mm256_cvtps_epi32( v1 );
  687. __m256i i2 = _mm256_cvtps_epi32( v2 );
  688. __m256i i3 = _mm256_cvtps_epi32( v3 );
  689. #if defined(__AVX2__)
  690. // Convert int32 to int16
  691. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  692. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  693. // Convert int16 to int8
  694. 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
  695. // We got our precious signed bytes, but the order is now wrong
  696. // These AVX2 pack instructions process 16-byte pieces independently
  697. // The following instruction is fixing the order
  698. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  699. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  700. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  701. #else
  702. // Since we don't have in AVX some necessary functions,
  703. // we split the registers in half and call AVX2 analogs from SSE
  704. __m128i ni0 = _mm256_castsi256_si128( i0 );
  705. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  706. __m128i ni2 = _mm256_castsi256_si128( i1 );
  707. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  708. __m128i ni4 = _mm256_castsi256_si128( i2 );
  709. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  710. __m128i ni6 = _mm256_castsi256_si128( i3 );
  711. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  712. // Convert int32 to int16
  713. ni0 = _mm_packs_epi32( ni0, ni1 );
  714. ni2 = _mm_packs_epi32( ni2, ni3 );
  715. ni4 = _mm_packs_epi32( ni4, ni5 );
  716. ni6 = _mm_packs_epi32( ni6, ni7 );
  717. // Convert int16 to int8
  718. ni0 = _mm_packs_epi16( ni0, ni2 );
  719. ni4 = _mm_packs_epi16( ni4, ni6 );
  720. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  721. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  722. #endif
  723. }
  724. #elif defined(__riscv_v_intrinsic)
  725. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  726. for (int i = 0; i < nb; i++) {
  727. // load elements
  728. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  729. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  730. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  731. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  732. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  733. const float d = amax / ((1 << 7) - 1);
  734. const float id = d ? 1.0f/d : 0.0f;
  735. y[i].d = GGML_FP32_TO_FP16(d);
  736. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  737. // convert to integer
  738. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  739. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  740. // store result
  741. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  742. }
  743. #else
  744. GGML_UNUSED(nb);
  745. // scalar
  746. quantize_row_q8_0_reference(x, y, k);
  747. #endif
  748. }
  749. // reference implementation for deterministic creation of model files
  750. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  751. assert(QK8_1 == 32);
  752. assert(k % QK8_1 == 0);
  753. const int nb = k / QK8_1;
  754. for (int i = 0; i < nb; i++) {
  755. float amax = 0.0f; // absolute max
  756. for (int j = 0; j < QK8_1; j++) {
  757. const float v = x[i*QK8_1 + j];
  758. amax = MAX(amax, fabsf(v));
  759. }
  760. const float d = amax / ((1 << 7) - 1);
  761. const float id = d ? 1.0f/d : 0.0f;
  762. y[i].d = GGML_FP32_TO_FP16(d);
  763. int sum = 0;
  764. for (int j = 0; j < QK8_1/2; ++j) {
  765. const float v0 = x[i*QK8_1 + j]*id;
  766. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  767. y[i].qs[ j] = roundf(v0);
  768. y[i].qs[QK8_1/2 + j] = roundf(v1);
  769. sum += y[i].qs[ j];
  770. sum += y[i].qs[QK8_1/2 + j];
  771. }
  772. y[i].s = GGML_FP32_TO_FP16(sum*d);
  773. }
  774. }
  775. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  776. assert(k % QK8_1 == 0);
  777. const int nb = k / QK8_1;
  778. block_q8_1 * restrict y = vy;
  779. #if defined(__ARM_NEON)
  780. for (int i = 0; i < nb; i++) {
  781. float32x4_t srcv [8];
  782. float32x4_t asrcv[8];
  783. float32x4_t amaxv[8];
  784. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  785. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  786. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  787. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  788. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  789. const float amax = vmaxvq_f32(amaxv[0]);
  790. const float d = amax / ((1 << 7) - 1);
  791. const float id = d ? 1.0f/d : 0.0f;
  792. y[i].d = GGML_FP32_TO_FP16(d);
  793. int32x4_t accv = vdupq_n_s32(0);
  794. for (int j = 0; j < 8; j++) {
  795. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  796. const int32x4_t vi = vcvtnq_s32_f32(v);
  797. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  798. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  799. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  800. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  801. accv = vaddq_s32(accv, vi);
  802. }
  803. y[i].s = GGML_FP32_TO_FP16(d * vaddvq_s32(accv));
  804. }
  805. #elif defined(__wasm_simd128__)
  806. for (int i = 0; i < nb; i++) {
  807. v128_t srcv [8];
  808. v128_t asrcv[8];
  809. v128_t amaxv[8];
  810. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  811. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  812. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  813. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  814. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  815. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  816. wasm_f32x4_extract_lane(amaxv[0], 1)),
  817. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  818. wasm_f32x4_extract_lane(amaxv[0], 3)));
  819. const float d = amax / ((1 << 7) - 1);
  820. const float id = d ? 1.0f/d : 0.0f;
  821. y[i].d = GGML_FP32_TO_FP16(d);
  822. v128_t accv = wasm_i32x4_splat(0);
  823. for (int j = 0; j < 8; j++) {
  824. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  825. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  826. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  827. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  828. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  829. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  830. accv = wasm_i32x4_add(accv, vi);
  831. }
  832. y[i].s = GGML_FP32_TO_FP16(
  833. d * (wasm_i32x4_extract_lane(accv, 0) +
  834. wasm_i32x4_extract_lane(accv, 1) +
  835. wasm_i32x4_extract_lane(accv, 2) +
  836. wasm_i32x4_extract_lane(accv, 3)));
  837. }
  838. #elif defined(__AVX2__) || defined(__AVX__)
  839. for (int i = 0; i < nb; i++) {
  840. // Load elements into 4 AVX vectors
  841. __m256 v0 = _mm256_loadu_ps( x );
  842. __m256 v1 = _mm256_loadu_ps( x + 8 );
  843. __m256 v2 = _mm256_loadu_ps( x + 16 );
  844. __m256 v3 = _mm256_loadu_ps( x + 24 );
  845. x += 32;
  846. // Compute max(abs(e)) for the block
  847. const __m256 signBit = _mm256_set1_ps( -0.0f );
  848. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  849. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  850. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  851. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  852. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  853. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  854. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  855. const float maxScalar = _mm_cvtss_f32( max4 );
  856. // Quantize these floats
  857. const float d = maxScalar / 127.f;
  858. y[i].d = GGML_FP32_TO_FP16(d);
  859. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  860. const __m256 mul = _mm256_set1_ps( id );
  861. // Apply the multiplier
  862. v0 = _mm256_mul_ps( v0, mul );
  863. v1 = _mm256_mul_ps( v1, mul );
  864. v2 = _mm256_mul_ps( v2, mul );
  865. v3 = _mm256_mul_ps( v3, mul );
  866. // Round to nearest integer
  867. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  868. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  869. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  870. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  871. // Convert floats to integers
  872. __m256i i0 = _mm256_cvtps_epi32( v0 );
  873. __m256i i1 = _mm256_cvtps_epi32( v1 );
  874. __m256i i2 = _mm256_cvtps_epi32( v2 );
  875. __m256i i3 = _mm256_cvtps_epi32( v3 );
  876. #if defined(__AVX2__)
  877. // Compute the sum of the quants and set y[i].s
  878. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3))));
  879. // Convert int32 to int16
  880. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  881. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  882. // Convert int16 to int8
  883. 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
  884. // We got our precious signed bytes, but the order is now wrong
  885. // These AVX2 pack instructions process 16-byte pieces independently
  886. // The following instruction is fixing the order
  887. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  888. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  889. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  890. #else
  891. // Since we don't have in AVX some necessary functions,
  892. // we split the registers in half and call AVX2 analogs from SSE
  893. __m128i ni0 = _mm256_castsi256_si128( i0 );
  894. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  895. __m128i ni2 = _mm256_castsi256_si128( i1 );
  896. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  897. __m128i ni4 = _mm256_castsi256_si128( i2 );
  898. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  899. __m128i ni6 = _mm256_castsi256_si128( i3 );
  900. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  901. // Compute the sum of the quants and set y[i].s
  902. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  903. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  904. y[i].s = GGML_FP32_TO_FP16(d * hsum_i32_4(_mm_add_epi32(s0, s1)));
  905. // Convert int32 to int16
  906. ni0 = _mm_packs_epi32( ni0, ni1 );
  907. ni2 = _mm_packs_epi32( ni2, ni3 );
  908. ni4 = _mm_packs_epi32( ni4, ni5 );
  909. ni6 = _mm_packs_epi32( ni6, ni7 );
  910. // Convert int16 to int8
  911. ni0 = _mm_packs_epi16( ni0, ni2 );
  912. ni4 = _mm_packs_epi16( ni4, ni6 );
  913. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  914. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  915. #endif
  916. }
  917. #elif defined(__riscv_v_intrinsic)
  918. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  919. for (int i = 0; i < nb; i++) {
  920. // load elements
  921. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  922. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  923. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  924. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  925. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  926. const float d = amax / ((1 << 7) - 1);
  927. const float id = d ? 1.0f/d : 0.0f;
  928. y[i].d = GGML_FP32_TO_FP16(d);
  929. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  930. // convert to integer
  931. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  932. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  933. // store result
  934. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  935. // compute sum for y[i].s
  936. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  937. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  938. // set y[i].s
  939. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  940. y[i].s = GGML_FP32_TO_FP16(sum*d);
  941. }
  942. #else
  943. GGML_UNUSED(nb);
  944. // scalar
  945. quantize_row_q8_1_reference(x, y, k);
  946. #endif
  947. }
  948. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  949. static const int qk = QK4_0;
  950. assert(k % qk == 0);
  951. const int nb = k / qk;
  952. for (int i = 0; i < nb; i++) {
  953. const float d = GGML_FP16_TO_FP32(x[i].d);
  954. for (int j = 0; j < qk/2; ++j) {
  955. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  956. const int x1 = (x[i].qs[j] >> 4) - 8;
  957. y[i*qk + j + 0 ] = x0*d;
  958. y[i*qk + j + qk/2] = x1*d;
  959. }
  960. }
  961. }
  962. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  963. static const int qk = QK4_1;
  964. assert(k % qk == 0);
  965. const int nb = k / qk;
  966. for (int i = 0; i < nb; i++) {
  967. const float d = GGML_FP16_TO_FP32(x[i].d);
  968. const float m = GGML_FP16_TO_FP32(x[i].m);
  969. for (int j = 0; j < qk/2; ++j) {
  970. const int x0 = (x[i].qs[j] & 0x0F);
  971. const int x1 = (x[i].qs[j] >> 4);
  972. y[i*qk + j + 0 ] = x0*d + m;
  973. y[i*qk + j + qk/2] = x1*d + m;
  974. }
  975. }
  976. }
  977. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  978. static const int qk = QK5_0;
  979. assert(k % qk == 0);
  980. const int nb = k / qk;
  981. for (int i = 0; i < nb; i++) {
  982. const float d = GGML_FP16_TO_FP32(x[i].d);
  983. uint32_t qh;
  984. memcpy(&qh, x[i].qh, sizeof(qh));
  985. for (int j = 0; j < qk/2; ++j) {
  986. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  987. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  988. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  989. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  990. y[i*qk + j + 0 ] = x0*d;
  991. y[i*qk + j + qk/2] = x1*d;
  992. }
  993. }
  994. }
  995. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  996. static const int qk = QK5_1;
  997. assert(k % qk == 0);
  998. const int nb = k / qk;
  999. for (int i = 0; i < nb; i++) {
  1000. const float d = GGML_FP16_TO_FP32(x[i].d);
  1001. const float m = GGML_FP16_TO_FP32(x[i].m);
  1002. uint32_t qh;
  1003. memcpy(&qh, x[i].qh, sizeof(qh));
  1004. for (int j = 0; j < qk/2; ++j) {
  1005. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  1006. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  1007. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  1008. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  1009. y[i*qk + j + 0 ] = x0*d + m;
  1010. y[i*qk + j + qk/2] = x1*d + m;
  1011. }
  1012. }
  1013. }
  1014. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  1015. static const int qk = QK8_0;
  1016. assert(k % qk == 0);
  1017. const int nb = k / qk;
  1018. for (int i = 0; i < nb; i++) {
  1019. const float d = GGML_FP16_TO_FP32(x[i].d);
  1020. for (int j = 0; j < qk; ++j) {
  1021. y[i*qk + j] = x[i].qs[j]*d;
  1022. }
  1023. }
  1024. }
  1025. //
  1026. // 2-6 bit quantization in super-blocks
  1027. //
  1028. //
  1029. // ===================== Helper functions
  1030. //
  1031. static inline int nearest_int(float fval) {
  1032. assert(fval <= 4194303.f);
  1033. float val = fval + 12582912.f;
  1034. int i; memcpy(&i, &val, sizeof(int));
  1035. return (i & 0x007fffff) - 0x00400000;
  1036. }
  1037. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  1038. const float * restrict qw) {
  1039. float max = 0;
  1040. float amax = 0;
  1041. for (int i = 0; i < n; ++i) {
  1042. float ax = fabsf(x[i]);
  1043. if (ax > amax) { amax = ax; max = x[i]; }
  1044. }
  1045. if (amax < 1e-30f) { // all zero
  1046. for (int i = 0; i < n; ++i) {
  1047. L[i] = 0;
  1048. }
  1049. return 0.f;
  1050. }
  1051. float iscale = -nmax / max;
  1052. if (rmse_type == 0) {
  1053. for (int i = 0; i < n; ++i) {
  1054. int l = nearest_int(iscale * x[i]);
  1055. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1056. }
  1057. return 1/iscale;
  1058. }
  1059. bool return_early = false;
  1060. if (rmse_type < 0) {
  1061. rmse_type = -rmse_type;
  1062. return_early = true;
  1063. }
  1064. float sumlx = 0;
  1065. float suml2 = 0;
  1066. #ifdef HAVE_BUGGY_APPLE_LINKER
  1067. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1068. for (volatile int i = 0; i < n; ++i) {
  1069. #else
  1070. for (int i = 0; i < n; ++i) {
  1071. #endif
  1072. int l = nearest_int(iscale * x[i]);
  1073. l = MAX(-nmax, MIN(nmax-1, l));
  1074. L[i] = l + nmax;
  1075. 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]));
  1076. sumlx += w*x[i]*l;
  1077. suml2 += w*l*l;
  1078. }
  1079. float scale = sumlx/suml2;
  1080. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1081. float best = scale * sumlx;
  1082. for (int is = -9; is <= 9; ++is) {
  1083. if (is == 0) {
  1084. continue;
  1085. }
  1086. iscale = -(nmax + 0.1f*is) / max;
  1087. sumlx = suml2 = 0;
  1088. for (int i = 0; i < n; ++i) {
  1089. int l = nearest_int(iscale * x[i]);
  1090. l = MAX(-nmax, MIN(nmax-1, l));
  1091. 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]));
  1092. sumlx += w*x[i]*l;
  1093. suml2 += w*l*l;
  1094. }
  1095. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1096. for (int i = 0; i < n; ++i) {
  1097. int l = nearest_int(iscale * x[i]);
  1098. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1099. }
  1100. scale = sumlx/suml2; best = scale*sumlx;
  1101. }
  1102. }
  1103. return scale;
  1104. }
  1105. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1106. float max = 0;
  1107. float amax = 0;
  1108. for (int i = 0; i < n; ++i) {
  1109. float ax = fabsf(x[i]);
  1110. if (ax > amax) { amax = ax; max = x[i]; }
  1111. }
  1112. if (!amax) { // all zero
  1113. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1114. return 0.f;
  1115. }
  1116. float iscale = -nmax / max;
  1117. if (do_rmse) {
  1118. float sumlx = 0;
  1119. float suml2 = 0;
  1120. for (int i = 0; i < n; ++i) {
  1121. int l = nearest_int(iscale * x[i]);
  1122. l = MAX(-nmax, MIN(nmax-1, l));
  1123. L[i] = l;
  1124. float w = x[i]*x[i];
  1125. sumlx += w*x[i]*l;
  1126. suml2 += w*l*l;
  1127. }
  1128. for (int itry = 0; itry < 5; ++itry) {
  1129. int n_changed = 0;
  1130. for (int i = 0; i < n; ++i) {
  1131. float w = x[i]*x[i];
  1132. float slx = sumlx - w*x[i]*L[i];
  1133. if (slx > 0) {
  1134. float sl2 = suml2 - w*L[i]*L[i];
  1135. int new_l = nearest_int(x[i] * sl2 / slx);
  1136. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1137. if (new_l != L[i]) {
  1138. slx += w*x[i]*new_l;
  1139. sl2 += w*new_l*new_l;
  1140. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1141. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1142. ++n_changed;
  1143. }
  1144. }
  1145. }
  1146. }
  1147. if (!n_changed) {
  1148. break;
  1149. }
  1150. }
  1151. for (int i = 0; i < n; ++i) {
  1152. L[i] += nmax;
  1153. }
  1154. return sumlx / suml2;
  1155. }
  1156. for (int i = 0; i < n; ++i) {
  1157. int l = nearest_int(iscale * x[i]);
  1158. l = MAX(-nmax, MIN(nmax-1, l));
  1159. L[i] = l + nmax;
  1160. }
  1161. return 1/iscale;
  1162. }
  1163. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1164. int ntry, float alpha) {
  1165. float min = x[0];
  1166. float max = x[0];
  1167. for (int i = 1; i < n; ++i) {
  1168. if (x[i] < min) min = x[i];
  1169. if (x[i] > max) max = x[i];
  1170. }
  1171. if (max == min) {
  1172. for (int i = 0; i < n; ++i) L[i] = 0;
  1173. *the_min = 0;
  1174. return 0.f;
  1175. }
  1176. if (min > 0) min = 0;
  1177. float iscale = nmax/(max - min);
  1178. float scale = 1/iscale;
  1179. for (int itry = 0; itry < ntry; ++itry) {
  1180. float sumlx = 0; int suml2 = 0;
  1181. bool did_change = false;
  1182. for (int i = 0; i < n; ++i) {
  1183. int l = nearest_int(iscale*(x[i] - min));
  1184. l = MAX(0, MIN(nmax, l));
  1185. if (l != L[i]) {
  1186. L[i] = l;
  1187. did_change = true;
  1188. }
  1189. sumlx += (x[i] - min)*l;
  1190. suml2 += l*l;
  1191. }
  1192. scale = sumlx/suml2;
  1193. float sum = 0;
  1194. for (int i = 0; i < n; ++i) {
  1195. sum += x[i] - scale*L[i];
  1196. }
  1197. min = alpha*min + (1 - alpha)*sum/n;
  1198. if (min > 0) min = 0;
  1199. iscale = 1/scale;
  1200. if (!did_change) break;
  1201. }
  1202. *the_min = -min;
  1203. return scale;
  1204. }
  1205. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1206. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1207. float rmin, float rdelta, int nstep, bool use_mad) {
  1208. float min = x[0];
  1209. float max = x[0];
  1210. float sum_w = weights[0];
  1211. float sum_x = sum_w * x[0];
  1212. #ifdef HAVE_BUGGY_APPLE_LINKER
  1213. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1214. for (volatile int i = 1; i < n; ++i) {
  1215. #else
  1216. for (int i = 1; i < n; ++i) {
  1217. #endif
  1218. if (x[i] < min) min = x[i];
  1219. if (x[i] > max) max = x[i];
  1220. float w = weights[i];
  1221. sum_w += w;
  1222. sum_x += w * x[i];
  1223. }
  1224. if (min > 0) min = 0;
  1225. if (max == min) {
  1226. for (int i = 0; i < n; ++i) L[i] = 0;
  1227. *the_min = -min;
  1228. return 0.f;
  1229. }
  1230. float iscale = nmax/(max - min);
  1231. float scale = 1/iscale;
  1232. float best_mad = 0;
  1233. for (int i = 0; i < n; ++i) {
  1234. int l = nearest_int(iscale*(x[i] - min));
  1235. L[i] = MAX(0, MIN(nmax, l));
  1236. float diff = scale * L[i] + min - x[i];
  1237. diff = use_mad ? fabsf(diff) : diff * diff;
  1238. float w = weights[i];
  1239. best_mad += w * diff;
  1240. }
  1241. if (nstep < 1) {
  1242. *the_min = -min;
  1243. return scale;
  1244. }
  1245. for (int is = 0; is <= nstep; ++is) {
  1246. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1247. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1248. for (int i = 0; i < n; ++i) {
  1249. int l = nearest_int(iscale*(x[i] - min));
  1250. l = MAX(0, MIN(nmax, l));
  1251. Laux[i] = l;
  1252. float w = weights[i];
  1253. sum_l += w*l;
  1254. sum_l2 += w*l*l;
  1255. sum_xl += w*l*x[i];
  1256. }
  1257. float D = sum_w * sum_l2 - sum_l * sum_l;
  1258. if (D > 0) {
  1259. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1260. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1261. if (this_min > 0) {
  1262. this_min = 0;
  1263. this_scale = sum_xl / sum_l2;
  1264. }
  1265. float mad = 0;
  1266. for (int i = 0; i < n; ++i) {
  1267. float diff = this_scale * Laux[i] + this_min - x[i];
  1268. diff = use_mad ? fabsf(diff) : diff * diff;
  1269. float w = weights[i];
  1270. mad += w * diff;
  1271. }
  1272. if (mad < best_mad) {
  1273. for (int i = 0; i < n; ++i) {
  1274. L[i] = Laux[i];
  1275. }
  1276. best_mad = mad;
  1277. scale = this_scale;
  1278. min = this_min;
  1279. }
  1280. }
  1281. }
  1282. *the_min = -min;
  1283. return scale;
  1284. }
  1285. #if QK_K == 256
  1286. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1287. if (j < 4) {
  1288. *d = q[j] & 63; *m = q[j + 4] & 63;
  1289. } else {
  1290. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1291. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1292. }
  1293. }
  1294. #endif
  1295. //========================- 2-bit (de)-quantization
  1296. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1297. assert(k % QK_K == 0);
  1298. const int nb = k / QK_K;
  1299. uint8_t L[QK_K];
  1300. uint8_t Laux[16];
  1301. float weights[16];
  1302. float mins[QK_K/16];
  1303. float scales[QK_K/16];
  1304. const float q4scale = 15.f;
  1305. for (int i = 0; i < nb; i++) {
  1306. float max_scale = 0; // as we are deducting the min, scales are always positive
  1307. float max_min = 0;
  1308. for (int j = 0; j < QK_K/16; ++j) {
  1309. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1310. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1311. float scale = scales[j];
  1312. if (scale > max_scale) {
  1313. max_scale = scale;
  1314. }
  1315. float min = mins[j];
  1316. if (min > max_min) {
  1317. max_min = min;
  1318. }
  1319. }
  1320. if (max_scale > 0) {
  1321. float iscale = q4scale/max_scale;
  1322. for (int j = 0; j < QK_K/16; ++j) {
  1323. int l = nearest_int(iscale*scales[j]);
  1324. y[i].scales[j] = l;
  1325. }
  1326. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1327. } else {
  1328. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1329. y[i].d = GGML_FP32_TO_FP16(0.f);
  1330. }
  1331. if (max_min > 0) {
  1332. float iscale = q4scale/max_min;
  1333. for (int j = 0; j < QK_K/16; ++j) {
  1334. int l = nearest_int(iscale*mins[j]);
  1335. y[i].scales[j] |= (l << 4);
  1336. }
  1337. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1338. } else {
  1339. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1340. }
  1341. for (int j = 0; j < QK_K/16; ++j) {
  1342. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1343. if (!d) continue;
  1344. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1345. for (int ii = 0; ii < 16; ++ii) {
  1346. int l = nearest_int((x[16*j + ii] + dm)/d);
  1347. l = MAX(0, MIN(3, l));
  1348. L[16*j + ii] = l;
  1349. }
  1350. }
  1351. #if QK_K == 256
  1352. for (int j = 0; j < QK_K; j += 128) {
  1353. for (int l = 0; l < 32; ++l) {
  1354. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1355. }
  1356. }
  1357. #else
  1358. for (int l = 0; l < 16; ++l) {
  1359. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1360. }
  1361. #endif
  1362. x += QK_K;
  1363. }
  1364. }
  1365. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1366. assert(k % QK_K == 0);
  1367. const int nb = k / QK_K;
  1368. for (int i = 0; i < nb; i++) {
  1369. const float d = GGML_FP16_TO_FP32(x[i].d);
  1370. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1371. const uint8_t * q = x[i].qs;
  1372. #if QK_K == 256
  1373. int is = 0;
  1374. float dl, ml;
  1375. for (int n = 0; n < QK_K; n += 128) {
  1376. int shift = 0;
  1377. for (int j = 0; j < 4; ++j) {
  1378. uint8_t sc = x[i].scales[is++];
  1379. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1380. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1381. sc = x[i].scales[is++];
  1382. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1383. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1384. shift += 2;
  1385. }
  1386. q += 32;
  1387. }
  1388. #else
  1389. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1390. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1391. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1392. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1393. for (int l = 0; l < 16; ++l) {
  1394. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1395. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1396. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1397. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1398. }
  1399. y += QK_K;
  1400. #endif
  1401. }
  1402. }
  1403. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1404. quantize_row_q2_K_reference(x, vy, k);
  1405. }
  1406. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1407. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1408. float rmin, float rdelta, int nstep, bool use_mad) {
  1409. float min = x[0];
  1410. float max = x[0];
  1411. float sum_w = weights ? weights[0] : x[0]*x[0];
  1412. float sum_x = sum_w * x[0];
  1413. #ifdef HAVE_BUGGY_APPLE_LINKER
  1414. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1415. for (volatile int i = 1; i < n; ++i) {
  1416. #else
  1417. for (int i = 1; i < n; ++i) {
  1418. #endif
  1419. if (x[i] < min) min = x[i];
  1420. if (x[i] > max) max = x[i];
  1421. float w = weights ? weights[i] : x[i]*x[i];
  1422. sum_w += w;
  1423. sum_x += w * x[i];
  1424. }
  1425. if (min > 0) {
  1426. min = 0;
  1427. }
  1428. if (max <= min) {
  1429. memset(L, 0, n);
  1430. *the_min = -min;
  1431. return 0.f;
  1432. }
  1433. float iscale = nmax/(max - min);
  1434. float scale = 1/iscale;
  1435. float best_mad = 0;
  1436. for (int i = 0; i < n; ++i) {
  1437. int l = nearest_int(iscale*(x[i] - min));
  1438. L[i] = MAX(0, MIN(nmax, l));
  1439. float diff = scale * L[i] + min - x[i];
  1440. diff = use_mad ? fabsf(diff) : diff*diff;
  1441. float w = weights ? weights[i] : x[i]*x[i];
  1442. best_mad += w * diff;
  1443. }
  1444. if (nstep < 1) {
  1445. *the_min = -min;
  1446. return scale;
  1447. }
  1448. for (int is = 0; is <= nstep; ++is) {
  1449. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1450. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1451. for (int i = 0; i < n; ++i) {
  1452. int l = nearest_int(iscale*(x[i] - min));
  1453. l = MAX(0, MIN(nmax, l));
  1454. Laux[i] = l;
  1455. float w = weights ? weights[i] : x[i]*x[i];
  1456. sum_l += w*l;
  1457. sum_l2 += w*l*l;
  1458. sum_xl += w*l*x[i];
  1459. }
  1460. float D = sum_w * sum_l2 - sum_l * sum_l;
  1461. if (D > 0) {
  1462. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1463. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1464. if (this_min > 0) {
  1465. this_min = 0;
  1466. this_scale = sum_xl / sum_l2;
  1467. }
  1468. float mad = 0;
  1469. for (int i = 0; i < n; ++i) {
  1470. float diff = this_scale * Laux[i] + this_min - x[i];
  1471. diff = use_mad ? fabsf(diff) : diff*diff;
  1472. float w = weights ? weights[i] : x[i]*x[i];
  1473. mad += w * diff;
  1474. }
  1475. if (mad < best_mad) {
  1476. for (int i = 0; i < n; ++i) {
  1477. L[i] = Laux[i];
  1478. }
  1479. best_mad = mad;
  1480. scale = this_scale;
  1481. min = this_min;
  1482. }
  1483. }
  1484. }
  1485. *the_min = -min;
  1486. return scale;
  1487. }
  1488. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1489. float max = 0;
  1490. for (int i = 0; i < n; ++i) {
  1491. max = MAX(max, x[i]);
  1492. }
  1493. if (!max) { // all zero
  1494. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1495. return 0.f;
  1496. }
  1497. float iscale = nmax / max;
  1498. for (int i = 0; i < n; ++i) {
  1499. L[i] = nearest_int(iscale * x[i]);
  1500. }
  1501. float scale = 1/iscale;
  1502. float best_mse = 0;
  1503. for (int i = 0; i < n; ++i) {
  1504. float diff = x[i] - scale*L[i];
  1505. float w = quant_weights[i];
  1506. best_mse += w*diff*diff;
  1507. }
  1508. for (int is = -4; is <= 4; ++is) {
  1509. if (is == 0) continue;
  1510. float iscale_is = (0.1f*is + nmax)/max;
  1511. float scale_is = 1/iscale_is;
  1512. float mse = 0;
  1513. for (int i = 0; i < n; ++i) {
  1514. int l = nearest_int(iscale_is*x[i]);
  1515. l = MIN(nmax, l);
  1516. float diff = x[i] - scale_is*l;
  1517. float w = quant_weights[i];
  1518. mse += w*diff*diff;
  1519. }
  1520. if (mse < best_mse) {
  1521. best_mse = mse;
  1522. iscale = iscale_is;
  1523. }
  1524. }
  1525. float sumlx = 0;
  1526. float suml2 = 0;
  1527. for (int i = 0; i < n; ++i) {
  1528. int l = nearest_int(iscale * x[i]);
  1529. l = MIN(nmax, l);
  1530. L[i] = l;
  1531. float w = quant_weights[i];
  1532. sumlx += w*x[i]*l;
  1533. suml2 += w*l*l;
  1534. }
  1535. for (int itry = 0; itry < 5; ++itry) {
  1536. int n_changed = 0;
  1537. for (int i = 0; i < n; ++i) {
  1538. float w = quant_weights[i];
  1539. float slx = sumlx - w*x[i]*L[i];
  1540. float sl2 = suml2 - w*L[i]*L[i];
  1541. if (slx > 0 && sl2 > 0) {
  1542. int new_l = nearest_int(x[i] * sl2 / slx);
  1543. new_l = MIN(nmax, new_l);
  1544. if (new_l != L[i]) {
  1545. slx += w*x[i]*new_l;
  1546. sl2 += w*new_l*new_l;
  1547. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1548. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1549. ++n_changed;
  1550. }
  1551. }
  1552. }
  1553. }
  1554. if (!n_changed) {
  1555. break;
  1556. }
  1557. }
  1558. return sumlx / suml2;
  1559. }
  1560. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1561. GGML_ASSERT(quant_weights);
  1562. assert(k % QK_K == 0);
  1563. const int nb = k / QK_K;
  1564. const bool requantize = true;
  1565. uint8_t L[QK_K];
  1566. uint8_t Laux[16];
  1567. float mins[QK_K/16];
  1568. float scales[QK_K/16];
  1569. float sw[QK_K/16];
  1570. float weight[16];
  1571. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1572. for (int i = 0; i < nb; i++) {
  1573. memset(sw, 0, QK_K/16*sizeof(float));
  1574. float sumx2 = 0;
  1575. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1576. float sigma2 = sumx2/QK_K;
  1577. for (int j = 0; j < QK_K/16; ++j) {
  1578. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1579. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1580. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1581. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1582. }
  1583. float dm, mm;
  1584. #if QK_K == 64
  1585. float max_scale = 0, max_min = 0;
  1586. for (int j = 0; j < QK_K/16; ++j) {
  1587. max_scale = MAX(max_scale, scales[j]);
  1588. max_min = MAX(max_min, mins[j]);
  1589. }
  1590. dm = max_scale/15;
  1591. mm = max_min/15;
  1592. if (max_scale) {
  1593. float id = 1/dm;
  1594. for (int j = 0; j < QK_K/16; ++j) {
  1595. int l = nearest_int(id*scales[j]);
  1596. Ls[j] = MAX(0, MIN(15, l));
  1597. }
  1598. } else {
  1599. memset(Ls, 0, QK_K/16);
  1600. }
  1601. if (max_min) {
  1602. float id = 1/mm;
  1603. for (int j = 0; j < QK_K/16; ++j) {
  1604. int l = nearest_int(id*mins[j]);
  1605. Lm[j] = MAX(0, MIN(15, l));
  1606. }
  1607. } else {
  1608. memset(Lm, 0, QK_K/16);
  1609. }
  1610. #else
  1611. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1612. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1613. #endif
  1614. y[i].d = GGML_FP32_TO_FP16(dm);
  1615. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1616. dm = GGML_FP16_TO_FP32(y[i].d);
  1617. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1618. for (int j = 0; j < QK_K/16; ++j) {
  1619. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1620. }
  1621. if (requantize) {
  1622. for (int j = 0; j < QK_K/16; ++j) {
  1623. const float d = dm * (y[i].scales[j] & 0xF);
  1624. if (!d) continue;
  1625. const float m = mm * (y[i].scales[j] >> 4);
  1626. for (int ii = 0; ii < 16; ++ii) {
  1627. int l = nearest_int((x[16*j + ii] + m)/d);
  1628. l = MAX(0, MIN(3, l));
  1629. L[16*j + ii] = l;
  1630. }
  1631. }
  1632. }
  1633. #if QK_K == 256
  1634. for (int j = 0; j < QK_K; j += 128) {
  1635. for (int l = 0; l < 32; ++l) {
  1636. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1637. }
  1638. }
  1639. #else
  1640. for (int l = 0; l < 16; ++l) {
  1641. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1642. }
  1643. #endif
  1644. x += QK_K;
  1645. }
  1646. }
  1647. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  1648. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1649. if (!quant_weights) {
  1650. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1651. }
  1652. else {
  1653. char * qrow = (char *)dst;
  1654. for (int row = 0; row < nrow; ++row) {
  1655. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1656. src += n_per_row;
  1657. qrow += row_size;
  1658. }
  1659. }
  1660. return nrow * row_size;
  1661. }
  1662. //========================= 3-bit (de)-quantization
  1663. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1664. assert(k % QK_K == 0);
  1665. const int nb = k / QK_K;
  1666. int8_t L[QK_K];
  1667. float scales[QK_K / 16];
  1668. for (int i = 0; i < nb; i++) {
  1669. float max_scale = 0;
  1670. float amax = 0;
  1671. for (int j = 0; j < QK_K/16; ++j) {
  1672. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1673. float scale = fabsf(scales[j]);
  1674. if (scale > amax) {
  1675. amax = scale; max_scale = scales[j];
  1676. }
  1677. }
  1678. #if QK_K == 256
  1679. memset(y[i].scales, 0, 12);
  1680. if (max_scale) {
  1681. float iscale = -32.f/max_scale;
  1682. for (int j = 0; j < QK_K/16; ++j) {
  1683. int8_t l = nearest_int(iscale*scales[j]);
  1684. l = MAX(-32, MIN(31, l)) + 32;
  1685. if (j < 8) {
  1686. y[i].scales[j] = l & 0xF;
  1687. } else {
  1688. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1689. }
  1690. l >>= 4;
  1691. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1692. }
  1693. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1694. } else {
  1695. y[i].d = GGML_FP32_TO_FP16(0.f);
  1696. }
  1697. int8_t sc;
  1698. for (int j = 0; j < QK_K/16; ++j) {
  1699. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1700. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1701. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1702. if (!d) {
  1703. continue;
  1704. }
  1705. for (int ii = 0; ii < 16; ++ii) {
  1706. int l = nearest_int(x[16*j + ii]/d);
  1707. l = MAX(-4, MIN(3, l));
  1708. L[16*j + ii] = l + 4;
  1709. }
  1710. }
  1711. #else
  1712. if (max_scale) {
  1713. float iscale = -8.f/max_scale;
  1714. for (int j = 0; j < QK_K/16; j+=2) {
  1715. int l1 = nearest_int(iscale*scales[j]);
  1716. l1 = 8 + MAX(-8, MIN(7, l1));
  1717. int l2 = nearest_int(iscale*scales[j+1]);
  1718. l2 = 8 + MAX(-8, MIN(7, l2));
  1719. y[i].scales[j/2] = l1 | (l2 << 4);
  1720. }
  1721. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1722. } else {
  1723. for (int j = 0; j < QK_K/16; j+=2) {
  1724. y[i].scales[j/2] = 0;
  1725. }
  1726. y[i].d = GGML_FP32_TO_FP16(0.f);
  1727. }
  1728. for (int j = 0; j < QK_K/16; ++j) {
  1729. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1730. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1731. if (!d) {
  1732. continue;
  1733. }
  1734. for (int ii = 0; ii < 16; ++ii) {
  1735. int l = nearest_int(x[16*j + ii]/d);
  1736. l = MAX(-4, MIN(3, l));
  1737. L[16*j + ii] = l + 4;
  1738. }
  1739. }
  1740. #endif
  1741. memset(y[i].hmask, 0, QK_K/8);
  1742. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1743. int m = 0;
  1744. uint8_t hm = 1;
  1745. for (int j = 0; j < QK_K; ++j) {
  1746. if (L[j] > 3) {
  1747. y[i].hmask[m] |= hm;
  1748. L[j] -= 4;
  1749. }
  1750. if (++m == QK_K/8) {
  1751. m = 0; hm <<= 1;
  1752. }
  1753. }
  1754. #if QK_K == 256
  1755. for (int j = 0; j < QK_K; j += 128) {
  1756. for (int l = 0; l < 32; ++l) {
  1757. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1758. }
  1759. }
  1760. #else
  1761. for (int l = 0; l < 16; ++l) {
  1762. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1763. }
  1764. #endif
  1765. x += QK_K;
  1766. }
  1767. }
  1768. #if QK_K == 256
  1769. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1770. assert(k % QK_K == 0);
  1771. const int nb = k / QK_K;
  1772. const uint32_t kmask1 = 0x03030303;
  1773. const uint32_t kmask2 = 0x0f0f0f0f;
  1774. uint32_t aux[4];
  1775. const int8_t * scales = (const int8_t*)aux;
  1776. for (int i = 0; i < nb; i++) {
  1777. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1778. const uint8_t * restrict q = x[i].qs;
  1779. const uint8_t * restrict hm = x[i].hmask;
  1780. uint8_t m = 1;
  1781. memcpy(aux, x[i].scales, 12);
  1782. uint32_t tmp = aux[2];
  1783. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1784. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1785. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1786. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1787. int is = 0;
  1788. float dl;
  1789. for (int n = 0; n < QK_K; n += 128) {
  1790. int shift = 0;
  1791. for (int j = 0; j < 4; ++j) {
  1792. dl = d_all * (scales[is++] - 32);
  1793. for (int l = 0; l < 16; ++l) {
  1794. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1795. }
  1796. dl = d_all * (scales[is++] - 32);
  1797. for (int l = 0; l < 16; ++l) {
  1798. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1799. }
  1800. shift += 2;
  1801. m <<= 1;
  1802. }
  1803. q += 32;
  1804. }
  1805. }
  1806. }
  1807. #else
  1808. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1809. assert(k % QK_K == 0);
  1810. assert(QK_K == 64);
  1811. const int nb = k / QK_K;
  1812. for (int i = 0; i < nb; i++) {
  1813. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1814. const uint8_t * restrict q = x[i].qs;
  1815. const uint8_t * restrict hm = x[i].hmask;
  1816. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1817. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1818. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1819. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1820. for (int l=0; l<8; ++l) {
  1821. uint8_t h = hm[l];
  1822. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1823. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1824. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1825. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1826. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1827. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1828. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1829. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1830. }
  1831. y += QK_K;
  1832. }
  1833. }
  1834. #endif
  1835. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1836. quantize_row_q3_K_reference(x, vy, k);
  1837. }
  1838. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int n_per_row, const float * restrict quant_weights) {
  1839. #if QK_K != 256
  1840. (void)quant_weights;
  1841. quantize_row_q3_K_reference(x, y, n_per_row);
  1842. #else
  1843. assert(n_per_row % QK_K == 0);
  1844. const int nb = n_per_row / QK_K;
  1845. int8_t L[QK_K];
  1846. float scales[QK_K / 16];
  1847. float weight[16];
  1848. float sw[QK_K / 16];
  1849. int8_t Ls[QK_K / 16];
  1850. for (int i = 0; i < nb; i++) {
  1851. float sumx2 = 0;
  1852. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1853. float sigma2 = 2*sumx2/QK_K;
  1854. for (int j = 0; j < QK_K/16; ++j) {
  1855. if (quant_weights) {
  1856. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1857. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1858. } else {
  1859. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1860. }
  1861. float sumw = 0;
  1862. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1863. sw[j] = sumw;
  1864. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1865. }
  1866. memset(y[i].scales, 0, 12);
  1867. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1868. for (int j = 0; j < QK_K/16; ++j) {
  1869. int l = Ls[j];
  1870. if (j < 8) {
  1871. y[i].scales[j] = l & 0xF;
  1872. } else {
  1873. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1874. }
  1875. l >>= 4;
  1876. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1877. }
  1878. y[i].d = GGML_FP32_TO_FP16(d_block);
  1879. int8_t sc;
  1880. for (int j = 0; j < QK_K/16; ++j) {
  1881. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1882. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1883. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1884. if (!d) {
  1885. continue;
  1886. }
  1887. for (int ii = 0; ii < 16; ++ii) {
  1888. int l = nearest_int(x[16*j + ii]/d);
  1889. l = MAX(-4, MIN(3, l));
  1890. L[16*j + ii] = l + 4;
  1891. }
  1892. }
  1893. memset(y[i].hmask, 0, QK_K/8);
  1894. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1895. int m = 0;
  1896. uint8_t hm = 1;
  1897. for (int j = 0; j < QK_K; ++j) {
  1898. if (L[j] > 3) {
  1899. y[i].hmask[m] |= hm;
  1900. L[j] -= 4;
  1901. }
  1902. if (++m == QK_K/8) {
  1903. m = 0; hm <<= 1;
  1904. }
  1905. }
  1906. for (int j = 0; j < QK_K; j += 128) {
  1907. for (int l = 0; l < 32; ++l) {
  1908. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1909. }
  1910. }
  1911. x += QK_K;
  1912. }
  1913. #endif
  1914. }
  1915. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  1916. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1917. if (!quant_weights) {
  1918. quantize_row_q3_K_reference(src, dst, nrow*n_per_row);
  1919. }
  1920. else {
  1921. char * qrow = (char *)dst;
  1922. for (int row = 0; row < nrow; ++row) {
  1923. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1924. src += n_per_row;
  1925. qrow += row_size;
  1926. }
  1927. }
  1928. return nrow * row_size;
  1929. }
  1930. // ====================== 4-bit (de)-quantization
  1931. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1932. assert(k % QK_K == 0);
  1933. const int nb = k / QK_K;
  1934. uint8_t L[QK_K];
  1935. uint8_t Laux[32];
  1936. float weights[32];
  1937. float mins[QK_K/32];
  1938. float scales[QK_K/32];
  1939. for (int i = 0; i < nb; i++) {
  1940. float max_scale = 0; // as we are deducting the min, scales are always positive
  1941. float max_min = 0;
  1942. for (int j = 0; j < QK_K/32; ++j) {
  1943. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1944. float sum_x2 = 0;
  1945. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1946. float av_x = sqrtf(sum_x2/32);
  1947. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1948. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1949. float scale = scales[j];
  1950. if (scale > max_scale) {
  1951. max_scale = scale;
  1952. }
  1953. float min = mins[j];
  1954. if (min > max_min) {
  1955. max_min = min;
  1956. }
  1957. }
  1958. #if QK_K == 256
  1959. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1960. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1961. for (int j = 0; j < QK_K/32; ++j) {
  1962. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1963. uint8_t lm = nearest_int(inv_min*mins[j]);
  1964. ls = MIN(63, ls);
  1965. lm = MIN(63, lm);
  1966. if (j < 4) {
  1967. y[i].scales[j] = ls;
  1968. y[i].scales[j+4] = lm;
  1969. } else {
  1970. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1971. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1972. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1973. }
  1974. }
  1975. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1976. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1977. uint8_t sc, m;
  1978. for (int j = 0; j < QK_K/32; ++j) {
  1979. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1980. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1981. if (!d) continue;
  1982. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1983. for (int ii = 0; ii < 32; ++ii) {
  1984. int l = nearest_int((x[32*j + ii] + dm)/d);
  1985. l = MAX(0, MIN(15, l));
  1986. L[32*j + ii] = l;
  1987. }
  1988. }
  1989. #else
  1990. const float s_factor = 15.f;
  1991. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1992. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1993. int d1 = nearest_int(inv_scale*scales[0]);
  1994. int m1 = nearest_int(inv_min*mins[0]);
  1995. int d2 = nearest_int(inv_scale*scales[1]);
  1996. int m2 = nearest_int(inv_min*mins[1]);
  1997. y[i].scales[0] = d1 | (m1 << 4);
  1998. y[i].scales[1] = d2 | (m2 << 4);
  1999. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  2000. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  2001. float sumlx = 0;
  2002. int suml2 = 0;
  2003. for (int j = 0; j < QK_K/32; ++j) {
  2004. const uint8_t sd = y[i].scales[j] & 0xF;
  2005. const uint8_t sm = y[i].scales[j] >> 4;
  2006. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  2007. if (!d) continue;
  2008. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  2009. for (int ii = 0; ii < 32; ++ii) {
  2010. int l = nearest_int((x[32*j + ii] + m)/d);
  2011. l = MAX(0, MIN(15, l));
  2012. L[32*j + ii] = l;
  2013. sumlx += (x[32*j + ii] + m)*l*sd;
  2014. suml2 += l*l*sd*sd;
  2015. }
  2016. }
  2017. if (suml2) {
  2018. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  2019. }
  2020. #endif
  2021. uint8_t * q = y[i].qs;
  2022. for (int j = 0; j < QK_K; j += 64) {
  2023. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2024. q += 32;
  2025. }
  2026. x += QK_K;
  2027. }
  2028. }
  2029. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  2030. assert(k % QK_K == 0);
  2031. const int nb = k / QK_K;
  2032. for (int i = 0; i < nb; i++) {
  2033. const uint8_t * q = x[i].qs;
  2034. #if QK_K == 256
  2035. const float d = GGML_FP16_TO_FP32(x[i].d);
  2036. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2037. int is = 0;
  2038. uint8_t sc, m;
  2039. for (int j = 0; j < QK_K; j += 64) {
  2040. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2041. const float d1 = d * sc; const float m1 = min * m;
  2042. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2043. const float d2 = d * sc; const float m2 = min * m;
  2044. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  2045. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  2046. q += 32; is += 2;
  2047. }
  2048. #else
  2049. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  2050. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  2051. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  2052. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  2053. for (int l = 0; l < 32; ++l) {
  2054. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  2055. y[l+32] = d2 * (q[l] >> 4) - m2;
  2056. }
  2057. y += QK_K;
  2058. #endif
  2059. }
  2060. }
  2061. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  2062. assert(k % QK_K == 0);
  2063. block_q4_K * restrict y = vy;
  2064. quantize_row_q4_K_reference(x, y, k);
  2065. }
  2066. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int n_per_row, const float * quant_weights) {
  2067. #if QK_K != 256
  2068. (void)quant_weights;
  2069. quantize_row_q4_K_reference(x, y, n_per_row);
  2070. #else
  2071. assert(n_per_row % QK_K == 0);
  2072. const int nb = n_per_row / QK_K;
  2073. uint8_t L[QK_K];
  2074. uint8_t Laux[32];
  2075. uint8_t Ls[QK_K/32];
  2076. uint8_t Lm[QK_K/32];
  2077. float weights[32];
  2078. float sw[QK_K/32];
  2079. float mins[QK_K/32];
  2080. float scales[QK_K/32];
  2081. for (int i = 0; i < nb; i++) {
  2082. float sum_x2 = 0;
  2083. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2084. float sigma2 = 2*sum_x2/QK_K;
  2085. float av_x = sqrtf(sigma2);
  2086. for (int j = 0; j < QK_K/32; ++j) {
  2087. if (quant_weights) {
  2088. const float * qw = quant_weights + QK_K*i + 32*j;
  2089. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2090. } else {
  2091. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2092. }
  2093. float sumw = 0;
  2094. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2095. sw[j] = sumw;
  2096. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2097. }
  2098. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2099. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2100. for (int j = 0; j < QK_K/32; ++j) {
  2101. uint8_t ls = Ls[j];
  2102. uint8_t lm = Lm[j];
  2103. if (j < 4) {
  2104. y[i].scales[j] = ls;
  2105. y[i].scales[j+4] = lm;
  2106. } else {
  2107. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2108. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2109. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2110. }
  2111. }
  2112. y[i].d = GGML_FP32_TO_FP16(d_block);
  2113. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2114. uint8_t sc, m;
  2115. for (int j = 0; j < QK_K/32; ++j) {
  2116. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2117. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2118. if (!d) continue;
  2119. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2120. for (int ii = 0; ii < 32; ++ii) {
  2121. int l = nearest_int((x[32*j + ii] + dm)/d);
  2122. l = MAX(0, MIN(15, l));
  2123. L[32*j + ii] = l;
  2124. }
  2125. }
  2126. uint8_t * q = y[i].qs;
  2127. for (int j = 0; j < QK_K; j += 64) {
  2128. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2129. q += 32;
  2130. }
  2131. x += QK_K;
  2132. }
  2133. #endif
  2134. }
  2135. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2136. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2137. if (!quant_weights) {
  2138. quantize_row_q4_K_reference(src, dst, nrow*n_per_row);
  2139. }
  2140. else {
  2141. char * qrow = (char *)dst;
  2142. for (int row = 0; row < nrow; ++row) {
  2143. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2144. src += n_per_row;
  2145. qrow += row_size;
  2146. }
  2147. }
  2148. return nrow * row_size;
  2149. }
  2150. // ====================== 5-bit (de)-quantization
  2151. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  2152. assert(k % QK_K == 0);
  2153. const int nb = k / QK_K;
  2154. #if QK_K == 256
  2155. uint8_t L[QK_K];
  2156. float mins[QK_K/32];
  2157. float scales[QK_K/32];
  2158. float weights[32];
  2159. uint8_t Laux[32];
  2160. #else
  2161. int8_t L[QK_K];
  2162. float scales[QK_K/16];
  2163. #endif
  2164. for (int i = 0; i < nb; i++) {
  2165. #if QK_K == 256
  2166. float max_scale = 0; // as we are deducting the min, scales are always positive
  2167. float max_min = 0;
  2168. for (int j = 0; j < QK_K/32; ++j) {
  2169. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2170. float sum_x2 = 0;
  2171. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2172. float av_x = sqrtf(sum_x2/32);
  2173. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2174. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2175. float scale = scales[j];
  2176. if (scale > max_scale) {
  2177. max_scale = scale;
  2178. }
  2179. float min = mins[j];
  2180. if (min > max_min) {
  2181. max_min = min;
  2182. }
  2183. }
  2184. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2185. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2186. for (int j = 0; j < QK_K/32; ++j) {
  2187. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2188. uint8_t lm = nearest_int(inv_min*mins[j]);
  2189. ls = MIN(63, ls);
  2190. lm = MIN(63, lm);
  2191. if (j < 4) {
  2192. y[i].scales[j] = ls;
  2193. y[i].scales[j+4] = lm;
  2194. } else {
  2195. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2196. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2197. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2198. }
  2199. }
  2200. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2201. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2202. uint8_t sc, m;
  2203. for (int j = 0; j < QK_K/32; ++j) {
  2204. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2205. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2206. if (!d) continue;
  2207. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2208. for (int ii = 0; ii < 32; ++ii) {
  2209. int l = nearest_int((x[32*j + ii] + dm)/d);
  2210. l = MAX(0, MIN(31, l));
  2211. L[32*j + ii] = l;
  2212. }
  2213. }
  2214. uint8_t * restrict qh = y[i].qh;
  2215. uint8_t * restrict ql = y[i].qs;
  2216. memset(qh, 0, QK_K/8);
  2217. uint8_t m1 = 1, m2 = 2;
  2218. for (int n = 0; n < QK_K; n += 64) {
  2219. for (int j = 0; j < 32; ++j) {
  2220. int l1 = L[n + j];
  2221. if (l1 > 15) {
  2222. l1 -= 16; qh[j] |= m1;
  2223. }
  2224. int l2 = L[n + j + 32];
  2225. if (l2 > 15) {
  2226. l2 -= 16; qh[j] |= m2;
  2227. }
  2228. ql[j] = l1 | (l2 << 4);
  2229. }
  2230. m1 <<= 2; m2 <<= 2;
  2231. ql += 32;
  2232. }
  2233. #else
  2234. float max_scale = 0, amax = 0;
  2235. for (int j = 0; j < QK_K/16; ++j) {
  2236. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2237. float abs_scale = fabsf(scales[j]);
  2238. if (abs_scale > amax) {
  2239. amax = abs_scale;
  2240. max_scale = scales[j];
  2241. }
  2242. }
  2243. float iscale = -128.f/max_scale;
  2244. for (int j = 0; j < QK_K/16; ++j) {
  2245. int l = nearest_int(iscale*scales[j]);
  2246. y[i].scales[j] = MAX(-128, MIN(127, l));
  2247. }
  2248. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2249. for (int j = 0; j < QK_K/16; ++j) {
  2250. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2251. if (!d) continue;
  2252. for (int ii = 0; ii < 16; ++ii) {
  2253. int l = nearest_int(x[16*j + ii]/d);
  2254. l = MAX(-16, MIN(15, l));
  2255. L[16*j + ii] = l + 16;
  2256. }
  2257. }
  2258. uint8_t * restrict qh = y[i].qh;
  2259. uint8_t * restrict ql = y[i].qs;
  2260. memset(qh, 0, QK_K/8);
  2261. for (int j = 0; j < 32; ++j) {
  2262. int jm = j%8;
  2263. int is = j/8;
  2264. int l1 = L[j];
  2265. if (l1 > 15) {
  2266. l1 -= 16; qh[jm] |= (1 << is);
  2267. }
  2268. int l2 = L[j + 32];
  2269. if (l2 > 15) {
  2270. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2271. }
  2272. ql[j] = l1 | (l2 << 4);
  2273. }
  2274. #endif
  2275. x += QK_K;
  2276. }
  2277. }
  2278. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2279. assert(k % QK_K == 0);
  2280. const int nb = k / QK_K;
  2281. for (int i = 0; i < nb; i++) {
  2282. const uint8_t * ql = x[i].qs;
  2283. const uint8_t * qh = x[i].qh;
  2284. #if QK_K == 256
  2285. const float d = GGML_FP16_TO_FP32(x[i].d);
  2286. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2287. int is = 0;
  2288. uint8_t sc, m;
  2289. uint8_t u1 = 1, u2 = 2;
  2290. for (int j = 0; j < QK_K; j += 64) {
  2291. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2292. const float d1 = d * sc; const float m1 = min * m;
  2293. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2294. const float d2 = d * sc; const float m2 = min * m;
  2295. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2296. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2297. ql += 32; is += 2;
  2298. u1 <<= 2; u2 <<= 2;
  2299. }
  2300. #else
  2301. float d = GGML_FP16_TO_FP32(x[i].d);
  2302. const int8_t * restrict s = x[i].scales;
  2303. for (int l = 0; l < 8; ++l) {
  2304. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2305. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2306. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2307. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2308. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2309. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2310. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2311. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2312. }
  2313. y += QK_K;
  2314. #endif
  2315. }
  2316. }
  2317. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2318. assert(k % QK_K == 0);
  2319. block_q5_K * restrict y = vy;
  2320. quantize_row_q5_K_reference(x, y, k);
  2321. }
  2322. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int n_per_row, const float * quant_weights) {
  2323. #if QK_K != 256
  2324. (void)quant_weights;
  2325. quantize_row_q5_K_reference(x, y, n_per_row);
  2326. #else
  2327. assert(n_per_row % QK_K == 0);
  2328. const int nb = n_per_row / QK_K;
  2329. uint8_t L[QK_K];
  2330. uint8_t Laux[32];
  2331. uint8_t Ls[QK_K/32];
  2332. uint8_t Lm[QK_K/32];
  2333. float mins[QK_K/32];
  2334. float scales[QK_K/32];
  2335. float sw[QK_K/32];
  2336. float weights[32];
  2337. for (int i = 0; i < nb; i++) {
  2338. float sum_x2 = 0;
  2339. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2340. float sigma2 = 2*sum_x2/QK_K;
  2341. float av_x = sqrtf(sigma2);
  2342. for (int j = 0; j < QK_K/32; ++j) {
  2343. if (quant_weights) {
  2344. const float * qw = quant_weights + QK_K*i + 32*j;
  2345. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2346. } else {
  2347. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2348. }
  2349. float sumw = 0;
  2350. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2351. sw[j] = sumw;
  2352. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2353. }
  2354. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2355. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2356. for (int j = 0; j < QK_K/32; ++j) {
  2357. uint8_t ls = Ls[j];
  2358. uint8_t lm = Lm[j];
  2359. ls = MIN(63, ls);
  2360. lm = MIN(63, lm);
  2361. if (j < 4) {
  2362. y[i].scales[j] = ls;
  2363. y[i].scales[j+4] = lm;
  2364. } else {
  2365. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2366. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2367. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2368. }
  2369. }
  2370. y[i].d = GGML_FP32_TO_FP16(d_block);
  2371. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2372. uint8_t sc, m;
  2373. for (int j = 0; j < QK_K/32; ++j) {
  2374. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2375. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2376. if (!d) continue;
  2377. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2378. for (int ii = 0; ii < 32; ++ii) {
  2379. int l = nearest_int((x[32*j + ii] + dm)/d);
  2380. l = MAX(0, MIN(31, l));
  2381. L[32*j + ii] = l;
  2382. }
  2383. }
  2384. uint8_t * restrict qh = y[i].qh;
  2385. uint8_t * restrict ql = y[i].qs;
  2386. memset(qh, 0, QK_K/8);
  2387. uint8_t m1 = 1, m2 = 2;
  2388. for (int n = 0; n < QK_K; n += 64) {
  2389. for (int j = 0; j < 32; ++j) {
  2390. int l1 = L[n + j];
  2391. if (l1 > 15) {
  2392. l1 -= 16; qh[j] |= m1;
  2393. }
  2394. int l2 = L[n + j + 32];
  2395. if (l2 > 15) {
  2396. l2 -= 16; qh[j] |= m2;
  2397. }
  2398. ql[j] = l1 | (l2 << 4);
  2399. }
  2400. m1 <<= 2; m2 <<= 2;
  2401. ql += 32;
  2402. }
  2403. x += QK_K;
  2404. }
  2405. #endif
  2406. }
  2407. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2408. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2409. if (!quant_weights) {
  2410. quantize_row_q5_K_reference(src, dst, nrow*n_per_row);
  2411. }
  2412. else {
  2413. char * qrow = (char *)dst;
  2414. for (int row = 0; row < nrow; ++row) {
  2415. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2416. src += n_per_row;
  2417. qrow += row_size;
  2418. }
  2419. }
  2420. return nrow * row_size;
  2421. }
  2422. // ====================== 6-bit (de)-quantization
  2423. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2424. assert(k % QK_K == 0);
  2425. const int nb = k / QK_K;
  2426. int8_t L[QK_K];
  2427. float scales[QK_K/16];
  2428. for (int i = 0; i < nb; i++) {
  2429. float max_scale = 0;
  2430. float max_abs_scale = 0;
  2431. for (int ib = 0; ib < QK_K/16; ++ib) {
  2432. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2433. scales[ib] = scale;
  2434. const float abs_scale = fabsf(scale);
  2435. if (abs_scale > max_abs_scale) {
  2436. max_abs_scale = abs_scale;
  2437. max_scale = scale;
  2438. }
  2439. }
  2440. if (!max_abs_scale) {
  2441. memset(&y[i], 0, sizeof(block_q6_K));
  2442. y[i].d = GGML_FP32_TO_FP16(0.f);
  2443. x += QK_K;
  2444. continue;
  2445. }
  2446. float iscale = -128.f/max_scale;
  2447. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2448. for (int ib = 0; ib < QK_K/16; ++ib) {
  2449. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2450. }
  2451. for (int j = 0; j < QK_K/16; ++j) {
  2452. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2453. if (!d) {
  2454. continue;
  2455. }
  2456. for (int ii = 0; ii < 16; ++ii) {
  2457. int l = nearest_int(x[16*j + ii]/d);
  2458. l = MAX(-32, MIN(31, l));
  2459. L[16*j + ii] = l + 32;
  2460. }
  2461. }
  2462. uint8_t * restrict ql = y[i].ql;
  2463. uint8_t * restrict qh = y[i].qh;
  2464. #if QK_K == 256
  2465. for (int j = 0; j < QK_K; j += 128) {
  2466. for (int l = 0; l < 32; ++l) {
  2467. const uint8_t q1 = L[j + l + 0] & 0xF;
  2468. const uint8_t q2 = L[j + l + 32] & 0xF;
  2469. const uint8_t q3 = L[j + l + 64] & 0xF;
  2470. const uint8_t q4 = L[j + l + 96] & 0xF;
  2471. ql[l+ 0] = q1 | (q3 << 4);
  2472. ql[l+32] = q2 | (q4 << 4);
  2473. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2474. }
  2475. ql += 64;
  2476. qh += 32;
  2477. }
  2478. #else
  2479. for (int l = 0; l < 32; ++l) {
  2480. const uint8_t q1 = L[l + 0] & 0xF;
  2481. const uint8_t q2 = L[l + 32] & 0xF;
  2482. ql[l] = q1 | (q2 << 4);
  2483. }
  2484. for (int l = 0; l < 16; ++l) {
  2485. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2486. }
  2487. #endif
  2488. x += QK_K;
  2489. }
  2490. }
  2491. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2492. assert(k % QK_K == 0);
  2493. const int nb = k / QK_K;
  2494. for (int i = 0; i < nb; i++) {
  2495. const float d = GGML_FP16_TO_FP32(x[i].d);
  2496. const uint8_t * restrict ql = x[i].ql;
  2497. const uint8_t * restrict qh = x[i].qh;
  2498. const int8_t * restrict sc = x[i].scales;
  2499. #if QK_K == 256
  2500. for (int n = 0; n < QK_K; n += 128) {
  2501. for (int l = 0; l < 32; ++l) {
  2502. int is = l/16;
  2503. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2504. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2505. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2506. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2507. y[l + 0] = d * sc[is + 0] * q1;
  2508. y[l + 32] = d * sc[is + 2] * q2;
  2509. y[l + 64] = d * sc[is + 4] * q3;
  2510. y[l + 96] = d * sc[is + 6] * q4;
  2511. }
  2512. y += 128;
  2513. ql += 64;
  2514. qh += 32;
  2515. sc += 8;
  2516. }
  2517. #else
  2518. for (int l = 0; l < 16; ++l) {
  2519. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2520. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2521. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2522. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2523. y[l+ 0] = d * sc[0] * q1;
  2524. y[l+16] = d * sc[1] * q2;
  2525. y[l+32] = d * sc[2] * q3;
  2526. y[l+48] = d * sc[3] * q4;
  2527. }
  2528. y += 64;
  2529. #endif
  2530. }
  2531. }
  2532. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2533. assert(k % QK_K == 0);
  2534. block_q6_K * restrict y = vy;
  2535. quantize_row_q6_K_reference(x, y, k);
  2536. }
  2537. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int n_per_row, const float * quant_weights) {
  2538. #if QK_K != 256
  2539. (void)quant_weights;
  2540. quantize_row_q6_K_reference(x, y, n_per_row);
  2541. #else
  2542. assert(n_per_row % QK_K == 0);
  2543. const int nb = n_per_row / QK_K;
  2544. int8_t L[QK_K];
  2545. float scales[QK_K/16];
  2546. //float weights[16];
  2547. for (int i = 0; i < nb; i++) {
  2548. //float sum_x2 = 0;
  2549. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2550. //float sigma2 = sum_x2/QK_K;
  2551. float max_scale = 0;
  2552. float max_abs_scale = 0;
  2553. for (int ib = 0; ib < QK_K/16; ++ib) {
  2554. float scale;
  2555. if (quant_weights) {
  2556. const float * qw = quant_weights + QK_K*i + 16*ib;
  2557. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2558. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2559. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2560. } else {
  2561. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2562. }
  2563. scales[ib] = scale;
  2564. const float abs_scale = fabsf(scale);
  2565. if (abs_scale > max_abs_scale) {
  2566. max_abs_scale = abs_scale;
  2567. max_scale = scale;
  2568. }
  2569. }
  2570. if (!max_abs_scale) {
  2571. memset(&y[i], 0, sizeof(block_q6_K));
  2572. y[i].d = GGML_FP32_TO_FP16(0.f);
  2573. x += QK_K;
  2574. continue;
  2575. }
  2576. float iscale = -128.f/max_scale;
  2577. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2578. for (int ib = 0; ib < QK_K/16; ++ib) {
  2579. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2580. }
  2581. for (int j = 0; j < QK_K/16; ++j) {
  2582. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2583. if (!d) {
  2584. continue;
  2585. }
  2586. for (int ii = 0; ii < 16; ++ii) {
  2587. int l = nearest_int(x[16*j + ii]/d);
  2588. l = MAX(-32, MIN(31, l));
  2589. L[16*j + ii] = l + 32;
  2590. }
  2591. }
  2592. uint8_t * restrict ql = y[i].ql;
  2593. uint8_t * restrict qh = y[i].qh;
  2594. for (int j = 0; j < QK_K; j += 128) {
  2595. for (int l = 0; l < 32; ++l) {
  2596. const uint8_t q1 = L[j + l + 0] & 0xF;
  2597. const uint8_t q2 = L[j + l + 32] & 0xF;
  2598. const uint8_t q3 = L[j + l + 64] & 0xF;
  2599. const uint8_t q4 = L[j + l + 96] & 0xF;
  2600. ql[l+ 0] = q1 | (q3 << 4);
  2601. ql[l+32] = q2 | (q4 << 4);
  2602. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2603. }
  2604. ql += 64;
  2605. qh += 32;
  2606. }
  2607. x += QK_K;
  2608. }
  2609. #endif
  2610. }
  2611. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2612. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2613. if (!quant_weights) {
  2614. quantize_row_q6_K_reference(src, dst, nrow*n_per_row);
  2615. }
  2616. else {
  2617. char * qrow = (char *)dst;
  2618. for (int row = 0; row < nrow; ++row) {
  2619. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2620. src += n_per_row;
  2621. qrow += row_size;
  2622. }
  2623. }
  2624. return nrow * row_size;
  2625. }
  2626. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int n_per_row, const float * quant_weights) {
  2627. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2628. if (!quant_weights) {
  2629. quantize_row_q4_0_reference(x, y, n_per_row);
  2630. return;
  2631. }
  2632. float weight[QK4_0];
  2633. int8_t L[QK4_0];
  2634. float sum_x2 = 0;
  2635. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2636. float sigma2 = sum_x2/n_per_row;
  2637. const int nb = n_per_row/QK4_0;
  2638. for (int ib = 0; ib < nb; ++ib) {
  2639. const float * xb = x + QK4_0 * ib;
  2640. const float * qw = quant_weights + QK4_0 * ib;
  2641. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2642. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2643. y[ib].d = GGML_FP32_TO_FP16(d);
  2644. for (int j = 0; j < 16; ++j) {
  2645. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2646. }
  2647. }
  2648. }
  2649. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2650. if (!quant_weights) {
  2651. quantize_row_q4_0_reference(src, dst, nrow*n_per_row);
  2652. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2653. }
  2654. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2655. char * qrow = (char *)dst;
  2656. for (int row = 0; row < nrow; ++row) {
  2657. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2658. src += n_per_row;
  2659. qrow += row_size;
  2660. }
  2661. return nrow * row_size;
  2662. }
  2663. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int n_per_row, const float * quant_weights) {
  2664. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2665. if (!quant_weights) {
  2666. quantize_row_q4_1_reference(x, y, n_per_row);
  2667. return;
  2668. }
  2669. float weight[QK4_1];
  2670. uint8_t L[QK4_1], Laux[QK4_1];
  2671. float sum_x2 = 0;
  2672. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2673. float sigma2 = sum_x2/n_per_row;
  2674. const int nb = n_per_row/QK4_1;
  2675. for (int ib = 0; ib < nb; ++ib) {
  2676. const float * xb = x + QK4_1 * ib;
  2677. const float * qw = quant_weights + QK4_1 * ib;
  2678. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2679. float min;
  2680. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2681. y[ib].d = GGML_FP32_TO_FP16(d);
  2682. y[ib].m = GGML_FP32_TO_FP16(-min);
  2683. for (int j = 0; j < 16; ++j) {
  2684. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2685. }
  2686. }
  2687. }
  2688. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2689. if (!quant_weights) {
  2690. quantize_row_q4_1_reference(src, dst, nrow*n_per_row);
  2691. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2692. }
  2693. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2694. char * qrow = (char *)dst;
  2695. for (int row = 0; row < nrow; ++row) {
  2696. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2697. src += n_per_row;
  2698. qrow += row_size;
  2699. }
  2700. return nrow * row_size;
  2701. }
  2702. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int n_per_row, const float * quant_weights) {
  2703. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2704. if (!quant_weights) {
  2705. quantize_row_q5_0_reference(x, y, n_per_row);
  2706. return;
  2707. }
  2708. float weight[QK5_0];
  2709. int8_t L[QK5_0];
  2710. float sum_x2 = 0;
  2711. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2712. float sigma2 = sum_x2/n_per_row;
  2713. const int nb = n_per_row/QK5_0;
  2714. for (int ib = 0; ib < nb; ++ib) {
  2715. const float * xb = x + QK5_0 * ib;
  2716. const float * qw = quant_weights + QK5_0 * ib;
  2717. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2718. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2719. y[ib].d = GGML_FP32_TO_FP16(d);
  2720. uint32_t qh = 0;
  2721. for (int j = 0; j < 16; ++j) {
  2722. const uint8_t xi0 = L[j];
  2723. const uint8_t xi1 = L[j+16];
  2724. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2725. // get the 5-th bit and store it in qh at the right position
  2726. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2727. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2728. }
  2729. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2730. }
  2731. }
  2732. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2733. if (!quant_weights) {
  2734. quantize_row_q5_0_reference(src, dst, nrow*n_per_row);
  2735. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2736. }
  2737. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2738. char * qrow = (char *)dst;
  2739. for (int row = 0; row < nrow; ++row) {
  2740. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2741. src += n_per_row;
  2742. qrow += row_size;
  2743. }
  2744. return nrow * row_size;
  2745. }
  2746. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int n_per_row, const float * quant_weights) {
  2747. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2748. if (!quant_weights) {
  2749. quantize_row_q5_1_reference(x, y, n_per_row);
  2750. return;
  2751. }
  2752. float weight[QK5_1];
  2753. uint8_t L[QK5_1], Laux[QK5_1];
  2754. float sum_x2 = 0;
  2755. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2756. float sigma2 = sum_x2/n_per_row;
  2757. const int nb = n_per_row/QK5_1;
  2758. for (int ib = 0; ib < nb; ++ib) {
  2759. const float * xb = x + QK5_1 * ib;
  2760. const float * qw = quant_weights + QK5_1 * ib;
  2761. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2762. float min;
  2763. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2764. y[ib].d = GGML_FP32_TO_FP16(d);
  2765. y[ib].m = GGML_FP32_TO_FP16(-min);
  2766. uint32_t qh = 0;
  2767. for (int j = 0; j < 16; ++j) {
  2768. const uint8_t xi0 = L[j];
  2769. const uint8_t xi1 = L[j+16];
  2770. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2771. // get the 5-th bit and store it in qh at the right position
  2772. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2773. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2774. }
  2775. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2776. }
  2777. }
  2778. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2779. if (!quant_weights) {
  2780. quantize_row_q5_1_reference(src, dst, nrow*n_per_row);
  2781. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2782. }
  2783. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2784. char * qrow = (char *)dst;
  2785. for (int row = 0; row < nrow; ++row) {
  2786. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2787. src += n_per_row;
  2788. qrow += row_size;
  2789. }
  2790. return nrow * row_size;
  2791. }
  2792. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2793. (void)quant_weights; // not used
  2794. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2795. quantize_row_q8_0_reference(src, dst, nrow*n_per_row);
  2796. return nrow * row_size;
  2797. }
  2798. // ====================== "True" 2-bit (de)-quantization
  2799. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2800. assert(k % QK_K == 0);
  2801. const int nb = k / QK_K;
  2802. uint32_t aux32[2];
  2803. const uint8_t * aux8 = (const uint8_t *)aux32;
  2804. for (int i = 0; i < nb; i++) {
  2805. const float d = GGML_FP16_TO_FP32(x[i].d);
  2806. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2807. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2808. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2809. for (int l = 0; l < 4; ++l) {
  2810. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2811. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2812. for (int j = 0; j < 8; ++j) {
  2813. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2814. }
  2815. y += 8;
  2816. }
  2817. }
  2818. }
  2819. }
  2820. // ====================== 2.3125 bpw (de)-quantization
  2821. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2822. assert(k % QK_K == 0);
  2823. const int nb = k / QK_K;
  2824. float db[2];
  2825. for (int i = 0; i < nb; i++) {
  2826. const float d = GGML_FP16_TO_FP32(x[i].d);
  2827. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2828. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2829. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2830. for (int l = 0; l < 4; ++l) {
  2831. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2832. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2833. for (int j = 0; j < 8; ++j) {
  2834. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2835. }
  2836. y += 8;
  2837. }
  2838. }
  2839. }
  2840. }
  2841. // ====================== 2.5625 bpw (de)-quantization
  2842. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int k) {
  2843. assert(k % QK_K == 0);
  2844. const int nb = k / QK_K;
  2845. float db[2];
  2846. for (int i = 0; i < nb; i++) {
  2847. const float d = GGML_FP16_TO_FP32(x[i].d);
  2848. const uint8_t * qs = x[i].qs;
  2849. const uint8_t * qh = x[i].qh;
  2850. const uint8_t * signs = qs + QK_K/8;
  2851. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2852. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2853. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2854. for (int l = 0; l < 4; ++l) {
  2855. const float dl = db[l/2];
  2856. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2857. for (int j = 0; j < 8; ++j) {
  2858. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2859. }
  2860. y += 8;
  2861. }
  2862. qs += 4;
  2863. signs += 4;
  2864. }
  2865. }
  2866. }
  2867. // ====================== 3.0625 bpw (de)-quantization
  2868. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int k) {
  2869. assert(k % QK_K == 0);
  2870. const int nb = k / QK_K;
  2871. uint32_t aux32;
  2872. for (int i = 0; i < nb; i++) {
  2873. const float d = GGML_FP16_TO_FP32(x[i].d);
  2874. const uint8_t * qs = x[i].qs;
  2875. const uint8_t * scales_and_signs = qs + QK_K/4;
  2876. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2877. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2878. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2879. for (int l = 0; l < 4; ++l) {
  2880. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2881. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2882. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2883. for (int j = 0; j < 4; ++j) {
  2884. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2885. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2886. }
  2887. y += 8;
  2888. }
  2889. qs += 8;
  2890. }
  2891. }
  2892. }
  2893. // ====================== 3.3125 bpw (de)-quantization
  2894. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int k) {
  2895. assert(k % QK_K == 0);
  2896. const int nb = k / QK_K;
  2897. for (int i = 0; i < nb; i++) {
  2898. const float d = GGML_FP16_TO_FP32(x[i].d);
  2899. const uint8_t * qs = x[i].qs;
  2900. const uint8_t * qh = x[i].qh;
  2901. const uint8_t * signs = x[i].signs;
  2902. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2903. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2904. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2905. for (int l = 0; l < 4; ++l) {
  2906. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2907. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2908. for (int j = 0; j < 4; ++j) {
  2909. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2910. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2911. }
  2912. y += 8;
  2913. }
  2914. qs += 8;
  2915. signs += 4;
  2916. for (int l = 0; l < 4; ++l) {
  2917. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2918. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2919. for (int j = 0; j < 4; ++j) {
  2920. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2921. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2922. }
  2923. y += 8;
  2924. }
  2925. qh += 2;
  2926. qs += 8;
  2927. signs += 4;
  2928. }
  2929. }
  2930. }
  2931. // ====================== 1.5625 bpw (de)-quantization
  2932. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int k) {
  2933. assert(k % QK_K == 0);
  2934. const int nb = k / QK_K;
  2935. for (int i = 0; i < nb; i++) {
  2936. const float d = GGML_FP16_TO_FP32(x[i].d);
  2937. const uint8_t * qs = x[i].qs;
  2938. const uint16_t * qh = x[i].qh;
  2939. for (int ib = 0; ib < QK_K/32; ++ib) {
  2940. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2941. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2942. for (int l = 0; l < 4; ++l) {
  2943. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2944. for (int j = 0; j < 8; ++j) {
  2945. y[j] = dl * (grid[j] + delta);
  2946. }
  2947. y += 8;
  2948. }
  2949. qs += 4;
  2950. }
  2951. }
  2952. }
  2953. void dequantize_row_iq1_m(const block_iq1_m * restrict x, float * restrict y, int k) {
  2954. assert(k % QK_K == 0);
  2955. const int nb = k / QK_K;
  2956. float delta[4];
  2957. uint16_t idx[4];
  2958. iq1m_scale_t scale;
  2959. for (int i = 0; i < nb; i++) {
  2960. const uint16_t * sc = (const uint16_t *)x[i].scales;
  2961. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  2962. const float d = GGML_FP16_TO_FP32(scale.f16);
  2963. const uint8_t * qs = x[i].qs;
  2964. const uint8_t * qh = x[i].qh;
  2965. for (int ib = 0; ib < QK_K/32; ++ib) {
  2966. const float dl1 = d * (2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1);
  2967. const float dl2 = d * (2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1);
  2968. idx[0] = qs[0] | ((qh[0] << 8) & 0x700);
  2969. idx[1] = qs[1] | ((qh[0] << 4) & 0x700);
  2970. idx[2] = qs[2] | ((qh[1] << 8) & 0x700);
  2971. idx[3] = qs[3] | ((qh[1] << 4) & 0x700);
  2972. delta[0] = qh[0] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2973. delta[1] = qh[0] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2974. delta[2] = qh[1] & 0x08 ? -IQ1S_DELTA : IQ1S_DELTA;
  2975. delta[3] = qh[1] & 0x80 ? -IQ1S_DELTA : IQ1S_DELTA;
  2976. for (int l = 0; l < 2; ++l) {
  2977. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2978. for (int j = 0; j < 8; ++j) {
  2979. y[j] = dl1 * (grid[j] + delta[l]);
  2980. }
  2981. y += 8;
  2982. }
  2983. for (int l = 2; l < 4; ++l) {
  2984. const int8_t * grid = (const int8_t *)(iq1s_grid + idx[l]);
  2985. for (int j = 0; j < 8; ++j) {
  2986. y[j] = dl2 * (grid[j] + delta[l]);
  2987. }
  2988. y += 8;
  2989. }
  2990. qs += 4;
  2991. qh += 2;
  2992. }
  2993. }
  2994. }
  2995. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2996. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int k) {
  2997. assert(k % QK4_NL == 0);
  2998. const int nb = k / QK4_NL;
  2999. for (int i = 0; i < nb; i++) {
  3000. const uint8_t * qs = x[i].qs;
  3001. const float d = GGML_FP16_TO_FP32(x[i].d);
  3002. for (int j = 0; j < QK4_NL/2; ++j) {
  3003. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  3004. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  3005. }
  3006. y += QK4_NL;
  3007. qs += QK4_NL/2;
  3008. }
  3009. }
  3010. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int k) {
  3011. assert(k % QK_K == 0);
  3012. #if QK_K == 64
  3013. dequantize_row_iq4_nl((const block_iq4_nl *)x, y, k);
  3014. #else
  3015. const int nb = k / QK_K;
  3016. for (int i = 0; i < nb; i++) {
  3017. const uint8_t * qs = x[i].qs;
  3018. const float d = GGML_FP16_TO_FP32(x[i].d);
  3019. for (int ib = 0; ib < QK_K/32; ++ib) {
  3020. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  3021. const float dl = d * (ls - 32);
  3022. for (int j = 0; j < 16; ++j) {
  3023. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  3024. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  3025. }
  3026. y += 32;
  3027. qs += 16;
  3028. }
  3029. }
  3030. #endif
  3031. }
  3032. //===================================== Q8_K ==============================================
  3033. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  3034. assert(k % QK_K == 0);
  3035. const int nb = k / QK_K;
  3036. for (int i = 0; i < nb; i++) {
  3037. float max = 0;
  3038. float amax = 0;
  3039. for (int j = 0; j < QK_K; ++j) {
  3040. float ax = fabsf(x[j]);
  3041. if (ax > amax) {
  3042. amax = ax; max = x[j];
  3043. }
  3044. }
  3045. if (!amax) {
  3046. y[i].d = 0;
  3047. memset(y[i].qs, 0, QK_K);
  3048. x += QK_K;
  3049. continue;
  3050. }
  3051. //const float iscale = -128.f/max;
  3052. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3053. const float iscale = -127.f/max;
  3054. for (int j = 0; j < QK_K; ++j) {
  3055. int v = nearest_int(iscale*x[j]);
  3056. y[i].qs[j] = MIN(127, v);
  3057. }
  3058. for (int j = 0; j < QK_K/16; ++j) {
  3059. int sum = 0;
  3060. for (int ii = 0; ii < 16; ++ii) {
  3061. sum += y[i].qs[j*16 + ii];
  3062. }
  3063. y[i].bsums[j] = sum;
  3064. }
  3065. y[i].d = 1/iscale;
  3066. x += QK_K;
  3067. }
  3068. }
  3069. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  3070. assert(k % QK_K == 0);
  3071. const int nb = k / QK_K;
  3072. for (int i = 0; i < nb; i++) {
  3073. for (int j = 0; j < QK_K; ++j) {
  3074. *y++ = x[i].d * x[i].qs[j];
  3075. }
  3076. }
  3077. }
  3078. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  3079. quantize_row_q8_K_reference(x, y, k);
  3080. }
  3081. //===================================== Dot ptoducts =================================
  3082. //
  3083. // Helper functions
  3084. //
  3085. #if __AVX__ || __AVX2__ || __AVX512F__
  3086. // shuffles to pick the required scales in dot products
  3087. static inline __m256i get_scale_shuffle_q3k(int i) {
  3088. static const uint8_t k_shuffle[128] = {
  3089. 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,
  3090. 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,
  3091. 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,
  3092. 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,
  3093. };
  3094. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3095. }
  3096. static inline __m256i get_scale_shuffle_k4(int i) {
  3097. static const uint8_t k_shuffle[256] = {
  3098. 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,
  3099. 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,
  3100. 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,
  3101. 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,
  3102. 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,
  3103. 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,
  3104. 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,
  3105. 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
  3106. };
  3107. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3108. }
  3109. static inline __m128i get_scale_shuffle(int i) {
  3110. static const uint8_t k_shuffle[128] = {
  3111. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3112. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3113. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3114. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3115. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3116. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3117. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3118. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3119. };
  3120. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3121. }
  3122. #endif
  3123. 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) {
  3124. const int qk = QK8_0;
  3125. const int nb = n / qk;
  3126. assert(n % qk == 0);
  3127. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3128. assert((nrc == 2) || (nrc == 1));
  3129. #else
  3130. assert(nrc == 1);
  3131. #endif
  3132. UNUSED(nrc);
  3133. UNUSED(bx);
  3134. UNUSED(by);
  3135. UNUSED(bs);
  3136. const block_q4_0 * restrict x = vx;
  3137. const block_q8_0 * restrict y = vy;
  3138. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3139. if (nrc == 2) {
  3140. const block_q4_0 * restrict vx0 = vx;
  3141. const block_q4_0 * restrict vx1 = vx + bx;
  3142. const block_q8_0 * restrict vy0 = vy;
  3143. const block_q8_0 * restrict vy1 = vy + by;
  3144. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3145. for (int i = 0; i < nb; i++) {
  3146. const block_q4_0 * restrict b_x0 = &vx0[i];
  3147. const block_q4_0 * restrict b_x1 = &vx1[i];
  3148. const block_q8_0 * restrict b_y0 = &vy0[i];
  3149. const block_q8_0 * restrict b_y1 = &vy1[i];
  3150. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3151. const int8x16_t s8b = vdupq_n_s8(0x8);
  3152. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3153. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3154. // 4-bit -> 8-bit
  3155. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3156. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3157. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3158. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3159. // sub 8
  3160. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  3161. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  3162. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  3163. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  3164. // load y
  3165. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3166. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3167. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3168. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3169. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3170. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3171. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3172. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3173. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3174. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3175. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3176. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3177. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3178. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3179. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3180. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3181. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3182. l1, r1)), l2, r2)), l3, r3))), scale);
  3183. }
  3184. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3185. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3186. vst1_f32(s, vget_low_f32(sumv2));
  3187. vst1_f32(s + bs, vget_high_f32(sumv2));
  3188. return;
  3189. }
  3190. #endif
  3191. #if defined(__ARM_NEON)
  3192. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3193. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3194. assert(nb % 2 == 0); // TODO: handle odd nb
  3195. for (int i = 0; i < nb; i += 2) {
  3196. const block_q4_0 * restrict x0 = &x[i + 0];
  3197. const block_q4_0 * restrict x1 = &x[i + 1];
  3198. const block_q8_0 * restrict y0 = &y[i + 0];
  3199. const block_q8_0 * restrict y1 = &y[i + 1];
  3200. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3201. const int8x16_t s8b = vdupq_n_s8(0x8);
  3202. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3203. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3204. // 4-bit -> 8-bit
  3205. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3206. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3207. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3208. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3209. // sub 8
  3210. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3211. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3212. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3213. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3214. // load y
  3215. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3216. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3217. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3218. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3219. // dot product into int32x4_t
  3220. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3221. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3222. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3223. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3224. }
  3225. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3226. #elif defined(__AVX2__)
  3227. // Initialize accumulator with zeros
  3228. __m256 acc = _mm256_setzero_ps();
  3229. // Main loop
  3230. for (int i = 0; i < nb; ++i) {
  3231. /* Compute combined scale for the block */
  3232. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3233. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3234. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3235. const __m256i off = _mm256_set1_epi8( 8 );
  3236. qx = _mm256_sub_epi8( qx, off );
  3237. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3238. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3239. /* Multiply q with scale and accumulate */
  3240. acc = _mm256_fmadd_ps( d, q, acc );
  3241. }
  3242. *s = hsum_float_8(acc);
  3243. #elif defined(__AVX__)
  3244. // Initialize accumulator with zeros
  3245. __m256 acc = _mm256_setzero_ps();
  3246. // Main loop
  3247. for (int i = 0; i < nb; ++i) {
  3248. // Compute combined scale for the block
  3249. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3250. const __m128i lowMask = _mm_set1_epi8(0xF);
  3251. const __m128i off = _mm_set1_epi8(8);
  3252. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3253. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3254. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3255. bx_0 = _mm_sub_epi8(bx_0, off);
  3256. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3257. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3258. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3259. bx_0 = _mm_sub_epi8(bx_0, off);
  3260. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3261. // Convert int32_t to float
  3262. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3263. // Apply the scale, and accumulate
  3264. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3265. }
  3266. *s = hsum_float_8(acc);
  3267. #elif defined(__SSSE3__)
  3268. // set constants
  3269. const __m128i lowMask = _mm_set1_epi8(0xF);
  3270. const __m128i off = _mm_set1_epi8(8);
  3271. // Initialize accumulator with zeros
  3272. __m128 acc_0 = _mm_setzero_ps();
  3273. __m128 acc_1 = _mm_setzero_ps();
  3274. __m128 acc_2 = _mm_setzero_ps();
  3275. __m128 acc_3 = _mm_setzero_ps();
  3276. // First round without accumulation
  3277. {
  3278. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3279. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3280. // Compute combined scale for the block 0 and 1
  3281. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3282. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3283. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3284. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3285. bx_0 = _mm_sub_epi8(bx_0, off);
  3286. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3287. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3288. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3289. bx_1 = _mm_sub_epi8(bx_1, off);
  3290. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3291. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3292. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3293. // Compute combined scale for the block 2 and 3
  3294. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3295. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3296. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3297. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3298. bx_2 = _mm_sub_epi8(bx_2, off);
  3299. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3300. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3301. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3302. bx_3 = _mm_sub_epi8(bx_3, off);
  3303. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3304. // Convert int32_t to float
  3305. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3306. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3307. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3308. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3309. // Apply the scale
  3310. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3311. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3312. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3313. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3314. }
  3315. assert(nb % 2 == 0); // TODO: handle odd nb
  3316. // Main loop
  3317. for (int i = 2; i < nb; i+=2) {
  3318. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3319. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3320. // Compute combined scale for the block 0 and 1
  3321. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3322. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3323. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3324. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3325. bx_0 = _mm_sub_epi8(bx_0, off);
  3326. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3327. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3328. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3329. bx_1 = _mm_sub_epi8(bx_1, off);
  3330. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3331. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3332. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3333. // Compute combined scale for the block 2 and 3
  3334. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3335. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3336. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3337. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3338. bx_2 = _mm_sub_epi8(bx_2, off);
  3339. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3340. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3341. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3342. bx_3 = _mm_sub_epi8(bx_3, off);
  3343. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3344. // Convert int32_t to float
  3345. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3346. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3347. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3348. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3349. // Apply the scale
  3350. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3351. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3352. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3353. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3354. // Acummulate
  3355. acc_0 = _mm_add_ps(p0_d, acc_0);
  3356. acc_1 = _mm_add_ps(p1_d, acc_1);
  3357. acc_2 = _mm_add_ps(p2_d, acc_2);
  3358. acc_3 = _mm_add_ps(p3_d, acc_3);
  3359. }
  3360. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3361. #elif defined(__riscv_v_intrinsic)
  3362. float sumf = 0.0;
  3363. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3364. for (int i = 0; i < nb; i++) {
  3365. // load elements
  3366. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3367. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3368. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3369. // mask and store lower part of x, and then upper part
  3370. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3371. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3372. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3373. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3374. // subtract offset
  3375. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3376. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3377. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3378. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3379. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3380. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3381. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3382. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3383. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3384. }
  3385. *s = sumf;
  3386. #else
  3387. // scalar
  3388. float sumf = 0.0;
  3389. for (int i = 0; i < nb; i++) {
  3390. int sumi = 0;
  3391. for (int j = 0; j < qk/2; ++j) {
  3392. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3393. const int v1 = (x[i].qs[j] >> 4) - 8;
  3394. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3395. }
  3396. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3397. }
  3398. *s = sumf;
  3399. #endif
  3400. }
  3401. 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) {
  3402. const int qk = QK8_1;
  3403. const int nb = n / qk;
  3404. assert(n % qk == 0);
  3405. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3406. assert((nrc == 2) || (nrc == 1));
  3407. #else
  3408. assert(nrc == 1);
  3409. #endif
  3410. UNUSED(nrc);
  3411. UNUSED(bx);
  3412. UNUSED(by);
  3413. UNUSED(bs);
  3414. const block_q4_1 * restrict x = vx;
  3415. const block_q8_1 * restrict y = vy;
  3416. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3417. if (nrc == 2) {
  3418. const block_q4_1 * restrict vx0 = vx;
  3419. const block_q4_1 * restrict vx1 = vx + bx;
  3420. const block_q8_1 * restrict vy0 = vy;
  3421. const block_q8_1 * restrict vy1 = vy + by;
  3422. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3423. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3424. for (int i = 0; i < nb; i++) {
  3425. const block_q4_1 * restrict b_x0 = &vx0[i];
  3426. const block_q4_1 * restrict b_x1 = &vx1[i];
  3427. const block_q8_1 * restrict b_y0 = &vy0[i];
  3428. const block_q8_1 * restrict b_y1 = &vy1[i];
  3429. float32x4_t summs_t = {GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y0->s),
  3430. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y0->s),
  3431. GGML_FP16_TO_FP32(b_x0->m) * GGML_FP16_TO_FP32(b_y1->s),
  3432. GGML_FP16_TO_FP32(b_x1->m) * GGML_FP16_TO_FP32(b_y1->s)};
  3433. summs0 += summs_t;
  3434. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3435. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3436. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3437. // 4-bit -> 8-bit
  3438. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3439. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3440. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3441. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3442. // load y
  3443. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3444. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3445. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3446. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3447. // mmla into int32x4_t
  3448. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3449. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3450. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3451. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3452. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3453. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3454. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3455. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3456. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3457. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3458. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3459. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3460. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3461. l1, r1)), l2, r2)), l3, r3))), scale);
  3462. }
  3463. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3464. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3465. sumv2 = sumv2 + summs0;
  3466. vst1_f32(s, vget_low_f32(sumv2));
  3467. vst1_f32(s + bs, vget_high_f32(sumv2));
  3468. return;
  3469. }
  3470. #endif
  3471. // TODO: add WASM SIMD
  3472. #if defined(__ARM_NEON)
  3473. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3474. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3475. float summs = 0;
  3476. assert(nb % 2 == 0); // TODO: handle odd nb
  3477. for (int i = 0; i < nb; i += 2) {
  3478. const block_q4_1 * restrict x0 = &x[i + 0];
  3479. const block_q4_1 * restrict x1 = &x[i + 1];
  3480. const block_q8_1 * restrict y0 = &y[i + 0];
  3481. const block_q8_1 * restrict y1 = &y[i + 1];
  3482. 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);
  3483. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3484. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3485. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3486. // 4-bit -> 8-bit
  3487. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3488. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3489. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3490. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3491. // load y
  3492. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3493. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3494. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3495. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3496. // dot product into int32x4_t
  3497. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3498. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3499. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3500. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3501. }
  3502. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3503. #elif defined(__AVX2__) || defined(__AVX__)
  3504. // Initialize accumulator with zeros
  3505. __m256 acc = _mm256_setzero_ps();
  3506. float summs = 0;
  3507. // Main loop
  3508. for (int i = 0; i < nb; ++i) {
  3509. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3510. const float d1 = GGML_FP16_TO_FP32(y[i].d);
  3511. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3512. const __m256 d0v = _mm256_set1_ps( d0 );
  3513. const __m256 d1v = _mm256_set1_ps( d1 );
  3514. // Compute combined scales
  3515. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3516. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3517. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3518. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3519. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3520. // Accumulate d0*d1*x*y
  3521. #if defined(__AVX2__)
  3522. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3523. #else
  3524. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3525. #endif
  3526. }
  3527. *s = hsum_float_8(acc) + summs;
  3528. #elif defined(__riscv_v_intrinsic)
  3529. float sumf = 0.0;
  3530. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3531. for (int i = 0; i < nb; i++) {
  3532. // load elements
  3533. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3534. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3535. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3536. // mask and store lower part of x, and then upper part
  3537. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3538. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3539. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3540. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3541. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3542. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3543. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3544. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3545. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3546. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3547. 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);
  3548. }
  3549. *s = sumf;
  3550. #else
  3551. // scalar
  3552. float sumf = 0.0;
  3553. for (int i = 0; i < nb; i++) {
  3554. int sumi = 0;
  3555. for (int j = 0; j < qk/2; ++j) {
  3556. const int v0 = (x[i].qs[j] & 0x0F);
  3557. const int v1 = (x[i].qs[j] >> 4);
  3558. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3559. }
  3560. 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);
  3561. }
  3562. *s = sumf;
  3563. #endif
  3564. }
  3565. 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) {
  3566. const int qk = QK8_0;
  3567. const int nb = n / qk;
  3568. assert(n % qk == 0);
  3569. assert(qk == QK5_0);
  3570. assert(nrc == 1);
  3571. UNUSED(nrc);
  3572. UNUSED(bx);
  3573. UNUSED(by);
  3574. UNUSED(bs);
  3575. const block_q5_0 * restrict x = vx;
  3576. const block_q8_0 * restrict y = vy;
  3577. #if defined(__ARM_NEON)
  3578. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3579. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3580. uint32_t qh0;
  3581. uint32_t qh1;
  3582. uint64_t tmp0[4];
  3583. uint64_t tmp1[4];
  3584. assert(nb % 2 == 0); // TODO: handle odd nb
  3585. for (int i = 0; i < nb; i += 2) {
  3586. const block_q5_0 * restrict x0 = &x[i];
  3587. const block_q5_0 * restrict x1 = &x[i + 1];
  3588. const block_q8_0 * restrict y0 = &y[i];
  3589. const block_q8_0 * restrict y1 = &y[i + 1];
  3590. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3591. // extract the 5th bit via lookup table ((!b) << 4)
  3592. memcpy(&qh0, x0->qh, sizeof(qh0));
  3593. memcpy(&qh1, x1->qh, sizeof(qh1));
  3594. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3595. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3596. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3597. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3598. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3599. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3600. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3601. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3602. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3603. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3604. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3605. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3606. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3607. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3608. // 4-bit -> 8-bit
  3609. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3610. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3611. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3612. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3613. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3614. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3615. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3616. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3617. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3618. // load y
  3619. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3620. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3621. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3622. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3623. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3624. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3625. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3626. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3627. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3628. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3629. }
  3630. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3631. #elif defined(__wasm_simd128__)
  3632. v128_t sumv = wasm_f32x4_splat(0.0f);
  3633. uint32_t qh;
  3634. uint64_t tmp[4];
  3635. // TODO: check if unrolling this is better
  3636. for (int i = 0; i < nb; ++i) {
  3637. const block_q5_0 * restrict x0 = &x[i];
  3638. const block_q8_0 * restrict y0 = &y[i];
  3639. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3640. // extract the 5th bit
  3641. memcpy(&qh, x0->qh, sizeof(qh));
  3642. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3643. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3644. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3645. tmp[3] = table_b2b_1[(qh >> 24) ];
  3646. const v128_t qhl = wasm_v128_load(tmp + 0);
  3647. const v128_t qhh = wasm_v128_load(tmp + 2);
  3648. const v128_t v0 = wasm_v128_load(x0->qs);
  3649. // 4-bit -> 8-bit
  3650. const v128_t v0l = wasm_v128_and (v0, m4b);
  3651. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3652. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3653. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3654. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3655. // load y
  3656. const v128_t v1l = wasm_v128_load(y0->qs);
  3657. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3658. // int8x16 -> int16x8
  3659. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3660. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3661. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3662. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3663. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3664. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3665. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3666. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3667. // dot product
  3668. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3669. wasm_i32x4_add(
  3670. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3671. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3672. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3673. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3674. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3675. }
  3676. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3677. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3678. #elif defined(__AVX2__)
  3679. // Initialize accumulator with zeros
  3680. __m256 acc = _mm256_setzero_ps();
  3681. // Main loop
  3682. for (int i = 0; i < nb; i++) {
  3683. /* Compute combined scale for the block */
  3684. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3685. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3686. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3687. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3688. qx = _mm256_or_si256(qx, bxhi);
  3689. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3690. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3691. /* Multiply q with scale and accumulate */
  3692. acc = _mm256_fmadd_ps(d, q, acc);
  3693. }
  3694. *s = hsum_float_8(acc);
  3695. #elif defined(__AVX__)
  3696. // Initialize accumulator with zeros
  3697. __m256 acc = _mm256_setzero_ps();
  3698. __m128i mask = _mm_set1_epi8((char)0xF0);
  3699. // Main loop
  3700. for (int i = 0; i < nb; i++) {
  3701. /* Compute combined scale for the block */
  3702. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3703. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3704. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3705. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3706. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3707. bxhil = _mm_andnot_si128(bxhil, mask);
  3708. bxhih = _mm_andnot_si128(bxhih, mask);
  3709. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3710. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3711. bxl = _mm_or_si128(bxl, bxhil);
  3712. bxh = _mm_or_si128(bxh, bxhih);
  3713. bx_0 = MM256_SET_M128I(bxh, bxl);
  3714. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3715. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3716. /* Multiply q with scale and accumulate */
  3717. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3718. }
  3719. *s = hsum_float_8(acc);
  3720. #elif defined(__riscv_v_intrinsic)
  3721. float sumf = 0.0;
  3722. uint32_t qh;
  3723. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3724. // These temporary registers are for masking and shift operations
  3725. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3726. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3727. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3728. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3729. for (int i = 0; i < nb; i++) {
  3730. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3731. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3732. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3733. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3734. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3735. // ((qh & (1u << (j + 16))) >> (j + 12));
  3736. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3737. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3738. // narrowing
  3739. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3740. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3741. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3742. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3743. // load
  3744. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3745. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3746. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3747. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3748. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3749. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3750. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3751. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3752. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3753. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3754. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3755. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3756. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3757. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3758. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3759. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3760. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3761. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3762. }
  3763. *s = sumf;
  3764. #else
  3765. // scalar
  3766. float sumf = 0.0;
  3767. for (int i = 0; i < nb; i++) {
  3768. uint32_t qh;
  3769. memcpy(&qh, x[i].qh, sizeof(qh));
  3770. int sumi = 0;
  3771. for (int j = 0; j < qk/2; ++j) {
  3772. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3773. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3774. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3775. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3776. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3777. }
  3778. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3779. }
  3780. *s = sumf;
  3781. #endif
  3782. }
  3783. 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) {
  3784. const int qk = QK8_1;
  3785. const int nb = n / qk;
  3786. assert(n % qk == 0);
  3787. assert(qk == QK5_1);
  3788. assert(nrc == 1);
  3789. UNUSED(nrc);
  3790. UNUSED(bx);
  3791. UNUSED(by);
  3792. UNUSED(bs);
  3793. const block_q5_1 * restrict x = vx;
  3794. const block_q8_1 * restrict y = vy;
  3795. #if defined(__ARM_NEON)
  3796. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3797. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3798. float summs0 = 0.0f;
  3799. float summs1 = 0.0f;
  3800. uint32_t qh0;
  3801. uint32_t qh1;
  3802. uint64_t tmp0[4];
  3803. uint64_t tmp1[4];
  3804. assert(nb % 2 == 0); // TODO: handle odd nb
  3805. for (int i = 0; i < nb; i += 2) {
  3806. const block_q5_1 * restrict x0 = &x[i];
  3807. const block_q5_1 * restrict x1 = &x[i + 1];
  3808. const block_q8_1 * restrict y0 = &y[i];
  3809. const block_q8_1 * restrict y1 = &y[i + 1];
  3810. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3811. summs0 += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3812. summs1 += GGML_FP16_TO_FP32(x1->m) * GGML_FP16_TO_FP32(y1->s);
  3813. // extract the 5th bit via lookup table ((b) << 4)
  3814. memcpy(&qh0, x0->qh, sizeof(qh0));
  3815. memcpy(&qh1, x1->qh, sizeof(qh1));
  3816. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3817. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3818. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3819. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3820. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3821. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3822. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3823. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3824. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3825. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3826. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3827. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3828. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3829. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3830. // 4-bit -> 8-bit
  3831. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3832. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3833. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3834. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3835. // add high bit
  3836. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3837. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3838. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3839. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3840. // load y
  3841. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3842. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3843. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3844. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3845. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3846. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3847. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3848. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3849. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3850. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3851. }
  3852. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3853. #elif defined(__wasm_simd128__)
  3854. v128_t sumv = wasm_f32x4_splat(0.0f);
  3855. float summs = 0.0f;
  3856. uint32_t qh;
  3857. uint64_t tmp[4];
  3858. // TODO: check if unrolling this is better
  3859. for (int i = 0; i < nb; ++i) {
  3860. const block_q5_1 * restrict x0 = &x[i];
  3861. const block_q8_1 * restrict y0 = &y[i];
  3862. summs += GGML_FP16_TO_FP32(x0->m) * GGML_FP16_TO_FP32(y0->s);
  3863. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3864. // extract the 5th bit
  3865. memcpy(&qh, x0->qh, sizeof(qh));
  3866. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3867. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3868. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3869. tmp[3] = table_b2b_0[(qh >> 24) ];
  3870. const v128_t qhl = wasm_v128_load(tmp + 0);
  3871. const v128_t qhh = wasm_v128_load(tmp + 2);
  3872. const v128_t v0 = wasm_v128_load(x0->qs);
  3873. // 4-bit -> 8-bit
  3874. const v128_t v0l = wasm_v128_and (v0, m4b);
  3875. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3876. // add high bit
  3877. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3878. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3879. // load y
  3880. const v128_t v1l = wasm_v128_load(y0->qs);
  3881. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3882. // int8x16 -> int16x8
  3883. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3884. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3885. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3886. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3887. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3888. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3889. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3890. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3891. // dot product
  3892. sumv = wasm_f32x4_add(sumv,
  3893. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3894. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3895. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3896. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3897. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3898. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3899. }
  3900. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3901. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3902. #elif defined(__AVX2__)
  3903. // Initialize accumulator with zeros
  3904. __m256 acc = _mm256_setzero_ps();
  3905. float summs = 0.0f;
  3906. // Main loop
  3907. for (int i = 0; i < nb; i++) {
  3908. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3909. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3910. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3911. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3912. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3913. qx = _mm256_or_si256(qx, bxhi);
  3914. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3915. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3916. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  3917. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3918. }
  3919. *s = hsum_float_8(acc) + summs;
  3920. #elif defined(__AVX__)
  3921. // Initialize accumulator with zeros
  3922. __m256 acc = _mm256_setzero_ps();
  3923. __m128i mask = _mm_set1_epi8(0x10);
  3924. float summs = 0.0f;
  3925. // Main loop
  3926. for (int i = 0; i < nb; i++) {
  3927. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3928. summs += GGML_FP16_TO_FP32(x[i].m) * GGML_FP16_TO_FP32(y[i].s);
  3929. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3930. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3931. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3932. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3933. bxhil = _mm_and_si128(bxhil, mask);
  3934. bxhih = _mm_and_si128(bxhih, mask);
  3935. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3936. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3937. bxl = _mm_or_si128(bxl, bxhil);
  3938. bxh = _mm_or_si128(bxh, bxhih);
  3939. bx_0 = MM256_SET_M128I(bxh, bxl);
  3940. const __m256 dy = _mm256_set1_ps(GGML_FP16_TO_FP32(y[i].d));
  3941. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3942. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  3943. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3944. }
  3945. *s = hsum_float_8(acc) + summs;
  3946. #elif defined(__riscv_v_intrinsic)
  3947. float sumf = 0.0;
  3948. uint32_t qh;
  3949. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3950. // temporary registers for shift operations
  3951. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3952. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3953. for (int i = 0; i < nb; i++) {
  3954. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3955. // load qh
  3956. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3957. // ((qh >> (j + 0)) << 4) & 0x10;
  3958. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3959. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3960. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3961. // ((qh >> (j + 12)) ) & 0x10;
  3962. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3963. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3964. // narrowing
  3965. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3966. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3967. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3968. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3969. // load
  3970. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3971. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3972. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3973. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3974. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3975. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3976. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3977. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3978. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3979. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3980. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3981. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3982. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3983. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3984. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3985. 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);
  3986. }
  3987. *s = sumf;
  3988. #else
  3989. // scalar
  3990. float sumf = 0.0;
  3991. for (int i = 0; i < nb; i++) {
  3992. uint32_t qh;
  3993. memcpy(&qh, x[i].qh, sizeof(qh));
  3994. int sumi = 0;
  3995. for (int j = 0; j < qk/2; ++j) {
  3996. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3997. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3998. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3999. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  4000. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  4001. }
  4002. 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);
  4003. }
  4004. *s = sumf;
  4005. #endif
  4006. }
  4007. 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) {
  4008. const int qk = QK8_0;
  4009. const int nb = n / qk;
  4010. assert(n % qk == 0);
  4011. #if defined(__ARM_FEATURE_MATMUL_INT8)
  4012. assert((nrc == 2) || (nrc == 1));
  4013. #else
  4014. assert(nrc == 1);
  4015. #endif
  4016. UNUSED(nrc);
  4017. UNUSED(bx);
  4018. UNUSED(by);
  4019. UNUSED(bs);
  4020. const block_q8_0 * restrict x = vx;
  4021. const block_q8_0 * restrict y = vy;
  4022. #if defined(__ARM_FEATURE_MATMUL_INT8)
  4023. if (nrc == 2) {
  4024. const block_q8_0 * restrict vx0 = vx;
  4025. const block_q8_0 * restrict vx1 = vx + bx;
  4026. const block_q8_0 * restrict vy0 = vy;
  4027. const block_q8_0 * restrict vy1 = vy + by;
  4028. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4029. for (int i = 0; i < nb; i++) {
  4030. const block_q8_0 * restrict b_x0 = &vx0[i];
  4031. const block_q8_0 * restrict b_y0 = &vy0[i];
  4032. const block_q8_0 * restrict b_x1 = &vx1[i];
  4033. const block_q8_0 * restrict b_y1 = &vy1[i];
  4034. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  4035. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  4036. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  4037. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  4038. // load y
  4039. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  4040. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  4041. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  4042. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  4043. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  4044. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  4045. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  4046. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  4047. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4048. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4049. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4050. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4051. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4052. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4053. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4054. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4055. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  4056. l1, r1)), l2, r2)), l3, r3))), scale);
  4057. }
  4058. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  4059. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  4060. vst1_f32(s, vget_low_f32(sumv2));
  4061. vst1_f32(s + bs, vget_high_f32(sumv2));
  4062. return;
  4063. }
  4064. #endif
  4065. #if defined(__ARM_NEON)
  4066. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4067. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  4068. assert(nb % 2 == 0); // TODO: handle odd nb
  4069. for (int i = 0; i < nb; i += 2) {
  4070. const block_q8_0 * restrict x0 = &x[i + 0];
  4071. const block_q8_0 * restrict x1 = &x[i + 1];
  4072. const block_q8_0 * restrict y0 = &y[i + 0];
  4073. const block_q8_0 * restrict y1 = &y[i + 1];
  4074. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  4075. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  4076. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  4077. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  4078. // load y
  4079. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  4080. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  4081. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  4082. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  4083. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  4084. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  4085. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  4086. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  4087. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  4088. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  4089. }
  4090. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  4091. #elif defined(__AVX2__) || defined(__AVX__)
  4092. // Initialize accumulator with zeros
  4093. __m256 acc = _mm256_setzero_ps();
  4094. // Main loop
  4095. for (int i = 0; i < nb; ++i) {
  4096. // Compute combined scale for the block
  4097. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  4098. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  4099. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  4100. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  4101. // Multiply q with scale and accumulate
  4102. #if defined(__AVX2__)
  4103. acc = _mm256_fmadd_ps( d, q, acc );
  4104. #else
  4105. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  4106. #endif
  4107. }
  4108. *s = hsum_float_8(acc);
  4109. #elif defined(__riscv_v_intrinsic)
  4110. float sumf = 0.0;
  4111. size_t vl = __riscv_vsetvl_e8m1(qk);
  4112. for (int i = 0; i < nb; i++) {
  4113. // load elements
  4114. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  4115. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  4116. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  4117. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  4118. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  4119. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  4120. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4121. }
  4122. *s = sumf;
  4123. #else
  4124. // scalar
  4125. float sumf = 0.0;
  4126. for (int i = 0; i < nb; i++) {
  4127. int sumi = 0;
  4128. for (int j = 0; j < qk; j++) {
  4129. sumi += x[i].qs[j]*y[i].qs[j];
  4130. }
  4131. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4132. }
  4133. *s = sumf;
  4134. #endif
  4135. }
  4136. #if QK_K == 256
  4137. 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) {
  4138. assert(nrc == 1);
  4139. UNUSED(nrc);
  4140. UNUSED(bx);
  4141. UNUSED(by);
  4142. UNUSED(bs);
  4143. const block_q2_K * restrict x = vx;
  4144. const block_q8_K * restrict y = vy;
  4145. const int nb = n / QK_K;
  4146. #ifdef __ARM_NEON
  4147. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4148. const uint8x16_t m4 = vdupq_n_u8(0xF);
  4149. const int32x4_t vzero = vdupq_n_s32(0);
  4150. ggml_int8x16x2_t q2bytes;
  4151. uint8_t aux[16];
  4152. float sum = 0;
  4153. for (int i = 0; i < nb; ++i) {
  4154. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4155. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4156. const uint8_t * restrict q2 = x[i].qs;
  4157. const int8_t * restrict q8 = y[i].qs;
  4158. const uint8_t * restrict sc = x[i].scales;
  4159. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  4160. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  4161. vst1q_u8(aux, scales);
  4162. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  4163. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  4164. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  4165. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  4166. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  4167. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  4168. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  4169. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  4170. int isum = 0;
  4171. int is = 0;
  4172. // We use this macro instead of a function call because for some reason
  4173. // the code runs 2-3% slower, even if the function is declared inline
  4174. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  4175. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  4176. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  4177. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  4178. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  4179. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  4180. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  4181. MULTIPLY_ACCUM_WITH_SCALE((index));
  4182. for (int j = 0; j < QK_K/128; ++j) {
  4183. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  4184. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4185. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  4186. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  4187. MULTIPLY_ACCUM_WITH_SCALE(0);
  4188. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  4189. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  4190. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  4191. is += 8;
  4192. }
  4193. sum += d * isum;
  4194. }
  4195. *s = sum;
  4196. #elif defined __AVX2__
  4197. const __m256i m3 = _mm256_set1_epi8(3);
  4198. const __m128i m4 = _mm_set1_epi8(0xF);
  4199. __m256 acc = _mm256_setzero_ps();
  4200. for (int i = 0; i < nb; ++i) {
  4201. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4202. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4203. const uint8_t * restrict q2 = x[i].qs;
  4204. const int8_t * restrict q8 = y[i].qs;
  4205. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4206. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  4207. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4208. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4209. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4210. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4211. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4212. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4213. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4214. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4215. __m256i sumi = _mm256_setzero_si256();
  4216. for (int j = 0; j < QK_K/128; ++j) {
  4217. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4218. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4219. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4220. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4221. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4222. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4223. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4224. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4225. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4226. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4227. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4228. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4229. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4230. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4231. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4232. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4233. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4234. p0 = _mm256_add_epi32(p0, p1);
  4235. p2 = _mm256_add_epi32(p2, p3);
  4236. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4237. }
  4238. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4239. }
  4240. *s = hsum_float_8(acc);
  4241. #elif defined __AVX__
  4242. const __m128i m3 = _mm_set1_epi8(0x3);
  4243. const __m128i m4 = _mm_set1_epi8(0xF);
  4244. const __m128i m2 = _mm_set1_epi8(0x2);
  4245. __m256 acc = _mm256_setzero_ps();
  4246. for (int i = 0; i < nb; ++i) {
  4247. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4248. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4249. const uint8_t * restrict q2 = x[i].qs;
  4250. const int8_t * restrict q8 = y[i].qs;
  4251. // load mins and scales from block_q2_K.scales[QK_K/16]
  4252. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4253. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4254. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4255. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4256. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4257. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4258. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4259. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4260. // sumf += -dmin * summs in 32bits*8
  4261. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4262. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4263. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4264. const __m128i scales[2] = { scales_0, scales_1 };
  4265. __m128i sumi_0 = _mm_setzero_si128();
  4266. __m128i sumi_1 = _mm_setzero_si128();
  4267. for (int j = 0; j < QK_K/128; ++j) {
  4268. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4269. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4270. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4271. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4272. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4273. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4274. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4275. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4276. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4277. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4278. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4279. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4280. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4281. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4282. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4283. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4284. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4285. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4286. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4287. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4288. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4289. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4290. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4291. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4292. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4293. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4294. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4295. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4296. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4297. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4298. __m128i shuffle = _mm_set1_epi16(0x0100);
  4299. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4300. shuffle = _mm_add_epi16(shuffle, m2);
  4301. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4302. shuffle = _mm_add_epi16(shuffle, m2);
  4303. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4304. shuffle = _mm_add_epi16(shuffle, m2);
  4305. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4306. shuffle = _mm_add_epi16(shuffle, m2);
  4307. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4308. shuffle = _mm_add_epi16(shuffle, m2);
  4309. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4310. shuffle = _mm_add_epi16(shuffle, m2);
  4311. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4312. shuffle = _mm_add_epi16(shuffle, m2);
  4313. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4314. p0 = _mm_add_epi32(p0, p1);
  4315. p2 = _mm_add_epi32(p2, p3);
  4316. p4 = _mm_add_epi32(p4, p5);
  4317. p6 = _mm_add_epi32(p6, p7);
  4318. // isum in 32bits*4*2
  4319. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4320. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4321. }
  4322. // sumf += dall * isum - dmin * summs in 32bits
  4323. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4324. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4325. }
  4326. *s = hsum_float_8(acc);
  4327. #elif defined __riscv_v_intrinsic
  4328. float sumf = 0;
  4329. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4330. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4331. for (int i = 0; i < nb; ++i) {
  4332. const uint8_t * q2 = x[i].qs;
  4333. const int8_t * q8 = y[i].qs;
  4334. const uint8_t * sc = x[i].scales;
  4335. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4336. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4337. size_t vl = 16;
  4338. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4339. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4340. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4341. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4342. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4343. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4344. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4345. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4346. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4347. vl = 32;
  4348. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4349. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4350. uint8_t is=0;
  4351. int isum=0;
  4352. for (int j = 0; j < QK_K/128; ++j) {
  4353. // load Q2
  4354. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4355. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4356. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4357. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4358. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4359. // duplicate scale elements for product
  4360. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4361. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4362. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4363. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4364. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4365. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4366. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4367. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4368. // load Q8
  4369. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4370. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4371. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4372. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4373. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4374. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4375. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4376. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4377. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4378. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4379. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4380. q2+=32; q8+=128; is=8;
  4381. }
  4382. sumf += dall * isum;
  4383. }
  4384. *s = sumf;
  4385. #else
  4386. float sumf = 0;
  4387. for (int i = 0; i < nb; ++i) {
  4388. const uint8_t * q2 = x[i].qs;
  4389. const int8_t * q8 = y[i].qs;
  4390. const uint8_t * sc = x[i].scales;
  4391. int summs = 0;
  4392. for (int j = 0; j < 16; ++j) {
  4393. summs += y[i].bsums[j] * (sc[j] >> 4);
  4394. }
  4395. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4396. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4397. int isum = 0;
  4398. int is = 0;
  4399. int d;
  4400. for (int k = 0; k < QK_K/128; ++k) {
  4401. int shift = 0;
  4402. for (int j = 0; j < 4; ++j) {
  4403. d = sc[is++] & 0xF;
  4404. int isuml = 0;
  4405. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4406. isum += d * isuml;
  4407. d = sc[is++] & 0xF;
  4408. isuml = 0;
  4409. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4410. isum += d * isuml;
  4411. shift += 2;
  4412. q8 += 32;
  4413. }
  4414. q2 += 32;
  4415. }
  4416. sumf += dall * isum - dmin * summs;
  4417. }
  4418. *s = sumf;
  4419. #endif
  4420. }
  4421. #else
  4422. 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) {
  4423. assert(nrc == 1);
  4424. UNUSED(nrc);
  4425. UNUSED(bx);
  4426. UNUSED(by);
  4427. UNUSED(bs);
  4428. const block_q2_K * restrict x = vx;
  4429. const block_q8_K * restrict y = vy;
  4430. const int nb = n / QK_K;
  4431. #ifdef __ARM_NEON
  4432. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4433. const int32x4_t vzero = vdupq_n_s32(0);
  4434. ggml_int8x16x4_t q2bytes;
  4435. uint32_t aux32[2];
  4436. const uint8_t * scales = (const uint8_t *)aux32;
  4437. float sum = 0;
  4438. for (int i = 0; i < nb; ++i) {
  4439. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4440. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4441. const uint8_t * restrict q2 = x[i].qs;
  4442. const int8_t * restrict q8 = y[i].qs;
  4443. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4444. aux32[0] = sc[0] & 0x0f0f0f0f;
  4445. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4446. 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]);
  4447. int isum1 = 0, isum2 = 0;
  4448. const uint8x16_t q2bits = vld1q_u8(q2);
  4449. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4450. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4451. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4452. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4453. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4454. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4455. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4456. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4457. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4458. sum += d * (isum1 + isum2);
  4459. }
  4460. *s = sum;
  4461. #elif defined __AVX2__
  4462. const __m256i m3 = _mm256_set1_epi8(3);
  4463. __m256 acc = _mm256_setzero_ps();
  4464. uint32_t ud, um;
  4465. const uint8_t * restrict db = (const uint8_t *)&ud;
  4466. const uint8_t * restrict mb = (const uint8_t *)&um;
  4467. float summs = 0;
  4468. // TODO: optimize this
  4469. for (int i = 0; i < nb; ++i) {
  4470. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4471. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4472. const uint8_t * restrict q2 = x[i].qs;
  4473. const int8_t * restrict q8 = y[i].qs;
  4474. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4475. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4476. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4477. 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];
  4478. summs += dmin * smin;
  4479. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4480. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4481. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4482. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4483. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4484. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4485. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4486. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4487. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4488. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4489. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4490. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4491. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4492. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4493. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4494. }
  4495. *s = hsum_float_8(acc) + summs;
  4496. #elif defined __AVX__
  4497. const __m128i m3 = _mm_set1_epi8(3);
  4498. __m256 acc = _mm256_setzero_ps();
  4499. uint32_t ud, um;
  4500. const uint8_t * restrict db = (const uint8_t *)&ud;
  4501. const uint8_t * restrict mb = (const uint8_t *)&um;
  4502. float summs = 0;
  4503. // TODO: optimize this
  4504. for (int i = 0; i < nb; ++i) {
  4505. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4506. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4507. const uint8_t * restrict q2 = x[i].qs;
  4508. const int8_t * restrict q8 = y[i].qs;
  4509. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4510. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4511. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4512. 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];
  4513. summs += dmin * smin;
  4514. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4515. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4516. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4517. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4518. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4519. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4520. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4521. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4522. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4523. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4524. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4525. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4526. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4527. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4528. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4529. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4530. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4531. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4532. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4533. }
  4534. *s = hsum_float_8(acc) + summs;
  4535. #elif defined __riscv_v_intrinsic
  4536. uint32_t aux32[2];
  4537. const uint8_t * scales = (const uint8_t *)aux32;
  4538. float sumf = 0;
  4539. for (int i = 0; i < nb; ++i) {
  4540. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4541. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4542. const uint8_t * restrict q2 = x[i].qs;
  4543. const int8_t * restrict q8 = y[i].qs;
  4544. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4545. aux32[0] = sc[0] & 0x0f0f0f0f;
  4546. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4547. 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]);
  4548. int isum1 = 0;
  4549. int isum2 = 0;
  4550. size_t vl = 16;
  4551. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4552. // load Q2
  4553. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4554. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4555. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4556. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4557. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4558. // load Q8, and take product with Q2
  4559. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4560. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4561. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4562. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4563. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4564. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4565. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4566. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4567. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4568. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4569. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4570. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4571. sumf += d * (isum1 + isum2);
  4572. }
  4573. *s = sumf;
  4574. #else
  4575. float sumf = 0;
  4576. int isum[QK_K/16];
  4577. for (int i = 0; i < nb; ++i) {
  4578. const uint8_t * q2 = x[i].qs;
  4579. const int8_t * q8 = y[i].qs;
  4580. const uint8_t * sc = x[i].scales;
  4581. int summs = 0;
  4582. for (int j = 0; j < QK_K/16; ++j) {
  4583. summs += y[i].bsums[j] * (sc[j] >> 4);
  4584. }
  4585. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4586. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4587. memset(isum, 0, (QK_K/16)*sizeof(int));
  4588. for (int l = 0; l < 16; ++l) {
  4589. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4590. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4591. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4592. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4593. }
  4594. for (int l = 0; l < QK_K/16; ++l) {
  4595. isum[l] *= (sc[l] & 0xF);
  4596. }
  4597. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4598. }
  4599. *s = sumf;
  4600. #endif
  4601. }
  4602. #endif
  4603. #if QK_K == 256
  4604. 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) {
  4605. assert(n % QK_K == 0);
  4606. assert(nrc == 1);
  4607. UNUSED(nrc);
  4608. UNUSED(bx);
  4609. UNUSED(by);
  4610. UNUSED(bs);
  4611. const uint32_t kmask1 = 0x03030303;
  4612. const uint32_t kmask2 = 0x0f0f0f0f;
  4613. const block_q3_K * restrict x = vx;
  4614. const block_q8_K * restrict y = vy;
  4615. const int nb = n / QK_K;
  4616. #ifdef __ARM_NEON
  4617. uint32_t aux[3];
  4618. uint32_t utmp[4];
  4619. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4620. const int32x4_t vzero = vdupq_n_s32(0);
  4621. const uint8x16_t m0 = vdupq_n_u8(1);
  4622. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4623. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4624. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4625. const int8_t m32 = 32;
  4626. ggml_int8x16x4_t q3bytes;
  4627. float sum = 0;
  4628. for (int i = 0; i < nb; ++i) {
  4629. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4630. const uint8_t * restrict q3 = x[i].qs;
  4631. const uint8_t * restrict qh = x[i].hmask;
  4632. const int8_t * restrict q8 = y[i].qs;
  4633. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4634. ggml_uint8x16x4_t q3h;
  4635. int32_t isum = 0;
  4636. // Set up scales
  4637. memcpy(aux, x[i].scales, 12);
  4638. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4639. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4640. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4641. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4642. int8_t * scale = (int8_t *)utmp;
  4643. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4644. for (int j = 0; j < QK_K/128; ++j) {
  4645. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4646. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4647. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4648. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4649. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4650. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4651. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4652. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4653. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4654. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4655. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4656. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4657. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4658. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4659. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4660. scale += 4;
  4661. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4662. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4663. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4664. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4665. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4666. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4667. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4668. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4669. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4670. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4671. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4672. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4673. scale += 4;
  4674. if (j == 0) {
  4675. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4676. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4677. }
  4678. }
  4679. sum += d * isum;
  4680. }
  4681. *s = sum;
  4682. #elif defined __AVX2__
  4683. const __m256i m3 = _mm256_set1_epi8(3);
  4684. const __m256i mone = _mm256_set1_epi8(1);
  4685. const __m128i m32 = _mm_set1_epi8(32);
  4686. __m256 acc = _mm256_setzero_ps();
  4687. uint32_t aux[3];
  4688. for (int i = 0; i < nb; ++i) {
  4689. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4690. const uint8_t * restrict q3 = x[i].qs;
  4691. const int8_t * restrict q8 = y[i].qs;
  4692. // Set up scales
  4693. memcpy(aux, x[i].scales, 12);
  4694. __m128i scales128 = _mm_set_epi32(
  4695. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4696. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4697. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4698. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4699. scales128 = _mm_sub_epi8(scales128, m32);
  4700. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4701. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4702. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4703. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4704. // high bit
  4705. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4706. // integer accumulator
  4707. __m256i sumi = _mm256_setzero_si256();
  4708. int bit = 0;
  4709. int is = 0;
  4710. for (int j = 0; j < QK_K/128; ++j) {
  4711. // load low 2 bits
  4712. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4713. // prepare low and high bits
  4714. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4715. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4716. ++bit;
  4717. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4718. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4719. ++bit;
  4720. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4721. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4722. ++bit;
  4723. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4724. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4725. ++bit;
  4726. // load Q8 quants
  4727. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4728. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4729. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4730. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4731. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4732. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4733. // and 2 if the high bit was set)
  4734. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4735. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4736. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4737. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4738. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4739. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4740. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4741. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4742. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4743. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4744. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4745. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4746. // multiply with scales
  4747. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4748. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4749. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4750. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4751. // accumulate
  4752. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4753. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4754. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4755. }
  4756. // multiply with block scale and accumulate
  4757. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4758. }
  4759. *s = hsum_float_8(acc);
  4760. #elif defined __AVX__
  4761. const __m128i m3 = _mm_set1_epi8(3);
  4762. const __m128i mone = _mm_set1_epi8(1);
  4763. const __m128i m32 = _mm_set1_epi8(32);
  4764. const __m128i m2 = _mm_set1_epi8(2);
  4765. __m256 acc = _mm256_setzero_ps();
  4766. const uint32_t *aux;
  4767. for (int i = 0; i < nb; ++i) {
  4768. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4769. const uint8_t * restrict q3 = x[i].qs;
  4770. const int8_t * restrict q8 = y[i].qs;
  4771. // Set up scales
  4772. aux = (const uint32_t *)x[i].scales;
  4773. __m128i scales128 = _mm_set_epi32(
  4774. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4775. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4776. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4777. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4778. scales128 = _mm_sub_epi8(scales128, m32);
  4779. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4780. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4781. const __m128i scales[2] = { scales_0, scales_1 };
  4782. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4783. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4784. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4785. // integer accumulator
  4786. __m128i sumi_0 = _mm_setzero_si128();
  4787. __m128i sumi_1 = _mm_setzero_si128();
  4788. for (int j = 0; j < QK_K/128; ++j) {
  4789. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4790. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4791. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4792. // prepare low and high bits
  4793. const int bit = j << 2;
  4794. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4795. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4796. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4797. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4798. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4799. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4800. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4801. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4802. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4803. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4804. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4805. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4806. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4807. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4808. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4809. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4810. // load Q8 quants from block_q8_K.qs[QK_K]
  4811. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4812. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4813. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4814. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4815. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4816. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4817. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4818. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4819. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4820. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4821. // and 2 if the high bit was set)
  4822. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4823. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4824. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4825. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4826. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4827. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4828. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4829. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4830. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4831. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4832. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4833. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4834. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4835. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4836. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4837. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4838. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4839. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4840. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4841. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4842. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4843. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4844. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4845. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4846. // multiply with scales
  4847. __m128i shuffle = _mm_set1_epi16(0x0100);
  4848. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4849. shuffle = _mm_add_epi16(shuffle, m2);
  4850. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4851. shuffle = _mm_add_epi16(shuffle, m2);
  4852. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4853. shuffle = _mm_add_epi16(shuffle, m2);
  4854. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4855. shuffle = _mm_add_epi16(shuffle, m2);
  4856. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4857. shuffle = _mm_add_epi16(shuffle, m2);
  4858. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4859. shuffle = _mm_add_epi16(shuffle, m2);
  4860. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4861. shuffle = _mm_add_epi16(shuffle, m2);
  4862. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4863. // accumulate
  4864. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4865. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4866. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4867. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4868. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4869. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4870. }
  4871. // multiply with block scale and accumulate
  4872. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4873. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4874. }
  4875. *s = hsum_float_8(acc);
  4876. #elif defined __riscv_v_intrinsic
  4877. uint32_t aux[3];
  4878. uint32_t utmp[4];
  4879. float sumf = 0;
  4880. for (int i = 0; i < nb; ++i) {
  4881. const uint8_t * restrict q3 = x[i].qs;
  4882. const uint8_t * restrict qh = x[i].hmask;
  4883. const int8_t * restrict q8 = y[i].qs;
  4884. memcpy(aux, x[i].scales, 12);
  4885. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4886. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4887. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4888. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4889. int8_t * scale = (int8_t *)utmp;
  4890. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4891. size_t vl = 32;
  4892. uint8_t m = 1;
  4893. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4894. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4895. int sum_t = 0;
  4896. for (int j = 0; j < QK_K; j += 128) {
  4897. vl = 32;
  4898. // load Q3
  4899. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4900. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4901. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4902. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4903. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4904. // compute mask for subtraction
  4905. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4906. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4907. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4908. m <<= 1;
  4909. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4910. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4911. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4912. m <<= 1;
  4913. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4914. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4915. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4916. m <<= 1;
  4917. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4918. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4919. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4920. m <<= 1;
  4921. // load Q8 and take product with Q3
  4922. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4923. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4924. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4925. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4926. vl = 16;
  4927. // retrieve lane to multiply with scale
  4928. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4929. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4930. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4931. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4932. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4933. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4934. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4935. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4936. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4937. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4938. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4939. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4940. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4941. q3 += 32; q8 += 128; scale += 8;
  4942. }
  4943. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4944. sumf += d*sum_t;
  4945. }
  4946. *s = sumf;
  4947. #else
  4948. // scalar version
  4949. // This function is written like this so the compiler can manage to vectorize most of it
  4950. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4951. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4952. // The ideal situation would be if we could just write the code once, and the compiler would
  4953. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4954. // write vectorized versions for AVX, ARM_NEON, etc.
  4955. int8_t aux8[QK_K];
  4956. int16_t aux16[8];
  4957. float sums [8];
  4958. int32_t aux32[8];
  4959. memset(sums, 0, 8*sizeof(float));
  4960. uint32_t auxs[4];
  4961. const int8_t * scales = (const int8_t*)auxs;
  4962. float sumf = 0;
  4963. for (int i = 0; i < nb; ++i) {
  4964. const uint8_t * restrict q3 = x[i].qs;
  4965. const uint8_t * restrict hm = x[i].hmask;
  4966. const int8_t * restrict q8 = y[i].qs;
  4967. memset(aux32, 0, 8*sizeof(int32_t));
  4968. int8_t * restrict a = aux8;
  4969. uint8_t m = 1;
  4970. for (int j = 0; j < QK_K; j += 128) {
  4971. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4972. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4973. a += 32; m <<= 1;
  4974. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4975. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4976. a += 32; m <<= 1;
  4977. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4978. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4979. a += 32; m <<= 1;
  4980. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4981. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4982. a += 32; m <<= 1;
  4983. q3 += 32;
  4984. }
  4985. a = aux8;
  4986. memcpy(auxs, x[i].scales, 12);
  4987. uint32_t tmp = auxs[2];
  4988. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4989. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4990. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4991. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4992. for (int j = 0; j < QK_K/16; ++j) {
  4993. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4994. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4995. q8 += 8; a += 8;
  4996. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4997. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4998. q8 += 8; a += 8;
  4999. }
  5000. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5001. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5002. }
  5003. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5004. *s = sumf;
  5005. #endif
  5006. }
  5007. #else
  5008. 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) {
  5009. assert(n % QK_K == 0);
  5010. assert(nrc == 1);
  5011. UNUSED(nrc);
  5012. UNUSED(bx);
  5013. UNUSED(by);
  5014. UNUSED(bs);
  5015. const block_q3_K * restrict x = vx;
  5016. const block_q8_K * restrict y = vy;
  5017. const int nb = n / QK_K;
  5018. #ifdef __ARM_NEON
  5019. const int32x4_t vzero = vdupq_n_s32(0);
  5020. const uint8x16_t m3b = vdupq_n_u8(0x3);
  5021. const uint8x16_t mh = vdupq_n_u8(4);
  5022. ggml_int8x16x4_t q3bytes;
  5023. uint16_t aux16[2];
  5024. int8_t * scales = (int8_t *)aux16;
  5025. float sum = 0;
  5026. for (int i = 0; i < nb; ++i) {
  5027. ggml_uint8x16x4_t q3h;
  5028. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  5029. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  5030. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  5031. const uint16_t a = *(const uint16_t *)x[i].scales;
  5032. aux16[0] = a & 0x0f0f;
  5033. aux16[1] = (a >> 4) & 0x0f0f;
  5034. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  5035. 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]);
  5036. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5037. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  5038. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  5039. q3h.val[1] = vandq_u8(mh, htmp);
  5040. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  5041. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  5042. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  5043. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  5044. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  5045. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  5046. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  5047. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  5048. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  5049. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  5050. sum += d * isum;
  5051. }
  5052. *s = sum;
  5053. #elif defined __AVX2__
  5054. const __m256i m3 = _mm256_set1_epi8(3);
  5055. const __m256i m1 = _mm256_set1_epi8(1);
  5056. __m256 acc = _mm256_setzero_ps();
  5057. uint64_t aux64;
  5058. uint16_t aux16[2];
  5059. const int8_t * aux8 = (const int8_t *)aux16;
  5060. for (int i = 0; i < nb; ++i) {
  5061. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5062. const uint8_t * restrict q3 = x[i].qs;
  5063. const int8_t * restrict q8 = y[i].qs;
  5064. const uint16_t a = *(const uint16_t *)x[i].scales;
  5065. aux16[0] = a & 0x0f0f;
  5066. aux16[1] = (a >> 4) & 0x0f0f;
  5067. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  5068. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  5069. memcpy(&aux64, x[i].hmask, 8);
  5070. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  5071. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  5072. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  5073. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  5074. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  5075. // load low 2 bits
  5076. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  5077. // prepare low and high bits
  5078. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  5079. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  5080. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  5081. // load Q8 quants
  5082. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5083. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5084. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  5085. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5086. // and 2 if the high bit was set)
  5087. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  5088. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  5089. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  5090. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  5091. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5092. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5093. // multiply with scales
  5094. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5095. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5096. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  5097. // multiply with block scale and accumulate
  5098. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  5099. }
  5100. *s = hsum_float_8(acc);
  5101. #elif defined __AVX__
  5102. const __m128i m3 = _mm_set1_epi8(3);
  5103. const __m128i m1 = _mm_set1_epi8(1);
  5104. __m256 acc = _mm256_setzero_ps();
  5105. uint64_t aux64;
  5106. uint16_t aux16[2];
  5107. const int8_t * aux8 = (const int8_t *)aux16;
  5108. for (int i = 0; i < nb; ++i) {
  5109. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5110. const uint8_t * restrict q3 = x[i].qs;
  5111. const int8_t * restrict q8 = y[i].qs;
  5112. const uint16_t a = *(const uint16_t *)x[i].scales;
  5113. aux16[0] = a & 0x0f0f;
  5114. aux16[1] = (a >> 4) & 0x0f0f;
  5115. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  5116. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  5117. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  5118. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  5119. memcpy(&aux64, x[i].hmask, 8);
  5120. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  5121. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  5122. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  5123. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  5124. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  5125. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  5126. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  5127. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  5128. // load low 2 bits
  5129. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  5130. // prepare low and high bits
  5131. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  5132. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  5133. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  5134. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  5135. // load Q8 quants
  5136. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5137. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5138. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  5139. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5140. // and 2 if the high bit was set)
  5141. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  5142. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  5143. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  5144. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  5145. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  5146. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  5147. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  5148. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  5149. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5150. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5151. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5152. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5153. // multiply with scales
  5154. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5155. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  5156. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  5157. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  5158. p16_0 = _mm_add_epi32(p16_0, p16_2);
  5159. p16_1 = _mm_add_epi32(p16_1, p16_3);
  5160. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  5161. // multiply with block scale and accumulate
  5162. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  5163. }
  5164. *s = hsum_float_8(acc);
  5165. #elif defined __riscv_v_intrinsic
  5166. uint16_t aux16[2];
  5167. int8_t * scales = (int8_t *)aux16;
  5168. float sumf = 0;
  5169. for (int i = 0; i < nb; ++i) {
  5170. const uint8_t * restrict q3 = x[i].qs;
  5171. const int8_t * restrict q8 = y[i].qs;
  5172. const uint16_t a = *(const uint16_t *)x[i].scales;
  5173. aux16[0] = a & 0x0f0f;
  5174. aux16[1] = (a >> 4) & 0x0f0f;
  5175. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  5176. 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]);
  5177. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5178. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5179. // load qh
  5180. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  5181. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5182. size_t vl = 16;
  5183. // extend and combine both qh_x1 and qh_x2
  5184. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5185. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5186. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  5187. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5188. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  5189. // load Q3
  5190. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  5191. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  5192. 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);
  5193. 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);
  5194. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  5195. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  5196. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  5197. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  5198. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  5199. // load Q8 and take product with Q3
  5200. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5201. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5202. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5203. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5204. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5205. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5206. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5207. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5208. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  5209. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  5210. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  5211. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  5212. sumf += d * isum;
  5213. }
  5214. *s = sumf;
  5215. #else
  5216. int8_t aux8[QK_K];
  5217. int16_t aux16[8];
  5218. float sums [8];
  5219. int32_t aux32[8];
  5220. int32_t scales[4];
  5221. memset(sums, 0, 8*sizeof(float));
  5222. float sumf = 0;
  5223. for (int i = 0; i < nb; ++i) {
  5224. const uint8_t * restrict q3 = x[i].qs;
  5225. const uint8_t * restrict hm = x[i].hmask;
  5226. const int8_t * restrict q8 = y[i].qs;
  5227. int8_t * restrict a = aux8;
  5228. for (int l = 0; l < 8; ++l) {
  5229. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5230. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5231. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5232. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5233. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5234. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5235. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5236. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5237. }
  5238. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5239. scales[1] = (x[i].scales[0] >> 4) - 8;
  5240. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5241. scales[3] = (x[i].scales[1] >> 4) - 8;
  5242. memset(aux32, 0, 8*sizeof(int32_t));
  5243. for (int j = 0; j < QK_K/16; ++j) {
  5244. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5245. q8 += 8; a += 8;
  5246. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5247. q8 += 8; a += 8;
  5248. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5249. }
  5250. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5251. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5252. }
  5253. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5254. *s = sumf;
  5255. #endif
  5256. }
  5257. #endif
  5258. #if QK_K == 256
  5259. 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) {
  5260. assert(n % QK_K == 0);
  5261. assert(nrc == 1);
  5262. UNUSED(nrc);
  5263. UNUSED(bx);
  5264. UNUSED(by);
  5265. UNUSED(bs);
  5266. const block_q4_K * restrict x = vx;
  5267. const block_q8_K * restrict y = vy;
  5268. const int nb = n / QK_K;
  5269. static const uint32_t kmask1 = 0x3f3f3f3f;
  5270. static const uint32_t kmask2 = 0x0f0f0f0f;
  5271. static const uint32_t kmask3 = 0x03030303;
  5272. uint32_t utmp[4];
  5273. #ifdef __ARM_NEON
  5274. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5275. const int32x4_t mzero = vdupq_n_s32(0);
  5276. ggml_int8x16x2_t q4bytes;
  5277. ggml_int8x16x2_t q8bytes;
  5278. float sumf = 0;
  5279. for (int i = 0; i < nb; ++i) {
  5280. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5281. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5282. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5283. memcpy(utmp, x[i].scales, 12);
  5284. uint32x2_t mins8 = { 0 };
  5285. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5286. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5287. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5288. utmp[0] &= kmask1;
  5289. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5290. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5291. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5292. sumf -= dmin * vaddvq_s32(prod);
  5293. const uint8_t * scales = (const uint8_t *)utmp;
  5294. const uint8_t * restrict q4 = x[i].qs;
  5295. const int8_t * restrict q8 = y[i].qs;
  5296. int32_t sumi1 = 0;
  5297. int32_t sumi2 = 0;
  5298. for (int j = 0; j < QK_K/64; ++j) {
  5299. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5300. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5301. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5302. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5303. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5304. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5305. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5306. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5307. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5308. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5309. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5310. }
  5311. sumf += d * (sumi1 + sumi2);
  5312. }
  5313. *s = sumf;
  5314. #elif defined __AVX2__
  5315. const __m256i m4 = _mm256_set1_epi8(0xF);
  5316. __m256 acc = _mm256_setzero_ps();
  5317. __m128 acc_m = _mm_setzero_ps();
  5318. for (int i = 0; i < nb; ++i) {
  5319. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5320. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5321. memcpy(utmp, x[i].scales, 12);
  5322. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5323. const uint32_t uaux = utmp[1] & kmask1;
  5324. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5325. utmp[2] = uaux;
  5326. utmp[0] &= kmask1;
  5327. const uint8_t * restrict q4 = x[i].qs;
  5328. const int8_t * restrict q8 = y[i].qs;
  5329. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5330. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5331. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5332. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5333. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5334. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5335. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5336. __m256i sumi = _mm256_setzero_si256();
  5337. for (int j = 0; j < QK_K/64; ++j) {
  5338. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5339. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5340. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5341. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5342. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5343. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5344. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5345. p16l = _mm256_madd_epi16(scale_l, p16l);
  5346. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5347. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5348. p16h = _mm256_madd_epi16(scale_h, p16h);
  5349. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5350. sumi = _mm256_add_epi32(sumi, sumj);
  5351. }
  5352. __m256 vd = _mm256_set1_ps(d);
  5353. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5354. }
  5355. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5356. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5357. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5358. #elif defined __AVX__
  5359. const __m128i m4 = _mm_set1_epi8(0xF);
  5360. const __m128i m2 = _mm_set1_epi8(0x2);
  5361. __m256 acc = _mm256_setzero_ps();
  5362. __m128 acc_m = _mm_setzero_ps();
  5363. for (int i = 0; i < nb; ++i) {
  5364. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5365. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5366. const uint8_t * restrict q4 = x[i].qs;
  5367. const int8_t * restrict q8 = y[i].qs;
  5368. memcpy(utmp, x[i].scales, 12);
  5369. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5370. const uint32_t uaux = utmp[1] & kmask1;
  5371. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5372. utmp[2] = uaux;
  5373. utmp[0] &= kmask1;
  5374. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5375. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5376. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5377. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5378. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5379. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5380. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5381. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5382. __m128i sumi_0 = _mm_setzero_si128();
  5383. __m128i sumi_1 = _mm_setzero_si128();
  5384. __m128i shuffle = _mm_set1_epi16(0x0100);
  5385. for (int j = 0; j < QK_K/64; ++j) {
  5386. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5387. shuffle = _mm_add_epi16(shuffle, m2);
  5388. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5389. shuffle = _mm_add_epi16(shuffle, m2);
  5390. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5391. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5392. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5393. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5394. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5395. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5396. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5397. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5398. p16l = _mm_madd_epi16(scale_l, p16l);
  5399. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5400. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5401. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5402. p16l = _mm_madd_epi16(scale_l, p16l);
  5403. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5404. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5405. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5406. p16h = _mm_madd_epi16(scale_h, p16h);
  5407. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5408. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5409. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5410. p16h = _mm_madd_epi16(scale_h, p16h);
  5411. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5412. }
  5413. __m256 vd = _mm256_set1_ps(d);
  5414. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5415. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5416. }
  5417. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5418. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5419. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5420. #elif defined __riscv_v_intrinsic
  5421. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5422. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5423. float sumf = 0;
  5424. for (int i = 0; i < nb; ++i) {
  5425. size_t vl = 8;
  5426. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5427. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5428. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5429. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5430. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5431. memcpy(utmp, x[i].scales, 12);
  5432. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5433. const uint32_t uaux = utmp[1] & kmask1;
  5434. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5435. utmp[2] = uaux;
  5436. utmp[0] &= kmask1;
  5437. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5438. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5439. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5440. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5441. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5442. const uint8_t * restrict q4 = x[i].qs;
  5443. const int8_t * restrict q8 = y[i].qs;
  5444. vl = 32;
  5445. int32_t sum_1 = 0;
  5446. int32_t sum_2 = 0;
  5447. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5448. for (int j = 0; j < QK_K/64; ++j) {
  5449. // load Q4
  5450. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5451. // load Q8 and multiply it with lower Q4 nibble
  5452. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5453. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5454. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5455. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5456. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5457. // load Q8 and multiply it with upper Q4 nibble
  5458. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5459. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5460. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5461. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5462. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5463. q4 += 32; q8 += 64;
  5464. }
  5465. sumf += d*(sum_1 + sum_2);
  5466. }
  5467. *s = sumf;
  5468. #else
  5469. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5470. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5471. int8_t aux8[QK_K];
  5472. int16_t aux16[8];
  5473. float sums [8];
  5474. int32_t aux32[8];
  5475. memset(sums, 0, 8*sizeof(float));
  5476. float sumf = 0;
  5477. for (int i = 0; i < nb; ++i) {
  5478. const uint8_t * restrict q4 = x[i].qs;
  5479. const int8_t * restrict q8 = y[i].qs;
  5480. memset(aux32, 0, 8*sizeof(int32_t));
  5481. int8_t * restrict a = aux8;
  5482. for (int j = 0; j < QK_K/64; ++j) {
  5483. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5484. a += 32;
  5485. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5486. a += 32; q4 += 32;
  5487. }
  5488. memcpy(utmp, x[i].scales, 12);
  5489. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5490. const uint32_t uaux = utmp[1] & kmask1;
  5491. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5492. utmp[2] = uaux;
  5493. utmp[0] &= kmask1;
  5494. int sumi = 0;
  5495. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5496. a = aux8;
  5497. int is = 0;
  5498. for (int j = 0; j < QK_K/32; ++j) {
  5499. int32_t scale = scales[is++];
  5500. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5501. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5502. q8 += 8; a += 8;
  5503. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5504. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5505. q8 += 8; a += 8;
  5506. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5507. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5508. q8 += 8; a += 8;
  5509. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5510. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5511. q8 += 8; a += 8;
  5512. }
  5513. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5514. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5515. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5516. sumf -= dmin * sumi;
  5517. }
  5518. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5519. *s = sumf;
  5520. #endif
  5521. }
  5522. #else
  5523. 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) {
  5524. assert(n % QK_K == 0);
  5525. assert(nrc == 1);
  5526. UNUSED(nrc);
  5527. UNUSED(bx);
  5528. UNUSED(by);
  5529. UNUSED(bs);
  5530. const block_q4_K * restrict x = vx;
  5531. const block_q8_K * restrict y = vy;
  5532. const int nb = n / QK_K;
  5533. #ifdef __ARM_NEON
  5534. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5535. const int32x4_t mzero = vdupq_n_s32(0);
  5536. float sumf = 0;
  5537. ggml_int8x16x2_t q4bytes;
  5538. ggml_int8x16x4_t q8bytes;
  5539. float sum_mins = 0.f;
  5540. uint16_t aux16[2];
  5541. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5542. for (int i = 0; i < nb; ++i) {
  5543. const uint8_t * restrict q4 = x[i].qs;
  5544. const int8_t * restrict q8 = y[i].qs;
  5545. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5546. aux16[0] = a[0] & 0x0f0f;
  5547. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5548. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5549. sum_mins += y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * summi;
  5550. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5551. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5552. q8bytes = ggml_vld1q_s8_x4(q8);
  5553. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5554. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5555. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5556. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5557. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5558. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5559. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5560. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5561. sumf += d * (sumi1 + sumi2);
  5562. }
  5563. *s = sumf - sum_mins;
  5564. #elif defined __AVX2__
  5565. const __m256i m4 = _mm256_set1_epi8(0xF);
  5566. __m256 acc = _mm256_setzero_ps();
  5567. float summs = 0;
  5568. uint16_t aux16[2];
  5569. const uint8_t * scales = (const uint8_t *)aux16;
  5570. for (int i = 0; i < nb; ++i) {
  5571. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5572. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5573. const __m256 vd = _mm256_set1_ps(d);
  5574. const uint16_t * a = (const uint16_t *)x[i].scales;
  5575. aux16[0] = a[0] & 0x0f0f;
  5576. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5577. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5578. const uint8_t * restrict q4 = x[i].qs;
  5579. const int8_t * restrict q8 = y[i].qs;
  5580. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5581. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5582. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5583. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5584. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5585. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5586. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5587. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5588. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5589. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5590. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5591. }
  5592. *s = hsum_float_8(acc) - summs;
  5593. #elif defined __AVX__
  5594. const __m128i m4 = _mm_set1_epi8(0xF);
  5595. __m256 acc = _mm256_setzero_ps();
  5596. float summs = 0;
  5597. uint16_t aux16[2];
  5598. const uint8_t * scales = (const uint8_t *)aux16;
  5599. for (int i = 0; i < nb; ++i) {
  5600. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5601. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5602. const __m256 vd = _mm256_set1_ps(d);
  5603. const uint16_t * a = (const uint16_t *)x[i].scales;
  5604. aux16[0] = a[0] & 0x0f0f;
  5605. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5606. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5607. const uint8_t * restrict q4 = x[i].qs;
  5608. const int8_t * restrict q8 = y[i].qs;
  5609. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5610. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5611. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5612. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5613. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5614. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5615. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5616. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5617. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5618. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5619. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5620. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5621. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5622. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5623. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5624. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5625. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5626. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5627. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5628. }
  5629. *s = hsum_float_8(acc) - summs;
  5630. #elif defined __riscv_v_intrinsic
  5631. uint16_t s16[2];
  5632. const uint8_t * restrict scales = (const uint8_t *)s16;
  5633. float sumf = 0;
  5634. for (int i = 0; i < nb; ++i) {
  5635. const uint8_t * restrict q4 = x[i].qs;
  5636. const int8_t * restrict q8 = y[i].qs;
  5637. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5638. s16[0] = b[0] & 0x0f0f;
  5639. s16[1] = (b[0] >> 4) & 0x0f0f;
  5640. 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]));
  5641. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5642. size_t vl = 32;
  5643. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5644. // load Q4
  5645. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5646. // load Q8 and multiply it with lower Q4 nibble
  5647. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5648. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5649. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5650. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5651. // load Q8 and multiply it with upper Q4 nibble
  5652. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5653. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5654. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5655. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5656. }
  5657. *s = sumf;
  5658. #else
  5659. uint8_t aux8[QK_K];
  5660. int16_t aux16[16];
  5661. float sums [8];
  5662. memset(sums, 0, 8*sizeof(float));
  5663. uint16_t s16[2];
  5664. const uint8_t * restrict scales = (const uint8_t *)s16;
  5665. float sumf = 0;
  5666. for (int i = 0; i < nb; ++i) {
  5667. const uint8_t * restrict q4 = x[i].qs;
  5668. const int8_t * restrict q8 = y[i].qs;
  5669. uint8_t * restrict a = aux8;
  5670. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5671. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5672. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5673. s16[0] = b[0] & 0x0f0f;
  5674. s16[1] = (b[0] >> 4) & 0x0f0f;
  5675. 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]));
  5676. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5677. for (int j = 0; j < QK_K/32; ++j) {
  5678. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5679. q8 += 16; a += 16;
  5680. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5681. q8 += 16; a += 16;
  5682. const float dl = d * scales[j];
  5683. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5684. }
  5685. }
  5686. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5687. *s = sumf;
  5688. #endif
  5689. }
  5690. #endif
  5691. #if QK_K == 256
  5692. 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) {
  5693. assert(n % QK_K == 0);
  5694. assert(nrc == 1);
  5695. UNUSED(nrc);
  5696. UNUSED(bx);
  5697. UNUSED(by);
  5698. UNUSED(bs);
  5699. const block_q5_K * restrict x = vx;
  5700. const block_q8_K * restrict y = vy;
  5701. const int nb = n / QK_K;
  5702. static const uint32_t kmask1 = 0x3f3f3f3f;
  5703. static const uint32_t kmask2 = 0x0f0f0f0f;
  5704. static const uint32_t kmask3 = 0x03030303;
  5705. uint32_t utmp[4];
  5706. #ifdef __ARM_NEON
  5707. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5708. const uint8x16_t mone = vdupq_n_u8(1);
  5709. const uint8x16_t mtwo = vdupq_n_u8(2);
  5710. const int32x4_t mzero = vdupq_n_s32(0);
  5711. ggml_int8x16x4_t q5bytes;
  5712. float sumf = 0;
  5713. for (int i = 0; i < nb; ++i) {
  5714. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5715. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5716. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5717. memcpy(utmp, x[i].scales, 12);
  5718. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5719. const uint32_t uaux = utmp[1] & kmask1;
  5720. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5721. utmp[2] = uaux;
  5722. utmp[0] &= kmask1;
  5723. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5724. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5725. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5726. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5727. int32_t sumi_mins = vaddvq_s32(prod);
  5728. const uint8_t * scales = (const uint8_t *)utmp;
  5729. const uint8_t * restrict q5 = x[i].qs;
  5730. const uint8_t * restrict qh = x[i].qh;
  5731. const int8_t * restrict q8 = y[i].qs;
  5732. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5733. ggml_uint8x16x4_t q5h;
  5734. int32_t sumi = 0;
  5735. for (int j = 0; j < QK_K/64; ++j) {
  5736. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5737. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5738. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5739. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5740. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5741. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5742. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5743. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5744. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5745. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5746. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5747. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5748. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5749. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5750. }
  5751. sumf += d * sumi - dmin * sumi_mins;
  5752. }
  5753. *s = sumf;
  5754. #elif defined __AVX2__
  5755. const __m256i m4 = _mm256_set1_epi8(0xF);
  5756. const __m128i mzero = _mm_setzero_si128();
  5757. const __m256i mone = _mm256_set1_epi8(1);
  5758. __m256 acc = _mm256_setzero_ps();
  5759. float summs = 0.f;
  5760. for (int i = 0; i < nb; ++i) {
  5761. const uint8_t * restrict q5 = x[i].qs;
  5762. const int8_t * restrict q8 = y[i].qs;
  5763. #if QK_K == 256
  5764. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5765. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5766. memcpy(utmp, x[i].scales, 12);
  5767. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5768. const uint32_t uaux = utmp[1] & kmask1;
  5769. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5770. utmp[2] = uaux;
  5771. utmp[0] &= kmask1;
  5772. #else
  5773. // TODO
  5774. const float d = 0, dmin = 0;
  5775. #endif
  5776. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5777. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5778. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5779. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5780. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5781. summs += dmin * _mm_extract_epi32(hsum, 0);
  5782. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5783. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5784. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5785. __m256i hmask = mone;
  5786. __m256i sumi = _mm256_setzero_si256();
  5787. int bit = 0;
  5788. for (int j = 0; j < QK_K/64; ++j) {
  5789. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5790. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5791. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5792. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5793. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5794. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5795. hmask = _mm256_slli_epi16(hmask, 1);
  5796. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5797. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5798. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5799. hmask = _mm256_slli_epi16(hmask, 1);
  5800. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5801. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5802. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5803. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5804. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5805. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5806. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5807. }
  5808. __m256 vd = _mm256_set1_ps(d);
  5809. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5810. }
  5811. *s = hsum_float_8(acc) + summs;
  5812. #elif defined __AVX__
  5813. const __m128i m4 = _mm_set1_epi8(0xF);
  5814. const __m128i mzero = _mm_setzero_si128();
  5815. const __m128i mone = _mm_set1_epi8(1);
  5816. const __m128i m2 = _mm_set1_epi8(2);
  5817. __m256 acc = _mm256_setzero_ps();
  5818. float summs = 0.f;
  5819. for (int i = 0; i < nb; ++i) {
  5820. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5821. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5822. const uint8_t * restrict q5 = x[i].qs;
  5823. const int8_t * restrict q8 = y[i].qs;
  5824. memcpy(utmp, x[i].scales, 12);
  5825. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5826. const uint32_t uaux = utmp[1] & kmask1;
  5827. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5828. utmp[2] = uaux;
  5829. utmp[0] &= kmask1;
  5830. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5831. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5832. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5833. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5834. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5835. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5836. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5837. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5838. summs += dmin * _mm_extract_epi32(hsum, 0);
  5839. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5840. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5841. __m128i hmask = mone;
  5842. __m128i sumi_0 = _mm_setzero_si128();
  5843. __m128i sumi_1 = _mm_setzero_si128();
  5844. int bit = 0;
  5845. __m128i shuffle = _mm_set1_epi16(0x0100);
  5846. for (int j = 0; j < QK_K/64; ++j) {
  5847. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5848. shuffle = _mm_add_epi16(shuffle, m2);
  5849. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5850. shuffle = _mm_add_epi16(shuffle, m2);
  5851. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5852. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5853. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5854. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5855. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5856. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5857. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5858. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5859. hmask = _mm_slli_epi16(hmask, 1);
  5860. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5861. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5862. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5863. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5864. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5865. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5866. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5867. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5868. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5869. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5870. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5871. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5872. hmask = _mm_slli_epi16(hmask, 1);
  5873. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5874. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5875. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5876. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5877. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5878. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5879. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5880. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5881. }
  5882. __m256 vd = _mm256_set1_ps(d);
  5883. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5884. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5885. }
  5886. *s = hsum_float_8(acc) + summs;
  5887. #elif defined __riscv_v_intrinsic
  5888. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5889. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5890. float sumf = 0;
  5891. float sums = 0.0;
  5892. size_t vl;
  5893. for (int i = 0; i < nb; ++i) {
  5894. vl = 8;
  5895. const uint8_t * restrict q5 = x[i].qs;
  5896. const uint8_t * restrict hm = x[i].qh;
  5897. const int8_t * restrict q8 = y[i].qs;
  5898. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5899. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5900. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5901. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5902. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5903. memcpy(utmp, x[i].scales, 12);
  5904. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5905. const uint32_t uaux = utmp[1] & kmask1;
  5906. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5907. utmp[2] = uaux;
  5908. utmp[0] &= kmask1;
  5909. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5910. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5911. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5912. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5913. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5914. vl = 32;
  5915. int32_t aux32 = 0;
  5916. int is = 0;
  5917. uint8_t m = 1;
  5918. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5919. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5920. for (int j = 0; j < QK_K/64; ++j) {
  5921. // load Q5 and Q8
  5922. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5923. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5924. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5925. // compute mask for addition
  5926. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5927. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5928. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5929. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5930. m <<= 1;
  5931. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5932. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5933. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5934. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5935. m <<= 1;
  5936. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5937. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5938. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5939. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5940. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5941. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5942. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5943. q5 += 32; q8 += 64;
  5944. }
  5945. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5946. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5947. }
  5948. *s = sumf+sums;
  5949. #else
  5950. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5951. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5952. int8_t aux8[QK_K];
  5953. int16_t aux16[8];
  5954. float sums [8];
  5955. int32_t aux32[8];
  5956. memset(sums, 0, 8*sizeof(float));
  5957. float sumf = 0;
  5958. for (int i = 0; i < nb; ++i) {
  5959. const uint8_t * restrict q4 = x[i].qs;
  5960. const uint8_t * restrict hm = x[i].qh;
  5961. const int8_t * restrict q8 = y[i].qs;
  5962. memset(aux32, 0, 8*sizeof(int32_t));
  5963. int8_t * restrict a = aux8;
  5964. uint8_t m = 1;
  5965. for (int j = 0; j < QK_K/64; ++j) {
  5966. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5967. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5968. a += 32; m <<= 1;
  5969. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5970. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5971. a += 32; m <<= 1;
  5972. q4 += 32;
  5973. }
  5974. memcpy(utmp, x[i].scales, 12);
  5975. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5976. const uint32_t uaux = utmp[1] & kmask1;
  5977. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5978. utmp[2] = uaux;
  5979. utmp[0] &= kmask1;
  5980. int sumi = 0;
  5981. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5982. a = aux8;
  5983. int is = 0;
  5984. for (int j = 0; j < QK_K/32; ++j) {
  5985. int32_t scale = scales[is++];
  5986. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5987. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5988. q8 += 8; a += 8;
  5989. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5990. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5991. q8 += 8; a += 8;
  5992. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5993. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5994. q8 += 8; a += 8;
  5995. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5996. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5997. q8 += 8; a += 8;
  5998. }
  5999. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6000. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6001. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  6002. sumf -= dmin * sumi;
  6003. }
  6004. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6005. *s = sumf;
  6006. #endif
  6007. }
  6008. #else
  6009. 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) {
  6010. assert(n % QK_K == 0);
  6011. assert(nrc == 1);
  6012. UNUSED(nrc);
  6013. UNUSED(bx);
  6014. UNUSED(by);
  6015. UNUSED(bs);
  6016. const block_q5_K * restrict x = vx;
  6017. const block_q8_K * restrict y = vy;
  6018. const int nb = n / QK_K;
  6019. #ifdef __ARM_NEON
  6020. const uint8x16_t m4b = vdupq_n_u8(0xf);
  6021. const uint8x16_t mh = vdupq_n_u8(16);
  6022. const int32x4_t mzero = vdupq_n_s32(0);
  6023. ggml_int8x16x4_t q5bytes;
  6024. ggml_uint8x16x4_t q5h;
  6025. float sumf = 0;
  6026. for (int i = 0; i < nb; ++i) {
  6027. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6028. const int8_t * sc = x[i].scales;
  6029. const uint8_t * restrict q5 = x[i].qs;
  6030. const uint8_t * restrict qh = x[i].qh;
  6031. const int8_t * restrict q8 = y[i].qs;
  6032. const uint8x8_t qhbits = vld1_u8(qh);
  6033. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  6034. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6035. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  6036. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  6037. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  6038. q5h.val[2] = vbicq_u8(mh, htmp);
  6039. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  6040. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  6041. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  6042. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  6043. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  6044. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  6045. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  6046. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  6047. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  6048. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  6049. }
  6050. *s = sumf;
  6051. #elif defined __AVX2__
  6052. const __m256i m4 = _mm256_set1_epi8(0xF);
  6053. const __m256i mone = _mm256_set1_epi8(1);
  6054. __m256 acc = _mm256_setzero_ps();
  6055. for (int i = 0; i < nb; ++i) {
  6056. const uint8_t * restrict q5 = x[i].qs;
  6057. const int8_t * restrict q8 = y[i].qs;
  6058. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6059. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  6060. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  6061. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  6062. int64_t aux64;
  6063. memcpy(&aux64, x[i].qh, 8);
  6064. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  6065. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  6066. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  6067. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  6068. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  6069. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  6070. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6071. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6072. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  6073. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  6074. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  6075. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  6076. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  6077. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  6078. }
  6079. *s = hsum_float_8(acc);
  6080. #elif defined __AVX__
  6081. const __m128i m4 = _mm_set1_epi8(0xF);
  6082. const __m128i mone = _mm_set1_epi8(1);
  6083. __m256 acc = _mm256_setzero_ps();
  6084. for (int i = 0; i < nb; ++i) {
  6085. const uint8_t * restrict q5 = x[i].qs;
  6086. const int8_t * restrict q8 = y[i].qs;
  6087. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6088. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  6089. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  6090. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  6091. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  6092. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  6093. int64_t aux64;
  6094. memcpy(&aux64, x[i].qh, 8);
  6095. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  6096. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  6097. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  6098. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  6099. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  6100. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  6101. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  6102. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  6103. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  6104. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  6105. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6106. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6107. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  6108. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  6109. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  6110. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  6111. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  6112. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  6113. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  6114. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  6115. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  6116. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  6117. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  6118. }
  6119. *s = hsum_float_8(acc);
  6120. #elif defined __riscv_v_intrinsic
  6121. float sumf = 0;
  6122. for (int i = 0; i < nb; ++i) {
  6123. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6124. const int8_t * sc = x[i].scales;
  6125. const uint8_t * restrict q5 = x[i].qs;
  6126. const uint8_t * restrict qh = x[i].qh;
  6127. const int8_t * restrict q8 = y[i].qs;
  6128. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6129. // load qh
  6130. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  6131. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  6132. size_t vl = 16;
  6133. // combine both qh_1 and qh_2
  6134. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  6135. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  6136. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  6137. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  6138. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  6139. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  6140. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  6141. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  6142. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  6143. // load q5
  6144. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  6145. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  6146. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  6147. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  6148. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  6149. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  6150. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  6151. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  6152. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  6153. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  6154. // load Q8 and multiply it with Q5
  6155. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6156. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6157. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6158. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6159. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6160. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6161. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6162. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6163. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  6164. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  6165. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  6166. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  6167. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  6168. }
  6169. *s = sumf;
  6170. #else
  6171. int8_t aux8[QK_K];
  6172. int16_t aux16[16];
  6173. float sums [8];
  6174. memset(sums, 0, 8*sizeof(float));
  6175. float sumf = 0;
  6176. for (int i = 0; i < nb; ++i) {
  6177. const uint8_t * restrict q4 = x[i].qs;
  6178. const uint8_t * restrict hm = x[i].qh;
  6179. const int8_t * restrict q8 = y[i].qs;
  6180. int8_t * restrict a = aux8;
  6181. for (int l = 0; l < 32; ++l) {
  6182. a[l+ 0] = q4[l] & 0xF;
  6183. a[l+32] = q4[l] >> 4;
  6184. }
  6185. for (int is = 0; is < 8; ++is) {
  6186. uint8_t m = 1 << is;
  6187. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  6188. }
  6189. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6190. const int8_t * restrict sc = x[i].scales;
  6191. for (int j = 0; j < QK_K/16; ++j) {
  6192. const float dl = d * sc[j];
  6193. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  6194. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  6195. q8 += 16; a += 16;
  6196. }
  6197. }
  6198. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6199. *s = sumf;
  6200. #endif
  6201. }
  6202. #endif
  6203. #if QK_K == 256
  6204. 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) {
  6205. assert(n % QK_K == 0);
  6206. assert(nrc == 1);
  6207. UNUSED(nrc);
  6208. UNUSED(bx);
  6209. UNUSED(by);
  6210. UNUSED(bs);
  6211. const block_q6_K * restrict x = vx;
  6212. const block_q8_K * restrict y = vy;
  6213. const int nb = n / QK_K;
  6214. #ifdef __ARM_NEON
  6215. float sum = 0;
  6216. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6217. const int32x4_t vzero = vdupq_n_s32(0);
  6218. //const int8x16_t m32s = vdupq_n_s8(32);
  6219. const uint8x16_t mone = vdupq_n_u8(3);
  6220. ggml_int8x16x4_t q6bytes;
  6221. ggml_uint8x16x4_t q6h;
  6222. for (int i = 0; i < nb; ++i) {
  6223. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6224. const uint8_t * restrict q6 = x[i].ql;
  6225. const uint8_t * restrict qh = x[i].qh;
  6226. const int8_t * restrict q8 = y[i].qs;
  6227. const int8_t * restrict scale = x[i].scales;
  6228. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6229. const int8x16_t scales = vld1q_s8(scale);
  6230. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6231. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6232. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6233. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6234. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6235. int32_t isum_mins = vaddvq_s32(prod);
  6236. int32_t isum = 0;
  6237. for (int j = 0; j < QK_K/128; ++j) {
  6238. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6239. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6240. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6241. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6242. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6243. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6244. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6245. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6246. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6247. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6248. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6249. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6250. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6251. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6252. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6253. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6254. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6255. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6256. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6257. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6258. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6259. scale += 4;
  6260. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6261. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6262. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6263. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6264. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6265. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6266. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6267. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6268. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6269. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6270. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6271. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6272. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6273. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6274. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6275. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6276. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6277. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6278. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6279. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6280. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6281. scale += 4;
  6282. }
  6283. //sum += isum * d_all * y[i].d;
  6284. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6285. }
  6286. *s = sum;
  6287. #elif defined __AVX2__
  6288. const __m256i m4 = _mm256_set1_epi8(0xF);
  6289. const __m256i m2 = _mm256_set1_epi8(3);
  6290. const __m256i m32s = _mm256_set1_epi8(32);
  6291. __m256 acc = _mm256_setzero_ps();
  6292. for (int i = 0; i < nb; ++i) {
  6293. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6294. const uint8_t * restrict q4 = x[i].ql;
  6295. const uint8_t * restrict qh = x[i].qh;
  6296. const int8_t * restrict q8 = y[i].qs;
  6297. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6298. __m256i sumi = _mm256_setzero_si256();
  6299. int is = 0;
  6300. for (int j = 0; j < QK_K/128; ++j) {
  6301. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6302. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6303. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6304. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6305. is += 4;
  6306. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6307. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6308. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6309. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6310. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6311. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6312. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6313. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6314. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6315. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6316. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6317. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6318. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6319. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6320. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6321. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6322. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6323. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6324. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6325. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6326. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6327. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6328. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6329. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6330. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6331. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6332. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6333. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6334. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6335. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6336. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6337. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6338. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6339. }
  6340. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6341. }
  6342. *s = hsum_float_8(acc);
  6343. #elif defined __AVX__
  6344. const __m128i m4 = _mm_set1_epi8(0xF);
  6345. const __m128i m3 = _mm_set1_epi8(3);
  6346. const __m128i m32s = _mm_set1_epi8(32);
  6347. const __m128i m2 = _mm_set1_epi8(2);
  6348. __m256 acc = _mm256_setzero_ps();
  6349. for (int i = 0; i < nb; ++i) {
  6350. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6351. const uint8_t * restrict q4 = x[i].ql;
  6352. const uint8_t * restrict qh = x[i].qh;
  6353. const int8_t * restrict q8 = y[i].qs;
  6354. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6355. __m128i sumi_0 = _mm_setzero_si128();
  6356. __m128i sumi_1 = _mm_setzero_si128();
  6357. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6358. for (int j = 0; j < QK_K/128; ++j) {
  6359. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6360. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6361. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6362. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6363. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6364. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6365. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6366. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6367. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6368. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6369. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6370. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6371. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6372. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6373. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6374. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6375. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6376. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6377. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6378. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6379. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6380. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6381. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6382. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6383. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6384. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6385. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6386. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6387. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6388. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6389. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6390. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6391. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6392. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6393. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6394. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6395. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6396. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6397. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6398. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6399. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6400. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6401. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6402. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6403. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6404. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6405. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6406. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6407. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6408. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6409. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6410. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6411. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6412. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6413. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6414. shuffle = _mm_add_epi8(shuffle, m2);
  6415. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6416. shuffle = _mm_add_epi8(shuffle, m2);
  6417. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6418. shuffle = _mm_add_epi8(shuffle, m2);
  6419. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6420. shuffle = _mm_add_epi8(shuffle, m2);
  6421. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6422. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6423. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6424. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6425. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6426. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6427. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6428. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6429. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6430. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6431. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6432. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6433. }
  6434. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6435. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6436. }
  6437. *s = hsum_float_8(acc);
  6438. #elif defined __riscv_v_intrinsic
  6439. float sumf = 0;
  6440. for (int i = 0; i < nb; ++i) {
  6441. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6442. const uint8_t * restrict q6 = x[i].ql;
  6443. const uint8_t * restrict qh = x[i].qh;
  6444. const int8_t * restrict q8 = y[i].qs;
  6445. const int8_t * restrict scale = x[i].scales;
  6446. size_t vl;
  6447. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6448. int sum_t = 0;
  6449. int is = 0;
  6450. for (int j = 0; j < QK_K/128; ++j) {
  6451. vl = 32;
  6452. // load qh
  6453. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6454. // load Q6
  6455. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6456. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6457. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6458. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6459. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6460. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6461. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6462. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6463. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6464. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6465. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6466. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6467. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6468. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6469. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6470. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6471. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6472. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6473. // load Q8 and take product
  6474. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6475. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6476. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6477. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6478. vl = 16;
  6479. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6480. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6481. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6482. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6483. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6484. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6485. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6486. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6487. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6488. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6489. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6490. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6491. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6492. q6 += 64; qh += 32; q8 += 128; is=8;
  6493. }
  6494. sumf += d * sum_t;
  6495. }
  6496. *s = sumf;
  6497. #else
  6498. int8_t aux8[QK_K];
  6499. int16_t aux16[8];
  6500. float sums [8];
  6501. int32_t aux32[8];
  6502. memset(sums, 0, 8*sizeof(float));
  6503. float sumf = 0;
  6504. for (int i = 0; i < nb; ++i) {
  6505. const uint8_t * restrict q4 = x[i].ql;
  6506. const uint8_t * restrict qh = x[i].qh;
  6507. const int8_t * restrict q8 = y[i].qs;
  6508. memset(aux32, 0, 8*sizeof(int32_t));
  6509. int8_t * restrict a = aux8;
  6510. for (int j = 0; j < QK_K; j += 128) {
  6511. for (int l = 0; l < 32; ++l) {
  6512. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6513. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6514. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6515. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6516. }
  6517. a += 128;
  6518. q4 += 64;
  6519. qh += 32;
  6520. }
  6521. a = aux8;
  6522. int is = 0;
  6523. for (int j = 0; j < QK_K/16; ++j) {
  6524. int scale = x[i].scales[is++];
  6525. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6526. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6527. q8 += 8; a += 8;
  6528. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6529. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6530. q8 += 8; a += 8;
  6531. }
  6532. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6533. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6534. }
  6535. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6536. *s = sumf;
  6537. #endif
  6538. }
  6539. #else
  6540. 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) {
  6541. assert(n % QK_K == 0);
  6542. assert(nrc == 1);
  6543. UNUSED(nrc);
  6544. UNUSED(bx);
  6545. UNUSED(by);
  6546. UNUSED(bs);
  6547. const block_q6_K * restrict x = vx;
  6548. const block_q8_K * restrict y = vy;
  6549. const int nb = n / QK_K;
  6550. #ifdef __ARM_NEON
  6551. float sum = 0;
  6552. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6553. const int8x16_t m32s = vdupq_n_s8(32);
  6554. const int32x4_t vzero = vdupq_n_s32(0);
  6555. const uint8x16_t mone = vdupq_n_u8(3);
  6556. ggml_int8x16x4_t q6bytes;
  6557. ggml_uint8x16x4_t q6h;
  6558. for (int i = 0; i < nb; ++i) {
  6559. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6560. const uint8_t * restrict q6 = x[i].ql;
  6561. const uint8_t * restrict qh = x[i].qh;
  6562. const int8_t * restrict q8 = y[i].qs;
  6563. const int8_t * restrict scale = x[i].scales;
  6564. int32_t isum = 0;
  6565. uint8x16_t qhbits = vld1q_u8(qh);
  6566. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6567. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6568. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6569. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6570. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6571. shifted = vshrq_n_u8(qhbits, 4);
  6572. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6573. shifted = vshrq_n_u8(qhbits, 6);
  6574. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6575. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6576. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6577. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6578. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6579. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6580. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6581. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6582. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6583. sum += isum * d_all * y[i].d;
  6584. }
  6585. *s = sum;
  6586. #elif defined __AVX2__
  6587. const __m256i m4 = _mm256_set1_epi8(0xF);
  6588. const __m256i m2 = _mm256_set1_epi8(3);
  6589. const __m256i m32s = _mm256_set1_epi8(32);
  6590. __m256 acc = _mm256_setzero_ps();
  6591. for (int i = 0; i < nb; ++i) {
  6592. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6593. const uint8_t * restrict q4 = x[i].ql;
  6594. const uint8_t * restrict qh = x[i].qh;
  6595. const int8_t * restrict q8 = y[i].qs;
  6596. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6597. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6598. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6599. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6600. __m256i sumi = _mm256_setzero_si256();
  6601. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6602. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6603. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6604. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6605. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6606. 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);
  6607. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6608. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6609. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6610. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6611. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6612. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6613. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6614. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6615. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6616. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6617. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6618. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6619. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6620. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6621. }
  6622. *s = hsum_float_8(acc);
  6623. #elif defined __AVX__
  6624. const __m128i m4 = _mm_set1_epi8(0xF);
  6625. const __m128i m2 = _mm_set1_epi8(3);
  6626. const __m128i m32s = _mm_set1_epi8(32);
  6627. __m256 acc = _mm256_setzero_ps();
  6628. for (int i = 0; i < nb; ++i) {
  6629. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6630. const uint8_t * restrict q4 = x[i].ql;
  6631. const uint8_t * restrict qh = x[i].qh;
  6632. const int8_t * restrict q8 = y[i].qs;
  6633. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6634. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6635. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6636. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6637. __m128i sumi_0 = _mm_setzero_si128();
  6638. __m128i sumi_1 = _mm_setzero_si128();
  6639. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6640. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6641. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6642. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6643. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6644. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6645. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6646. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6647. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6648. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6649. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6650. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6651. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6652. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6653. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6654. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6655. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6656. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6657. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6658. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6659. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6660. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6661. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6662. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6663. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6664. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6665. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6666. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6667. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6668. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6669. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6670. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6671. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6672. }
  6673. *s = hsum_float_8(acc);
  6674. #elif defined __riscv_v_intrinsic
  6675. float sumf = 0;
  6676. for (int i = 0; i < nb; ++i) {
  6677. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6678. const uint8_t * restrict q6 = x[i].ql;
  6679. const uint8_t * restrict qh = x[i].qh;
  6680. const int8_t * restrict q8 = y[i].qs;
  6681. const int8_t * restrict scale = x[i].scales;
  6682. int32_t isum = 0;
  6683. size_t vl = 16;
  6684. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6685. // load Q6
  6686. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6687. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6688. // load qh
  6689. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6690. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6691. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6692. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6693. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6694. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6695. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6696. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6697. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6698. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6699. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6700. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6701. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6702. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6703. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6704. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6705. // load Q8 and take product
  6706. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6707. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6708. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6709. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6710. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6711. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6712. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6713. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6714. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6715. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6716. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6717. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6718. sumf += isum * d_all * y[i].d;
  6719. }
  6720. *s = sumf;
  6721. #else
  6722. int8_t aux8[QK_K];
  6723. int16_t aux16[8];
  6724. float sums [8];
  6725. int32_t aux32[8];
  6726. memset(sums, 0, 8*sizeof(float));
  6727. float sumf = 0;
  6728. for (int i = 0; i < nb; ++i) {
  6729. const uint8_t * restrict q4 = x[i].ql;
  6730. const uint8_t * restrict qh = x[i].qh;
  6731. const int8_t * restrict q8 = y[i].qs;
  6732. memset(aux32, 0, 8*sizeof(int32_t));
  6733. int8_t * restrict a = aux8;
  6734. for (int l = 0; l < 16; ++l) {
  6735. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6736. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6737. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6738. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6739. }
  6740. int is = 0;
  6741. for (int j = 0; j < QK_K/16; ++j) {
  6742. int scale = x[i].scales[is++];
  6743. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6744. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6745. q8 += 8; a += 8;
  6746. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6747. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6748. q8 += 8; a += 8;
  6749. }
  6750. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6751. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6752. }
  6753. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6754. *s = sumf;
  6755. #endif
  6756. }
  6757. #endif
  6758. #if defined (__AVX2__) || defined (__ARM_NEON)
  6759. static const int8_t keven_signs_q2xs[1024] = {
  6760. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6761. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6762. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6763. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6764. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6765. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6766. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6767. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6768. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6769. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6770. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6771. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6772. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6773. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6774. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6775. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6776. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6777. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6778. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6779. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6780. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6781. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6782. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6783. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6784. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6785. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6786. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6787. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6788. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6789. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6790. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6791. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6792. };
  6793. #endif
  6794. 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) {
  6795. assert(n % QK_K == 0);
  6796. assert(nrc == 1);
  6797. UNUSED(nrc);
  6798. UNUSED(bx);
  6799. UNUSED(by);
  6800. UNUSED(bs);
  6801. const block_iq2_xxs * restrict x = vx;
  6802. const block_q8_K * restrict y = vy;
  6803. const int nb = n / QK_K;
  6804. #if defined(__ARM_NEON)
  6805. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6806. uint32_t aux32[4];
  6807. const uint8_t * aux8 = (const uint8_t *)aux32;
  6808. ggml_int8x16x4_t q2u;
  6809. ggml_int8x16x4_t q2s;
  6810. ggml_int8x16x4_t q8b;
  6811. float sumf = 0;
  6812. for (int i = 0; i < nb; ++i) {
  6813. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6814. const uint16_t * restrict q2 = x[i].qs;
  6815. const int8_t * restrict q8 = y[i].qs;
  6816. float sumf1 = 0, sumf2 = 0;
  6817. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6818. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6819. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6820. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6821. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6822. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6823. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6824. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6825. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6826. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6827. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6828. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6829. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6830. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6831. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6832. 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]);
  6833. 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]);
  6834. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6835. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6836. }
  6837. sumf += d*(sumf1 + sumf2);
  6838. }
  6839. *s = 0.25f * sumf;
  6840. #elif defined(__AVX2__)
  6841. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6842. uint32_t aux32[4];
  6843. const uint8_t * aux8 = (const uint8_t *)aux32;
  6844. __m256 accumf = _mm256_setzero_ps();
  6845. for (int i = 0; i < nb; ++i) {
  6846. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6847. const uint16_t * restrict q2 = x[i].qs;
  6848. const int8_t * restrict q8 = y[i].qs;
  6849. __m256i sumi1 = _mm256_setzero_si256();
  6850. __m256i sumi2 = _mm256_setzero_si256();
  6851. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6852. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6853. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6854. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6855. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6856. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6857. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6858. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6859. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6860. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6861. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6862. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6863. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6864. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6865. const uint16_t ls1 = aux32[1] >> 28;
  6866. const uint16_t ls2 = aux32[3] >> 28;
  6867. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6868. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6869. sumi1 = _mm256_add_epi32(sumi1, p1);
  6870. sumi2 = _mm256_add_epi32(sumi2, p2);
  6871. }
  6872. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6873. }
  6874. *s = 0.125f * hsum_float_8(accumf);
  6875. #else
  6876. uint32_t aux32[2];
  6877. const uint8_t * aux8 = (const uint8_t *)aux32;
  6878. float sumf = 0.f;
  6879. for (int i = 0; i < nb; ++i) {
  6880. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6881. const uint16_t * restrict q2 = x[i].qs;
  6882. const int8_t * restrict q8 = y[i].qs;
  6883. int32_t bsum = 0;
  6884. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6885. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6886. q2 += 4;
  6887. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6888. int32_t sumi = 0;
  6889. for (int l = 0; l < 4; ++l) {
  6890. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6891. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6892. for (int j = 0; j < 8; ++j) {
  6893. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6894. }
  6895. q8 += 8;
  6896. }
  6897. bsum += sumi * ls;
  6898. }
  6899. sumf += d * bsum;
  6900. }
  6901. *s = 0.125f * sumf;
  6902. #endif
  6903. }
  6904. 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) {
  6905. assert(n % QK_K == 0);
  6906. assert(nrc == 1);
  6907. UNUSED(nrc);
  6908. UNUSED(bx);
  6909. UNUSED(by);
  6910. UNUSED(bs);
  6911. const block_iq2_xs * restrict x = vx;
  6912. const block_q8_K * restrict y = vy;
  6913. const int nb = n / QK_K;
  6914. #if defined(__ARM_NEON)
  6915. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6916. ggml_int8x16x4_t q2u;
  6917. ggml_int8x16x4_t q2s;
  6918. ggml_int8x16x4_t q8b;
  6919. int32x4x4_t scales32;
  6920. float sumf = 0;
  6921. for (int i = 0; i < nb; ++i) {
  6922. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6923. const uint16_t * restrict q2 = x[i].qs;
  6924. const int8_t * restrict q8 = y[i].qs;
  6925. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6926. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6927. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6928. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6929. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6930. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6931. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6932. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6933. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6934. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6935. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6936. int32x4_t sumi = vdupq_n_s32(0);
  6937. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6938. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6939. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6940. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6941. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6942. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6943. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6944. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6945. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6946. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6947. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6948. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6949. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6950. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6951. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6952. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6953. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6954. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6955. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6956. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6957. q2 += 8;
  6958. }
  6959. sumf += d*vaddvq_s32(sumi);
  6960. }
  6961. *s = 0.125f * sumf;
  6962. #elif defined(__AVX2__)
  6963. const __m256i mone = _mm256_set1_epi8(1);
  6964. static const char block_sign_shuffle_mask_1[32] = {
  6965. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6966. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6967. };
  6968. static const char block_sign_shuffle_mask_2[32] = {
  6969. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6970. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6971. };
  6972. static const uint8_t bit_selector_mask_bytes[32] = {
  6973. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6974. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6975. };
  6976. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6977. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6978. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6979. #if QK_K == 64
  6980. static const uint8_t k_bit_helper[16] = {
  6981. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6982. };
  6983. const __m128i bit_helper = _mm_loadu_si128((const __m128i*)k_bit_helper);
  6984. const __m128i m511 = _mm_set1_epi16(511);
  6985. typedef union {
  6986. __m128i vec_index;
  6987. uint16_t index[8];
  6988. } index_t;
  6989. index_t idx;
  6990. __m256 accumf = _mm256_setzero_ps();
  6991. for (int i = 0; i < nb; ++i) {
  6992. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6993. const __m128i q2_data = _mm_loadu_si128((const __m128i*)x[i].qs);
  6994. idx.vec_index = _mm_and_si128(q2_data, m511);
  6995. const __m128i partial_sign_bits = _mm_srli_epi16(q2_data, 9);
  6996. const __m128i partial_sign_bits_upper = _mm_srli_epi16(q2_data, 13);
  6997. const __m128i partial_sign_bits_for_counting = _mm_xor_si128(partial_sign_bits, partial_sign_bits_upper);
  6998. const __m128i odd_bits = _mm_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6999. const __m128i full_sign_bits = _mm_or_si128(partial_sign_bits, odd_bits);
  7000. const __m256i full_signs = MM256_SET_M128I(full_sign_bits, full_sign_bits);
  7001. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  7002. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)(y[i].qs+32));
  7003. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[idx.index[3]], iq2xs_grid[idx.index[2]],
  7004. iq2xs_grid[idx.index[1]], iq2xs_grid[idx.index[0]]);
  7005. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[idx.index[7]], iq2xs_grid[idx.index[6]],
  7006. iq2xs_grid[idx.index[5]], iq2xs_grid[idx.index[4]]);
  7007. __m256i signs;
  7008. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_1);
  7009. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7010. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  7011. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_2);
  7012. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7013. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  7014. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7015. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7016. 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));
  7017. 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));
  7018. const __m256i sum = _mm256_add_epi32(_mm256_madd_epi16(sc1, dot1), _mm256_madd_epi16(sc2, dot2));
  7019. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sum), accumf);
  7020. }
  7021. *s = 0.125f * hsum_float_8(accumf);
  7022. #else
  7023. static const uint8_t k_bit_helper[32] = {
  7024. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  7025. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  7026. };
  7027. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  7028. const __m256i m511 = _mm256_set1_epi16(511);
  7029. const __m128i m4 = _mm_set1_epi8(0xf);
  7030. const __m128i m1 = _mm_set1_epi8(1);
  7031. uint64_t aux64;
  7032. // somewhat hacky, but gives a significant boost in performance
  7033. __m256i aux_gindex;
  7034. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  7035. __m256 accumf = _mm256_setzero_ps();
  7036. for (int i = 0; i < nb; ++i) {
  7037. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7038. const uint16_t * restrict q2 = x[i].qs;
  7039. const int8_t * restrict q8 = y[i].qs;
  7040. memcpy(&aux64, x[i].scales, 8);
  7041. __m128i stmp = _mm_set1_epi64x(aux64);
  7042. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  7043. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  7044. __m256i sumi1 = _mm256_setzero_si256();
  7045. __m256i sumi2 = _mm256_setzero_si256();
  7046. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  7047. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  7048. aux_gindex = _mm256_and_si256(q2_data, m511);
  7049. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  7050. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  7051. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  7052. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  7053. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  7054. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7055. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7056. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7057. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7058. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  7059. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  7060. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  7061. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  7062. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  7063. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  7064. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  7065. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  7066. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  7067. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  7068. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  7069. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  7070. __m256i signs;
  7071. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  7072. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7073. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  7074. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  7075. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7076. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  7077. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  7078. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7079. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  7080. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  7081. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7082. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  7083. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7084. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7085. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  7086. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  7087. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  7088. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  7089. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  7090. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  7091. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  7092. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  7093. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  7094. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  7095. }
  7096. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7097. }
  7098. *s = 0.125f * hsum_float_8(accumf);
  7099. #endif
  7100. #else
  7101. float sumf = 0.f;
  7102. for (int i = 0; i < nb; ++i) {
  7103. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7104. const uint16_t * restrict q2 = x[i].qs;
  7105. const uint8_t * restrict sc = x[i].scales;
  7106. const int8_t * restrict q8 = y[i].qs;
  7107. int32_t bsum = 0;
  7108. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7109. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  7110. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  7111. int32_t sumi = 0;
  7112. for (int l = 0; l < 2; ++l) {
  7113. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7114. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7115. for (int j = 0; j < 8; ++j) {
  7116. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7117. }
  7118. q8 += 8;
  7119. }
  7120. bsum += sumi * ls1;
  7121. sumi = 0;
  7122. for (int l = 2; l < 4; ++l) {
  7123. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7124. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7125. for (int j = 0; j < 8; ++j) {
  7126. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7127. }
  7128. q8 += 8;
  7129. }
  7130. bsum += sumi * ls2;
  7131. q2 += 4;
  7132. }
  7133. sumf += d * bsum;
  7134. }
  7135. *s = 0.125f * sumf;
  7136. #endif
  7137. }
  7138. 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) {
  7139. assert(n % QK_K == 0);
  7140. assert(nrc == 1);
  7141. UNUSED(nrc);
  7142. UNUSED(bx);
  7143. UNUSED(by);
  7144. UNUSED(bs);
  7145. const block_iq2_s * restrict x = vx;
  7146. const block_q8_K * restrict y = vy;
  7147. const int nb = n / QK_K;
  7148. #if defined(__ARM_NEON)
  7149. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7150. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7151. };
  7152. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7153. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7154. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7155. const uint8x16_t m1 = vdupq_n_u8(1);
  7156. const int32x4_t vzero = vdupq_n_s32(0);
  7157. uint8x16x2_t vs;
  7158. ggml_int8x16x4_t q2s;
  7159. ggml_int8x16x4_t q8b;
  7160. float sumf = 0;
  7161. for (int i = 0; i < nb; ++i) {
  7162. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7163. const uint8_t * restrict qs = x[i].qs;
  7164. const uint8_t * restrict qh = x[i].qh;
  7165. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7166. const int8_t * restrict q8 = y[i].qs;
  7167. int sumi1 = 0, sumi2 = 0;
  7168. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7169. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7170. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  7171. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  7172. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  7173. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  7174. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  7175. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  7176. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  7177. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  7178. qs += 8;
  7179. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7180. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7181. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7182. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7183. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7184. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  7185. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  7186. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7187. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7188. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7189. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7190. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7191. signs += 4;
  7192. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  7193. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  7194. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  7195. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  7196. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  7197. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  7198. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  7199. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  7200. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  7201. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  7202. }
  7203. sumf += d*(sumi1 + sumi2);
  7204. }
  7205. *s = 0.125f * sumf;
  7206. #elif defined(__AVX2__)
  7207. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7208. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7209. };
  7210. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7211. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7212. };
  7213. const __m128i m4 = _mm_set1_epi8(0xf);
  7214. const __m128i m1 = _mm_set1_epi8(1);
  7215. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7216. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7217. uint64_t aux64;
  7218. __m256 accumf = _mm256_setzero_ps();
  7219. for (int i = 0; i < nb; ++i) {
  7220. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7221. const uint8_t * restrict qs = x[i].qs;
  7222. const uint8_t * restrict qh = x[i].qh;
  7223. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7224. const int8_t * restrict q8 = y[i].qs;
  7225. memcpy(&aux64, x[i].scales, 8);
  7226. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7227. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7228. __m256i sumi1 = _mm256_setzero_si256();
  7229. __m256i sumi2 = _mm256_setzero_si256();
  7230. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7231. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7232. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7233. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7234. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7235. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7236. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7237. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7238. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7239. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7240. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7241. qs += 8;
  7242. __m256i aux256 = _mm256_set1_epi32(signs[0] | ((uint32_t) signs[1] << 16));
  7243. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7244. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7245. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7246. aux256 = _mm256_set1_epi32(signs[2] | ((uint32_t) signs[3] << 16));
  7247. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7248. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7249. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7250. signs += 4;
  7251. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7252. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7253. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7254. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7255. sumi1 = _mm256_add_epi32(sumi1, p1);
  7256. sumi2 = _mm256_add_epi32(sumi2, p2);
  7257. }
  7258. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7259. }
  7260. *s = 0.125f * hsum_float_8(accumf);
  7261. #else
  7262. float sumf = 0;
  7263. for (int i = 0; i < nb; i++) {
  7264. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7265. const int8_t * q8 = y[i].qs;
  7266. const uint8_t * qs = x[i].qs;
  7267. const uint8_t * qh = x[i].qh;
  7268. const uint8_t * signs = qs + QK_K/8;
  7269. int bsum = 0;
  7270. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7271. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7272. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7273. int sumi1 = 0, sumi2 = 0;
  7274. for (int l = 0; l < 2; ++l) {
  7275. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7276. for (int j = 0; j < 8; ++j) {
  7277. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7278. }
  7279. q8 += 8;
  7280. }
  7281. for (int l = 2; l < 4; ++l) {
  7282. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7283. for (int j = 0; j < 8; ++j) {
  7284. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7285. }
  7286. q8 += 8;
  7287. }
  7288. bsum += ls1 * sumi1 + ls2 * sumi2;
  7289. qs += 4;
  7290. signs += 4;
  7291. }
  7292. sumf += d * bsum;
  7293. }
  7294. *s = 0.125f * sumf;
  7295. #endif
  7296. }
  7297. 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) {
  7298. assert(n % QK_K == 0);
  7299. assert(nrc == 1);
  7300. UNUSED(nrc);
  7301. UNUSED(bx);
  7302. UNUSED(by);
  7303. UNUSED(bs);
  7304. const block_iq3_xxs * restrict x = vx;
  7305. const block_q8_K * restrict y = vy;
  7306. const int nb = n / QK_K;
  7307. #if defined(__ARM_NEON)
  7308. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7309. uint32_t aux32[2];
  7310. ggml_int8x16x4_t q3s;
  7311. ggml_int8x16x4_t q8b;
  7312. float sumf = 0;
  7313. for (int i = 0; i < nb; ++i) {
  7314. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7315. const uint8_t * restrict q3 = x[i].qs;
  7316. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7317. const int8_t * restrict q8 = y[i].qs;
  7318. float sumf1 = 0, sumf2 = 0;
  7319. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7320. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7321. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7322. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7323. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7324. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7325. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7326. q3 += 16;
  7327. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7328. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7329. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7330. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7331. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7332. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7333. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7334. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7335. 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]);
  7336. 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]);
  7337. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7338. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7339. }
  7340. sumf += d*(sumf1 + sumf2);
  7341. }
  7342. *s = 0.5f * sumf;
  7343. #elif defined(__AVX2__)
  7344. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7345. uint32_t aux32[2];
  7346. __m256 accumf = _mm256_setzero_ps();
  7347. for (int i = 0; i < nb; ++i) {
  7348. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7349. const uint8_t * restrict q3 = x[i].qs;
  7350. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7351. const int8_t * restrict q8 = y[i].qs;
  7352. __m256i sumi1 = _mm256_setzero_si256();
  7353. __m256i sumi2 = _mm256_setzero_si256();
  7354. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7355. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7356. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7357. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7358. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7359. q3 += 8;
  7360. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7361. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7362. q3 += 8;
  7363. memcpy(aux32, gas, 8); gas += 8;
  7364. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7365. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7366. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7367. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7368. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7369. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7370. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7371. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7372. const uint16_t ls1 = aux32[0] >> 28;
  7373. const uint16_t ls2 = aux32[1] >> 28;
  7374. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7375. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7376. sumi1 = _mm256_add_epi32(sumi1, p1);
  7377. sumi2 = _mm256_add_epi32(sumi2, p2);
  7378. }
  7379. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7380. }
  7381. *s = 0.25f * hsum_float_8(accumf);
  7382. #else
  7383. uint32_t aux32;
  7384. float sumf = 0.f;
  7385. for (int i = 0; i < nb; ++i) {
  7386. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7387. const uint8_t * restrict q3 = x[i].qs;
  7388. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7389. const int8_t * restrict q8 = y[i].qs;
  7390. int32_t bsum = 0;
  7391. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7392. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  7393. const uint32_t ls = 2*(aux32 >> 28) + 1;
  7394. int32_t sumi = 0;
  7395. for (int l = 0; l < 4; ++l) {
  7396. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  7397. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  7398. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  7399. for (int j = 0; j < 4; ++j) {
  7400. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  7401. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  7402. }
  7403. q8 += 8;
  7404. }
  7405. q3 += 8;
  7406. bsum += sumi * ls;
  7407. }
  7408. sumf += d * bsum;
  7409. }
  7410. *s = 0.25f * sumf;
  7411. #endif
  7412. }
  7413. 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) {
  7414. assert(n % QK_K == 0);
  7415. assert(nrc == 1);
  7416. UNUSED(nrc);
  7417. UNUSED(bx);
  7418. UNUSED(by);
  7419. UNUSED(bs);
  7420. const block_iq3_s * restrict x = vx;
  7421. const block_q8_K * restrict y = vy;
  7422. const int nb = n / QK_K;
  7423. #if defined(__ARM_NEON)
  7424. typedef union {
  7425. uint16x8_t vec_index;
  7426. uint16_t index[8];
  7427. } vec_index_t;
  7428. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7429. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7430. };
  7431. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7432. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  7433. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7434. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7435. const int16x8_t hshift = vld1q_s16(k_shift);
  7436. const uint16x8_t m256 = vdupq_n_u16(256);
  7437. const uint8x16_t m1 = vdupq_n_u8(1);
  7438. uint8x16x2_t vs;
  7439. ggml_int8x16x4_t q3s;
  7440. ggml_int8x16x4_t q8b;
  7441. vec_index_t idx;
  7442. #if QK_K == 256
  7443. uint32_t scales32[2];
  7444. const uint8_t * scales8 = (const uint8_t *)scales32;
  7445. #endif
  7446. float sumf = 0;
  7447. for (int i = 0; i < nb; ++i) {
  7448. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7449. const uint8_t * restrict qs = x[i].qs;
  7450. const uint8_t * restrict qh = x[i].qh;
  7451. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7452. const int8_t * restrict q8 = y[i].qs;
  7453. #if QK_K == 256
  7454. memcpy(scales32, x[i].scales, 4);
  7455. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  7456. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  7457. #endif
  7458. int sumi1 = 0, sumi2 = 0;
  7459. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7460. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7461. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  7462. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  7463. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7464. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7465. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7466. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7467. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  7468. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7469. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7470. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7471. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7472. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | ((uint32_t) signs[1] << 16)));
  7473. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7474. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7475. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7476. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7477. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  7478. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  7479. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | ((uint32_t) signs[3] << 16)));
  7480. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7481. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7482. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7483. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7484. signs += 4;
  7485. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  7486. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  7487. 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]);
  7488. 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]);
  7489. #if QK_K == 256
  7490. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  7491. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  7492. #else
  7493. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  7494. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32/2] >> 4));
  7495. #endif
  7496. }
  7497. sumf += d*(sumi1 + sumi2);
  7498. }
  7499. *s = sumf;
  7500. #elif defined(__AVX2__)
  7501. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7502. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7503. };
  7504. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7505. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7506. };
  7507. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7508. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7509. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  7510. const __m256i idx_mask = _mm256_set1_epi32(256);
  7511. typedef union {
  7512. __m256i vec[2];
  7513. uint32_t index[16];
  7514. } index_t;
  7515. index_t idx;
  7516. __m256 accumf = _mm256_setzero_ps();
  7517. for (int i = 0; i < nb; ++i) {
  7518. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7519. const uint8_t * restrict qs = x[i].qs;
  7520. const uint8_t * restrict qh = x[i].qh;
  7521. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7522. const int8_t * restrict q8 = y[i].qs;
  7523. __m256i sumi1 = _mm256_setzero_si256();
  7524. __m256i sumi2 = _mm256_setzero_si256();
  7525. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7526. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7527. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7528. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  7529. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  7530. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  7531. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  7532. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  7533. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  7534. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  7535. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  7536. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  7537. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  7538. const __m256i q2_1 = _mm256_set_epi32(
  7539. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  7540. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  7541. );
  7542. const __m256i q2_2 = _mm256_set_epi32(
  7543. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  7544. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  7545. );
  7546. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7547. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7548. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7549. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7550. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7551. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7552. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7553. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7554. signs += 4;
  7555. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7556. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7557. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  7558. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  7559. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7560. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7561. sumi1 = _mm256_add_epi32(sumi1, p1);
  7562. sumi2 = _mm256_add_epi32(sumi2, p2);
  7563. }
  7564. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7565. }
  7566. *s = hsum_float_8(accumf);
  7567. #else
  7568. float sumf = 0.f;
  7569. for (int i = 0; i < nb; ++i) {
  7570. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7571. const uint8_t * restrict qs = x[i].qs;
  7572. const uint8_t * restrict qh = x[i].qh;
  7573. const uint8_t * restrict signs = x[i].signs;
  7574. const int8_t * restrict q8 = y[i].qs;
  7575. int32_t bsum = 0;
  7576. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7577. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  7578. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  7579. int32_t sumi = 0;
  7580. for (int l = 0; l < 4; ++l) {
  7581. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  7582. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  7583. for (int j = 0; j < 4; ++j) {
  7584. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7585. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7586. }
  7587. q8 += 8;
  7588. }
  7589. qs += 8;
  7590. signs += 4;
  7591. bsum += sumi * ls1;
  7592. sumi = 0;
  7593. for (int l = 0; l < 4; ++l) {
  7594. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  7595. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  7596. for (int j = 0; j < 4; ++j) {
  7597. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7598. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7599. }
  7600. q8 += 8;
  7601. }
  7602. qs += 8;
  7603. signs += 4;
  7604. bsum += sumi * ls2;
  7605. }
  7606. sumf += d * bsum;
  7607. }
  7608. *s = sumf;
  7609. #endif
  7610. }
  7611. #ifdef __AVX2__
  7612. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  7613. const __m256i ax = _mm256_sign_epi8(x, x);
  7614. const __m256i sy = _mm256_sign_epi8(y, x);
  7615. return _mm256_maddubs_epi16(ax, sy);
  7616. }
  7617. #endif
  7618. 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) {
  7619. assert(n % QK_K == 0);
  7620. assert(nrc == 1);
  7621. UNUSED(nrc);
  7622. UNUSED(bx);
  7623. UNUSED(by);
  7624. UNUSED(bs);
  7625. const block_iq1_s * restrict x = vx;
  7626. const block_q8_K * restrict y = vy;
  7627. const int nb = n / QK_K;
  7628. #if defined __ARM_NEON
  7629. ggml_int8x16x4_t q1b;
  7630. ggml_int8x16x4_t q8b;
  7631. float sumf = 0;
  7632. for (int i = 0; i < nb; ++i) {
  7633. const int8_t * q8 = y[i].qs;
  7634. const uint8_t * qs = x[i].qs;
  7635. const uint16_t * qh = x[i].qh;
  7636. int sumi1 = 0, sumi2 = 0, sumi3 = 0;
  7637. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7638. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  7639. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  7640. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  7641. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  7642. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  7643. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  7644. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  7645. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  7646. qs += 8;
  7647. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7648. 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]);
  7649. 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]);
  7650. const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7651. const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7652. sumi1 += vaddvq_s32(p1) * ls1;
  7653. sumi2 += vaddvq_s32(p2) * ls2;
  7654. sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
  7655. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
  7656. }
  7657. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
  7658. }
  7659. *s = sumf;
  7660. #elif defined __AVX2__
  7661. __m256 accum = _mm256_setzero_ps();
  7662. float accum1 = 0;
  7663. for (int i = 0; i < nb; ++i) {
  7664. const int8_t * q8 = y[i].qs;
  7665. const uint8_t * qs = x[i].qs;
  7666. const uint16_t * qh = x[i].qh;
  7667. __m256i sumi = _mm256_setzero_si256();
  7668. int sumi1 = 0;
  7669. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7670. 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)],
  7671. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  7672. 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)],
  7673. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  7674. qs += 8;
  7675. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7676. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7677. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7678. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7679. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7680. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7681. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
  7682. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
  7683. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  7684. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  7685. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  7686. }
  7687. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  7688. accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
  7689. accum1 += d * sumi1;
  7690. }
  7691. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  7692. #else
  7693. float sumf = 0;
  7694. for (int i = 0; i < nb; i++) {
  7695. const int8_t * q8 = y[i].qs;
  7696. const uint8_t * qs = x[i].qs;
  7697. const uint16_t * qh = x[i].qh;
  7698. int sumi = 0, sumi1 = 0;
  7699. for (int ib = 0; ib < QK_K/32; ++ib) {
  7700. const int ls = 2*((qh[ib] >> 12) & 7) + 1;
  7701. const int delta = qh[ib] & 0x8000 ? -1 : 1;
  7702. int lsum = 0;
  7703. for (int l = 0; l < 4; ++l) {
  7704. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  7705. for (int j = 0; j < 8; ++j) {
  7706. lsum += q8[j] * grid[j];
  7707. }
  7708. q8 += 8;
  7709. }
  7710. sumi += ls * lsum;
  7711. sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
  7712. qs += 4;
  7713. }
  7714. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
  7715. }
  7716. *s = sumf;
  7717. #endif
  7718. }
  7719. 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) {
  7720. assert(n % QK_K == 0);
  7721. assert(nrc == 1);
  7722. UNUSED(nrc);
  7723. UNUSED(bx);
  7724. UNUSED(by);
  7725. UNUSED(bs);
  7726. const block_iq1_m * restrict x = vx;
  7727. const block_q8_K * restrict y = vy;
  7728. const int nb = n / QK_K;
  7729. iq1m_scale_t scale;
  7730. #if defined __ARM_NEON
  7731. const int32x4_t mask = vdupq_n_s32(0x7);
  7732. const int32x4_t mone = vdupq_n_s32(1);
  7733. const int32x4_t mzero = vdupq_n_s32(0);
  7734. ggml_int8x16x4_t deltas;
  7735. deltas.val[0] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(+1));
  7736. deltas.val[1] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(+1));
  7737. deltas.val[2] = vcombine_s8(vdup_n_s8(+1), vdup_n_s8(-1));
  7738. deltas.val[3] = vcombine_s8(vdup_n_s8(-1), vdup_n_s8(-1));
  7739. ggml_int8x16x4_t q1b;
  7740. ggml_int8x16x4_t q8b;
  7741. uint32_t aux32;
  7742. const uint8_t * aux8 = (const uint8_t *)&aux32;
  7743. float sumf = 0;
  7744. for (int i = 0; i < nb; ++i) {
  7745. const int8_t * q8 = y[i].qs;
  7746. const uint8_t * qs = x[i].qs;
  7747. const uint8_t * qh = x[i].qh;
  7748. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7749. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7750. int32x4_t sumi1 = mzero;
  7751. int32x4_t sumi2 = mzero;
  7752. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7753. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[0] << 8) & 0x700)))),
  7754. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[0] << 4) & 0x700)))));
  7755. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[1] << 8) & 0x700)))),
  7756. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[1] << 4) & 0x700)))));
  7757. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[2] << 8) & 0x700)))),
  7758. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[2] << 4) & 0x700)))));
  7759. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[3] << 8) & 0x700)))),
  7760. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[3] << 4) & 0x700)))));
  7761. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7762. 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]));
  7763. 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]));
  7764. const int32x4_t p12 = vpaddq_s32(p1, p2);
  7765. const uint32_t * qh32 = (const uint32_t *)qh; // we are 4-byte aligned, so we can do that
  7766. aux32 = ((qh32[0] >> 3) & 0x01010101) | ((qh32[0] >> 6) & 0x02020202);
  7767. 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]));
  7768. 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]));
  7769. const int32x4_t p34 = vpaddq_s32(p3, p4);
  7770. int32x4_t scales_4 = ggml_vld1q_u32(sc[ib/2] >> 0, sc[ib/2] >> 3, sc[ib/2] >> 6, sc[ib/2] >> 9);
  7771. scales_4 = vaddq_s32(vshlq_n_s32(vandq_s32(scales_4, mask), 1), mone);
  7772. sumi1 = vmlaq_s32(sumi1, scales_4, p12);
  7773. sumi2 = vmlaq_s32(sumi2, scales_4, p34);
  7774. qs += 8; qh += 4;
  7775. }
  7776. sumf += y[i].d * GGML_FP16_TO_FP32(scale.f16) * (vaddvq_s32(sumi1) + IQ1M_DELTA * vaddvq_s32(sumi2));
  7777. }
  7778. *s = sumf;
  7779. #elif defined __AVX2__
  7780. const __m256i mask = _mm256_set1_epi16(0x7);
  7781. const __m256i mone = _mm256_set1_epi16(1);
  7782. __m256 accum1 = _mm256_setzero_ps();
  7783. __m256 accum2 = _mm256_setzero_ps();
  7784. for (int i = 0; i < nb; ++i) {
  7785. const int8_t * q8 = y[i].qs;
  7786. const uint8_t * qs = x[i].qs;
  7787. const uint8_t * qh = x[i].qh;
  7788. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7789. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7790. __m256i sumi1 = _mm256_setzero_si256();
  7791. __m256i sumi2 = _mm256_setzero_si256();
  7792. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7793. const __m256i q1b_1 = _mm256_set_epi64x(
  7794. iq1s_grid[qs[3] | (((uint16_t)qh[1] << 4) & 0x700)], iq1s_grid[qs[2] | (((uint16_t)qh[1] << 8) & 0x700)],
  7795. iq1s_grid[qs[1] | (((uint16_t)qh[0] << 4) & 0x700)], iq1s_grid[qs[0] | (((uint16_t)qh[0] << 8) & 0x700)]
  7796. );
  7797. const __m256i q1b_2 = _mm256_set_epi64x(
  7798. iq1s_grid[qs[7] | (((uint16_t)qh[3] << 4) & 0x700)], iq1s_grid[qs[6] | (((uint16_t)qh[3] << 8) & 0x700)],
  7799. iq1s_grid[qs[5] | (((uint16_t)qh[2] << 4) & 0x700)], iq1s_grid[qs[4] | (((uint16_t)qh[2] << 8) & 0x700)]
  7800. );
  7801. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7802. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7803. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7804. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7805. const __m256i delta1 = _mm256_set_epi64x(qh[1] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7806. qh[1] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7807. qh[0] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7808. qh[0] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7809. const __m256i delta2 = _mm256_set_epi64x(qh[3] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7810. qh[3] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101,
  7811. qh[2] & 0x80 ? 0xffffffffffffffff : 0x0101010101010101,
  7812. qh[2] & 0x08 ? 0xffffffffffffffff : 0x0101010101010101);
  7813. const __m256i dot3 = mul_add_epi8(delta1, q8b_1);
  7814. const __m256i dot4 = mul_add_epi8(delta2, q8b_2);
  7815. __m256i scale1 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 3), _mm_set1_epi16(sc[ib/2] >> 0));
  7816. __m256i scale2 = MM256_SET_M128I(_mm_set1_epi16(sc[ib/2] >> 9), _mm_set1_epi16(sc[ib/2] >> 6));
  7817. scale1 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale1, mask), 1), mone);
  7818. scale2 = _mm256_add_epi16(_mm256_slli_epi16(_mm256_and_si256(scale2, mask), 1), mone);
  7819. const __m256i p1 = _mm256_madd_epi16(dot1, scale1);
  7820. const __m256i p2 = _mm256_madd_epi16(dot2, scale2);
  7821. const __m256i p3 = _mm256_madd_epi16(dot3, scale1);
  7822. const __m256i p4 = _mm256_madd_epi16(dot4, scale2);
  7823. sumi1 = _mm256_add_epi32(sumi1, _mm256_add_epi32(p1, p2));
  7824. sumi2 = _mm256_add_epi32(sumi2, _mm256_add_epi32(p3, p4));
  7825. qs += 8; qh += 4;
  7826. }
  7827. const __m256 d = _mm256_set1_ps(y[i].d * GGML_FP16_TO_FP32(scale.f16));
  7828. accum1 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi1), accum1);
  7829. accum2 = _mm256_fmadd_ps(d, _mm256_cvtepi32_ps(sumi2), accum2);
  7830. }
  7831. *s = hsum_float_8(accum1) + IQ1M_DELTA * hsum_float_8(accum2);
  7832. #else
  7833. int sum1[2], sum2[2], delta[4];
  7834. float sumf = 0;
  7835. for (int i = 0; i < nb; i++) {
  7836. const int8_t * q8 = y[i].qs;
  7837. const uint8_t * qs = x[i].qs;
  7838. const uint8_t * qh = x[i].qh;
  7839. const uint16_t * sc = (const uint16_t *)x[i].scales;
  7840. scale.u16 = (sc[0] >> 12) | ((sc[1] >> 8) & 0x00f0) | ((sc[2] >> 4) & 0x0f00) | (sc[3] & 0xf000);
  7841. int sumi1 = 0, sumi2 = 0;
  7842. for (int ib = 0; ib < QK_K/32; ++ib) {
  7843. delta[0] = qh[0] & 0x08 ? -1 : 1;
  7844. delta[1] = qh[0] & 0x80 ? -1 : 1;
  7845. delta[2] = qh[1] & 0x08 ? -1 : 1;
  7846. delta[3] = qh[1] & 0x80 ? -1 : 1;
  7847. sum1[0] = sum1[1] = sum2[0] = sum2[1] = 0;
  7848. for (int l = 0; l < 4; ++l) {
  7849. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((uint16_t)qh[l/2] << (8 - 4*(l%2))) & 0x700)));
  7850. int lsum1 = 0, lsum2 = 0;
  7851. for (int j = 0; j < 8; ++j) {
  7852. lsum1 += q8[j] * grid[j];
  7853. lsum2 += q8[j];
  7854. }
  7855. q8 += 8;
  7856. sum1[l/2] += lsum1;
  7857. sum2[l/2] += lsum2*delta[l];
  7858. }
  7859. const int ls1 = 2*((sc[ib/2] >> (6*(ib%2)+0)) & 0x7) + 1;
  7860. const int ls2 = 2*((sc[ib/2] >> (6*(ib%2)+3)) & 0x7) + 1;
  7861. sumi1 += sum1[0] * ls1 + sum1[1] * ls2;
  7862. sumi2 += sum2[0] * ls1 + sum2[1] * ls2;
  7863. qs += 4;
  7864. qh += 2;
  7865. }
  7866. sumf += GGML_FP16_TO_FP32(scale.f16) * y[i].d * (sumi1 + IQ1M_DELTA * sumi2);
  7867. }
  7868. *s = sumf;
  7869. #endif
  7870. }
  7871. 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) {
  7872. assert(nrc == 1);
  7873. UNUSED(nrc);
  7874. UNUSED(bx);
  7875. UNUSED(by);
  7876. UNUSED(bs);
  7877. assert(n % QK4_NL == 0);
  7878. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  7879. const block_iq4_nl * restrict x = vx;
  7880. const block_q8_0 * restrict y = vy;
  7881. const int nb = n / QK4_NL;
  7882. #if defined __ARM_NEON
  7883. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7884. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7885. uint8x16x2_t q4bits;
  7886. int8x16x4_t q4b;
  7887. int8x16x4_t q8b;
  7888. int32x4_t prod_1, prod_2;
  7889. float sumf = 0;
  7890. for (int ib = 0; ib < nb; ib += 2) {
  7891. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  7892. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  7893. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  7894. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  7895. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  7896. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  7897. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7898. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7899. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7900. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7901. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7902. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7903. sumf +=
  7904. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  7905. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  7906. }
  7907. *s = sumf;
  7908. #elif defined __AVX2__
  7909. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7910. const __m128i m4b = _mm_set1_epi8(0x0f);
  7911. const __m256i mone = _mm256_set1_epi16(1);
  7912. __m256 accum1 = _mm256_setzero_ps();
  7913. __m256 accum2 = _mm256_setzero_ps();
  7914. for (int ib = 0; ib < nb; ib += 2) {
  7915. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  7916. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  7917. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  7918. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  7919. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7920. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7921. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7922. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7923. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7924. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7925. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  7926. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  7927. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  7928. _mm256_cvtepi32_ps(p_1), accum1);
  7929. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  7930. _mm256_cvtepi32_ps(p_2), accum2);
  7931. y += 2;
  7932. x += 2;
  7933. }
  7934. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  7935. #else
  7936. float sumf = 0;
  7937. for (int ib = 0; ib < nb; ++ib) {
  7938. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  7939. int sumi1 = 0, sumi2 = 0;
  7940. for (int j = 0; j < QK4_NL/2; ++j) {
  7941. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  7942. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  7943. }
  7944. sumf += d * (sumi1 + sumi2);
  7945. }
  7946. *s = sumf;
  7947. #endif
  7948. }
  7949. 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) {
  7950. assert(nrc == 1);
  7951. UNUSED(nrc);
  7952. UNUSED(bx);
  7953. UNUSED(by);
  7954. UNUSED(bs);
  7955. assert(n % QK_K == 0);
  7956. #if QK_K == 64
  7957. ggml_vec_dot_iq4_nl_q8_0(n, s, bs, vx, bx, vy, by, nrc);
  7958. #else
  7959. const block_iq4_xs * restrict x = vx;
  7960. const block_q8_K * restrict y = vy;
  7961. const int nb = n / QK_K;
  7962. #if defined __ARM_NEON
  7963. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7964. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7965. ggml_uint8x16x2_t q4bits;
  7966. ggml_int8x16x4_t q4b;
  7967. ggml_int8x16x4_t q8b;
  7968. int32x4_t prod_1, prod_2;
  7969. float sumf = 0;
  7970. for (int ibl = 0; ibl < nb; ++ibl) {
  7971. const int8_t * q8 = y[ibl].qs;
  7972. const uint8_t * q4 = x[ibl].qs;
  7973. uint16_t h = x[ibl].scales_h;
  7974. int sumi1 = 0, sumi2 = 0;
  7975. for (int ib = 0; ib < QK_K/64; ++ib) {
  7976. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  7977. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7978. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7979. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7980. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7981. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7982. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7983. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7984. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  7985. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  7986. h >>= 4;
  7987. sumi1 += vaddvq_s32(prod_1) * ls1;
  7988. sumi2 += vaddvq_s32(prod_2) * ls2;
  7989. }
  7990. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  7991. }
  7992. *s = sumf;
  7993. #elif defined __AVX2__
  7994. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7995. const __m128i m4b = _mm_set1_epi8(0x0f);
  7996. __m256 accum = _mm256_setzero_ps();
  7997. for (int ibl = 0; ibl < nb; ++ibl) {
  7998. const uint8_t * qs = x[ibl].qs;
  7999. const int8_t * q8 = y[ibl].qs;
  8000. uint16_t sh = x[ibl].scales_h;
  8001. __m256i sumi1 = _mm256_setzero_si256();
  8002. __m256i sumi2 = _mm256_setzero_si256();
  8003. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8004. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  8005. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  8006. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  8007. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  8008. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  8009. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  8010. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  8011. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  8012. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  8013. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  8014. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  8015. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  8016. sh >>= 4;
  8017. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  8018. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  8019. sumi1 = _mm256_add_epi32(p_1, sumi1);
  8020. sumi2 = _mm256_add_epi32(p_2, sumi2);
  8021. }
  8022. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  8023. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  8024. }
  8025. *s = hsum_float_8(accum);
  8026. #else
  8027. float sumf = 0;
  8028. for (int ibl = 0; ibl < nb; ++ibl) {
  8029. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  8030. uint16_t h = x[ibl].scales_h;
  8031. const uint8_t * qs = x[ibl].qs;
  8032. const int8_t * q8 = y[ibl].qs;
  8033. for (int ib = 0; ib < QK_K/32; ib += 2) {
  8034. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  8035. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  8036. h >>= 4;
  8037. const float d1 = d4d8*(ls1 - 32);
  8038. const float d2 = d4d8*(ls2 - 32);
  8039. int sumi1 = 0, sumi2 = 0;
  8040. for (int j = 0; j < 16; ++j) {
  8041. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  8042. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  8043. }
  8044. sumf += d1 * (sumi1 + sumi2);
  8045. qs += 16;
  8046. q8 += 32;
  8047. sumi1 = sumi2 = 0;
  8048. for (int j = 0; j < 16; ++j) {
  8049. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  8050. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  8051. }
  8052. sumf += d2 * (sumi1 + sumi2);
  8053. qs += 16;
  8054. q8 += 32;
  8055. }
  8056. }
  8057. *s = sumf;
  8058. #endif
  8059. #endif
  8060. }
  8061. // ================================ IQ2 quantization =============================================
  8062. typedef struct {
  8063. uint64_t * grid;
  8064. int * map;
  8065. uint16_t * neighbours;
  8066. } iq2_entry_t;
  8067. static iq2_entry_t iq2_data[4] = {
  8068. {NULL, NULL, NULL},
  8069. {NULL, NULL, NULL},
  8070. {NULL, NULL, NULL},
  8071. {NULL, NULL, NULL},
  8072. };
  8073. static inline int iq2_data_index(enum ggml_type type) {
  8074. 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);
  8075. return type == GGML_TYPE_IQ2_XXS ? 0 :
  8076. type == GGML_TYPE_IQ2_XS ? 1 :
  8077. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 2 : 3;
  8078. }
  8079. static inline int iq2_grid_size(enum ggml_type type) {
  8080. 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);
  8081. return type == GGML_TYPE_IQ2_XXS ? 256 :
  8082. type == GGML_TYPE_IQ2_XS ? 512 :
  8083. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? NGRID_IQ1S : 1024;
  8084. }
  8085. static int iq2_compare_func(const void * left, const void * right) {
  8086. const int * l = (const int *)left;
  8087. const int * r = (const int *)right;
  8088. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8089. }
  8090. void iq2xs_init_impl(enum ggml_type type) {
  8091. const int gindex = iq2_data_index(type);
  8092. const int grid_size = iq2_grid_size(type);
  8093. if (iq2_data[gindex].grid) {
  8094. return;
  8095. }
  8096. static const uint16_t kgrid_2bit_256[256] = {
  8097. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  8098. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  8099. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  8100. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  8101. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  8102. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  8103. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  8104. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  8105. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  8106. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  8107. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  8108. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  8109. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  8110. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  8111. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  8112. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  8113. };
  8114. static const uint16_t kgrid_2bit_512[512] = {
  8115. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8116. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  8117. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  8118. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  8119. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  8120. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  8121. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  8122. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  8123. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  8124. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  8125. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  8126. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  8127. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  8128. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  8129. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  8130. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  8131. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  8132. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  8133. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  8134. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  8135. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  8136. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  8137. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  8138. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  8139. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  8140. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  8141. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  8142. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  8143. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  8144. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  8145. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  8146. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  8147. };
  8148. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  8149. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  8150. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  8151. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  8152. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  8153. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  8154. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  8155. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  8156. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  8157. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  8158. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  8159. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  8160. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  8161. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  8162. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  8163. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  8164. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  8165. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  8166. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  8167. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  8168. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  8169. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  8170. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  8171. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  8172. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  8173. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  8174. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  8175. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  8176. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  8177. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  8178. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  8179. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  8180. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  8181. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  8182. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  8183. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  8184. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  8185. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  8186. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  8187. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  8188. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  8189. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  8190. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  8191. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  8192. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  8193. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  8194. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  8195. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  8196. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  8197. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  8198. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  8199. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  8200. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  8201. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  8202. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  8203. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  8204. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  8205. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  8206. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  8207. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  8208. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  8209. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  8210. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  8211. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  8212. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  8213. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  8214. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  8215. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  8216. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  8217. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  8218. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  8219. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  8220. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  8221. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  8222. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  8223. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  8224. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  8225. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  8226. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  8227. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  8228. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  8229. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  8230. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  8231. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  8232. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  8233. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  8234. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  8235. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  8236. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  8237. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  8238. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  8239. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  8240. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  8241. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  8242. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  8243. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  8244. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  8245. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  8246. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  8247. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  8248. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  8249. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  8250. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  8251. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  8252. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  8253. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  8254. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  8255. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  8256. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  8257. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  8258. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  8259. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  8260. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  8261. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  8262. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  8263. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  8264. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  8265. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  8266. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  8267. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  8268. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  8269. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  8270. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  8271. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  8272. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  8273. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  8274. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  8275. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  8276. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  8277. };
  8278. static const uint16_t kgrid_2bit_1024[1024] = {
  8279. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8280. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  8281. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  8282. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  8283. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  8284. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  8285. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  8286. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  8287. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  8288. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  8289. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  8290. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  8291. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  8292. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  8293. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  8294. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  8295. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  8296. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  8297. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  8298. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  8299. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  8300. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  8301. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  8302. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  8303. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  8304. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  8305. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  8306. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  8307. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  8308. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  8309. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  8310. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  8311. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  8312. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  8313. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  8314. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  8315. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  8316. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  8317. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  8318. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  8319. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  8320. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  8321. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  8322. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  8323. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  8324. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  8325. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  8326. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  8327. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  8328. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  8329. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  8330. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  8331. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  8332. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  8333. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  8334. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  8335. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  8336. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  8337. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  8338. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  8339. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  8340. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  8341. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  8342. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  8343. };
  8344. const int kmap_size = 43692;
  8345. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  8346. const int nwant = type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  8347. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  8348. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  8349. type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ1_M ? kgrid_1bit_2048 : kgrid_2bit_1024;
  8350. uint64_t * kgrid_q2xs;
  8351. int * kmap_q2xs;
  8352. uint16_t * kneighbors_q2xs;
  8353. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8354. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  8355. for (int k = 0; k < grid_size; ++k) {
  8356. int8_t * pos = (int8_t *)(the_grid + k);
  8357. for (int i = 0; i < 8; ++i) {
  8358. int l = (kgrid[k] >> 2*i) & 0x3;
  8359. pos[i] = 2*l + 1;
  8360. }
  8361. }
  8362. kgrid_q2xs = the_grid;
  8363. iq2_data[gindex].grid = the_grid;
  8364. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  8365. iq2_data[gindex].map = kmap_q2xs;
  8366. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  8367. uint64_t aux64;
  8368. uint8_t * aux8 = (uint8_t *)&aux64;
  8369. for (int i = 0; i < grid_size; ++i) {
  8370. aux64 = kgrid_q2xs[i];
  8371. uint16_t index = 0;
  8372. for (int k=0; k<8; ++k) {
  8373. uint16_t q = (aux8[k] - 1)/2;
  8374. index |= (q << 2*k);
  8375. }
  8376. kmap_q2xs[index] = i;
  8377. }
  8378. int8_t pos[8];
  8379. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8380. int num_neighbors = 0, num_not_in_map = 0;
  8381. for (int i = 0; i < kmap_size; ++i) {
  8382. if (kmap_q2xs[i] >= 0) continue;
  8383. ++num_not_in_map;
  8384. for (int k = 0; k < 8; ++k) {
  8385. int l = (i >> 2*k) & 0x3;
  8386. pos[k] = 2*l + 1;
  8387. }
  8388. for (int j = 0; j < grid_size; ++j) {
  8389. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8390. int d2 = 0;
  8391. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8392. dist2[2*j+0] = d2;
  8393. dist2[2*j+1] = j;
  8394. }
  8395. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8396. int n = 0; int d2 = dist2[0];
  8397. int nhave = 1;
  8398. for (int j = 0; j < grid_size; ++j) {
  8399. if (dist2[2*j] > d2) {
  8400. if (nhave == nwant) break;
  8401. d2 = dist2[2*j];
  8402. ++nhave;
  8403. }
  8404. ++n;
  8405. }
  8406. num_neighbors += n;
  8407. }
  8408. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8409. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8410. iq2_data[gindex].neighbours = kneighbors_q2xs;
  8411. int counter = 0;
  8412. for (int i = 0; i < kmap_size; ++i) {
  8413. if (kmap_q2xs[i] >= 0) continue;
  8414. for (int k = 0; k < 8; ++k) {
  8415. int l = (i >> 2*k) & 0x3;
  8416. pos[k] = 2*l + 1;
  8417. }
  8418. for (int j = 0; j < grid_size; ++j) {
  8419. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8420. int d2 = 0;
  8421. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8422. dist2[2*j+0] = d2;
  8423. dist2[2*j+1] = j;
  8424. }
  8425. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8426. kmap_q2xs[i] = -(counter + 1);
  8427. int d2 = dist2[0];
  8428. uint16_t * start = &kneighbors_q2xs[counter++];
  8429. int n = 0, nhave = 1;
  8430. for (int j = 0; j < grid_size; ++j) {
  8431. if (dist2[2*j] > d2) {
  8432. if (nhave == nwant) break;
  8433. d2 = dist2[2*j];
  8434. ++nhave;
  8435. }
  8436. kneighbors_q2xs[counter++] = dist2[2*j+1];
  8437. ++n;
  8438. }
  8439. *start = n;
  8440. }
  8441. free(dist2);
  8442. }
  8443. void iq2xs_free_impl(enum ggml_type type) {
  8444. 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);
  8445. const int gindex = iq2_data_index(type);
  8446. if (iq2_data[gindex].grid) {
  8447. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  8448. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  8449. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  8450. }
  8451. }
  8452. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  8453. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8454. int num_neighbors = neighbours[0];
  8455. GGML_ASSERT(num_neighbors > 0);
  8456. float best_d2 = FLT_MAX;
  8457. int grid_index = -1;
  8458. for (int j = 1; j <= num_neighbors; ++j) {
  8459. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8460. float d2 = 0;
  8461. for (int i = 0; i < 8; ++i) {
  8462. float q = pg[i];
  8463. float diff = scale*q - xval[i];
  8464. d2 += weight[i]*diff*diff;
  8465. }
  8466. if (d2 < best_d2) {
  8467. best_d2 = d2; grid_index = neighbours[j];
  8468. }
  8469. }
  8470. GGML_ASSERT(grid_index >= 0);
  8471. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8472. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  8473. return grid_index;
  8474. }
  8475. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  8476. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  8477. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8478. const int * kmap_q2xs = iq2_data[gindex].map;
  8479. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8480. GGML_ASSERT(quant_weights && "missing quantization weights");
  8481. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8482. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8483. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8484. GGML_ASSERT(n%QK_K == 0);
  8485. const int kMaxQ = 3;
  8486. const int nbl = n/QK_K;
  8487. block_iq2_xxs * y = vy;
  8488. float scales[QK_K/32];
  8489. float weight[32];
  8490. float xval[32];
  8491. int8_t L[32];
  8492. int8_t Laux[32];
  8493. float waux[32];
  8494. uint8_t block_signs[4];
  8495. uint32_t q2[2*(QK_K/32)];
  8496. for (int ibl = 0; ibl < nbl; ++ibl) {
  8497. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8498. memset(q2, 0, QK_K/4);
  8499. float max_scale = 0;
  8500. const float * xbl = x + QK_K*ibl;
  8501. float sumx2 = 0;
  8502. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8503. float sigma2 = sumx2/QK_K;
  8504. for (int ib = 0; ib < QK_K/32; ++ib) {
  8505. const float * xb = xbl + 32*ib;
  8506. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8507. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8508. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8509. for (int k = 0; k < 4; ++k) {
  8510. int nflip = 0;
  8511. uint8_t s = 0;
  8512. for (int i = 0; i < 8; ++i) {
  8513. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8514. else {
  8515. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8516. }
  8517. }
  8518. if (nflip%2) {
  8519. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8520. for (int i = 1; i < 8; ++i) {
  8521. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8522. if (ax < min) {
  8523. min = ax; imin = i;
  8524. }
  8525. }
  8526. xval[8*k+imin] = -xval[8*k+imin];
  8527. s ^= (1 << imin);
  8528. }
  8529. block_signs[k] = s & 127;
  8530. }
  8531. float max = xval[0];
  8532. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8533. if (!max) {
  8534. scales[ib] = 0;
  8535. memset(L, 0, 32);
  8536. continue;
  8537. }
  8538. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  8539. float eff_max = scale*kMaxQ;
  8540. float best = 0;
  8541. for (int is = -6; is <= 6; ++is) {
  8542. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  8543. float this_scale = 1/id;
  8544. for (int k = 0; k < 4; ++k) {
  8545. for (int i = 0; i < 8; ++i) {
  8546. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8547. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8548. }
  8549. uint16_t u = 0;
  8550. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8551. int grid_index = kmap_q2xs[u];
  8552. if (grid_index < 0) {
  8553. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8554. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8555. }
  8556. }
  8557. float sumqx = 0, sumq2 = 0;
  8558. for (int i = 0; i < 32; ++i) {
  8559. float w = weight[i];
  8560. float q = 2*Laux[i] + 1;
  8561. sumqx += w*xval[i]*q;
  8562. sumq2 += w*q*q;
  8563. }
  8564. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8565. scale = sumqx/sumq2; best = scale*sumqx;
  8566. memcpy(L, Laux, 32);
  8567. }
  8568. }
  8569. if (scale > 0) {
  8570. float id = 1/scale;
  8571. for (int k = 0; k < 4; ++k) {
  8572. uint16_t u = 0;
  8573. for (int i = 0; i < 8; ++i) {
  8574. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8575. l = MAX(0, MIN(kMaxQ-1, l));
  8576. u |= (l << 2*i);
  8577. }
  8578. int grid_index = kmap_q2xs[u];
  8579. if (grid_index < 0) {
  8580. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8581. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8582. }
  8583. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  8584. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  8585. }
  8586. float sumqx = 0, sumq2 = 0;
  8587. for (int i = 0; i < 32; ++i) {
  8588. float w = weight[i];
  8589. float q = 2*L[i] + 1;
  8590. sumqx += w*xval[i]*q;
  8591. sumq2 += w*q*q;
  8592. }
  8593. if (sumq2 > 0) scale = sumqx/sumq2;
  8594. }
  8595. if (scale < 0) {
  8596. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8597. // and correspondingly flip quant signs.
  8598. scale = -scale;
  8599. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8600. }
  8601. for (int k = 0; k < 4; ++k) {
  8602. uint16_t u = 0;
  8603. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8604. int grid_index = kmap_q2xs[u];
  8605. if (grid_index < 0) {
  8606. printf("Oops: found point %u not on grid:", u);
  8607. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8608. printf("\n");
  8609. GGML_ASSERT(false);
  8610. }
  8611. q2[2*ib+0] |= (grid_index << 8*k);
  8612. q2[2*ib+1] |= (block_signs[k] << 7*k);
  8613. }
  8614. GGML_ASSERT(scale >= 0);
  8615. scales[ib] = scale;
  8616. max_scale = MAX(max_scale, scale);
  8617. }
  8618. if (!max_scale) {
  8619. memset(y[ibl].qs, 0, QK_K/4);
  8620. continue;
  8621. }
  8622. float d = max_scale/31;
  8623. y[ibl].d = GGML_FP32_TO_FP16(d);
  8624. float id = 1/d;
  8625. for (int ib = 0; ib < QK_K/32; ++ib) {
  8626. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8627. l = MAX(0, MIN(15, l));
  8628. q2[2*ib+1] |= ((uint32_t)l << 28);
  8629. }
  8630. memcpy(y[ibl].qs, q2, QK_K/4);
  8631. }
  8632. }
  8633. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  8634. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  8635. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8636. const int * kmap_q2xs = iq2_data[gindex].map;
  8637. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8638. GGML_ASSERT(quant_weights && "missing quantization weights");
  8639. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8640. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8641. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8642. GGML_ASSERT(n%QK_K == 0);
  8643. const int kMaxQ = 3;
  8644. const int nbl = n/QK_K;
  8645. block_iq2_xs * y = vy;
  8646. float scales[QK_K/16];
  8647. float weight[16];
  8648. float xval[16];
  8649. int8_t L[16];
  8650. int8_t Laux[16];
  8651. float waux[16];
  8652. bool is_on_grid[2];
  8653. bool is_on_grid_aux[2];
  8654. uint8_t block_signs[2];
  8655. uint16_t q2[2*(QK_K/16)];
  8656. for (int ibl = 0; ibl < nbl; ++ibl) {
  8657. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8658. memset(q2, 0, QK_K/4);
  8659. memset(y[ibl].scales, 0, QK_K/32);
  8660. float max_scale = 0;
  8661. const float * xbl = x + QK_K*ibl;
  8662. float sumx2 = 0;
  8663. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8664. float sigma2 = sumx2/QK_K;
  8665. for (int ib = 0; ib < QK_K/16; ++ib) {
  8666. const float * xb = xbl + 16*ib;
  8667. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  8668. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8669. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  8670. for (int k = 0; k < 2; ++k) {
  8671. int nflip = 0;
  8672. uint8_t s = 0;
  8673. for (int i = 0; i < 8; ++i) {
  8674. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8675. else {
  8676. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8677. }
  8678. }
  8679. if (nflip%2) {
  8680. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8681. for (int i = 1; i < 8; ++i) {
  8682. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8683. if (ax < min) {
  8684. min = ax; imin = i;
  8685. }
  8686. }
  8687. xval[8*k+imin] = -xval[8*k+imin];
  8688. s ^= (1 << imin);
  8689. }
  8690. block_signs[k] = s & 127;
  8691. }
  8692. float max = xval[0];
  8693. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  8694. if (!max) {
  8695. scales[ib] = 0;
  8696. memset(L, 0, 16);
  8697. continue;
  8698. }
  8699. float best = 0;
  8700. float scale = max/(2*kMaxQ-1);
  8701. is_on_grid[0] = is_on_grid[1] = true;
  8702. for (int is = -9; is <= 9; ++is) {
  8703. float id = (2*kMaxQ-1+is*0.1f)/max;
  8704. float this_scale = 1/id;
  8705. for (int k = 0; k < 2; ++k) {
  8706. for (int i = 0; i < 8; ++i) {
  8707. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8708. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8709. }
  8710. uint16_t u = 0;
  8711. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8712. int grid_index = kmap_q2xs[u];
  8713. is_on_grid_aux[k] = true;
  8714. if (grid_index < 0) {
  8715. is_on_grid_aux[k] = false;
  8716. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8717. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8718. }
  8719. }
  8720. float sumqx = 0, sumq2 = 0;
  8721. for (int i = 0; i < 16; ++i) {
  8722. float w = weight[i];
  8723. float q = 2*Laux[i] + 1;
  8724. sumqx += w*xval[i]*q;
  8725. sumq2 += w*q*q;
  8726. }
  8727. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8728. scale = sumqx/sumq2; best = scale*sumqx;
  8729. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  8730. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8731. }
  8732. }
  8733. int n_not_ongrid = 0;
  8734. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8735. if (n_not_ongrid > 0 && scale > 0) {
  8736. float id = 1/scale;
  8737. for (int k = 0; k < 2; ++k) {
  8738. if (is_on_grid[k]) continue;
  8739. uint16_t u = 0;
  8740. for (int i = 0; i < 8; ++i) {
  8741. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8742. l = MAX(0, MIN(kMaxQ-1, l));
  8743. u |= (l << 2*i);
  8744. L[8*k + i] = l;
  8745. }
  8746. int grid_index = kmap_q2xs[u];
  8747. if (grid_index < 0) {
  8748. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8749. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8750. }
  8751. }
  8752. float sumqx = 0, sumq2 = 0;
  8753. for (int i = 0; i < 16; ++i) {
  8754. float w = weight[i];
  8755. float q = 2*L[i] + 1;
  8756. sumqx += w*xval[i]*q;
  8757. sumq2 += w*q*q;
  8758. }
  8759. if (sumq2 > 0) scale = sumqx/sumq2;
  8760. }
  8761. if (scale < 0) {
  8762. scale = -scale;
  8763. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8764. }
  8765. for (int k = 0; k < 2; ++k) {
  8766. uint16_t u = 0;
  8767. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8768. int grid_index = kmap_q2xs[u];
  8769. if (grid_index < 0) {
  8770. printf("Oops: found point %u not on grid:", u);
  8771. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8772. printf("\n");
  8773. GGML_ASSERT(false);
  8774. }
  8775. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  8776. }
  8777. GGML_ASSERT(scale >= 0);
  8778. scales[ib] = scale;
  8779. max_scale = MAX(max_scale, scale);
  8780. }
  8781. if (!max_scale) {
  8782. memset(y[ibl].qs, 0, QK_K/4);
  8783. continue;
  8784. }
  8785. float d = max_scale/31;
  8786. y[ibl].d = GGML_FP32_TO_FP16(d);
  8787. float id = 1/d;
  8788. for (int ib = 0; ib < QK_K/16; ++ib) {
  8789. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8790. l = MAX(0, MIN(15, l));
  8791. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  8792. else y[ibl].scales[ib/2] |= (l << 4);
  8793. }
  8794. memcpy(y[ibl].qs, q2, QK_K/4);
  8795. }
  8796. }
  8797. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  8798. GGML_ASSERT(n_per_row%QK_K == 0);
  8799. int nblock = n_per_row/QK_K;
  8800. char * qrow = (char *)dst;
  8801. for (int row = 0; row < nrow; ++row) {
  8802. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  8803. src += n_per_row;
  8804. qrow += nblock*sizeof(block_iq2_xxs);
  8805. }
  8806. return nrow * nblock * sizeof(block_iq2_xxs);
  8807. }
  8808. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  8809. GGML_ASSERT(n_per_row%QK_K == 0);
  8810. int nblock = n_per_row/QK_K;
  8811. char * qrow = (char *)dst;
  8812. for (int row = 0; row < nrow; ++row) {
  8813. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  8814. src += n_per_row;
  8815. qrow += nblock*sizeof(block_iq2_xs);
  8816. }
  8817. return nrow * nblock * sizeof(block_iq2_xs);
  8818. }
  8819. //
  8820. // ============================================= 3-bit using D4 lattice
  8821. //
  8822. typedef struct {
  8823. uint32_t * grid;
  8824. int * map;
  8825. uint16_t * neighbours;
  8826. } iq3_entry_t;
  8827. static iq3_entry_t iq3_data[2] = {
  8828. {NULL, NULL, NULL},
  8829. {NULL, NULL, NULL},
  8830. };
  8831. static inline int iq3_data_index(int grid_size) {
  8832. (void)grid_size;
  8833. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8834. return grid_size == 256 ? 0 : 1;
  8835. }
  8836. static int iq3_compare_func(const void * left, const void * right) {
  8837. const int * l = (const int *)left;
  8838. const int * r = (const int *)right;
  8839. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8840. }
  8841. void iq3xs_init_impl(int grid_size) {
  8842. const int gindex = iq3_data_index(grid_size);
  8843. if (iq3_data[gindex].grid) {
  8844. return;
  8845. }
  8846. static const uint16_t kgrid_256[256] = {
  8847. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  8848. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  8849. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  8850. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  8851. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  8852. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  8853. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  8854. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  8855. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  8856. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  8857. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  8858. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  8859. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  8860. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  8861. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  8862. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  8863. };
  8864. static const uint16_t kgrid_512[512] = {
  8865. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  8866. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  8867. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  8868. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  8869. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  8870. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  8871. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  8872. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  8873. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  8874. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  8875. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  8876. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  8877. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  8878. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  8879. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  8880. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  8881. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  8882. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  8883. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  8884. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  8885. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  8886. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  8887. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  8888. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  8889. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  8890. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  8891. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  8892. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  8893. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  8894. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  8895. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  8896. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  8897. };
  8898. const int kmap_size = 4096;
  8899. const int nwant = grid_size == 256 ? 2 : 3;
  8900. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  8901. uint32_t * kgrid_q3xs;
  8902. int * kmap_q3xs;
  8903. uint16_t * kneighbors_q3xs;
  8904. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8905. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  8906. for (int k = 0; k < grid_size; ++k) {
  8907. int8_t * pos = (int8_t *)(the_grid + k);
  8908. for (int i = 0; i < 4; ++i) {
  8909. int l = (kgrid[k] >> 3*i) & 0x7;
  8910. pos[i] = 2*l + 1;
  8911. }
  8912. }
  8913. kgrid_q3xs = the_grid;
  8914. iq3_data[gindex].grid = the_grid;
  8915. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  8916. iq3_data[gindex].map = kmap_q3xs;
  8917. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  8918. uint32_t aux32;
  8919. uint8_t * aux8 = (uint8_t *)&aux32;
  8920. for (int i = 0; i < grid_size; ++i) {
  8921. aux32 = kgrid_q3xs[i];
  8922. uint16_t index = 0;
  8923. for (int k=0; k<4; ++k) {
  8924. uint16_t q = (aux8[k] - 1)/2;
  8925. index |= (q << 3*k);
  8926. }
  8927. kmap_q3xs[index] = i;
  8928. }
  8929. int8_t pos[4];
  8930. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8931. int num_neighbors = 0, num_not_in_map = 0;
  8932. for (int i = 0; i < kmap_size; ++i) {
  8933. if (kmap_q3xs[i] >= 0) continue;
  8934. ++num_not_in_map;
  8935. for (int k = 0; k < 4; ++k) {
  8936. int l = (i >> 3*k) & 0x7;
  8937. pos[k] = 2*l + 1;
  8938. }
  8939. for (int j = 0; j < grid_size; ++j) {
  8940. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8941. int d2 = 0;
  8942. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8943. dist2[2*j+0] = d2;
  8944. dist2[2*j+1] = j;
  8945. }
  8946. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8947. int n = 0; int d2 = dist2[0];
  8948. int nhave = 1;
  8949. for (int j = 0; j < grid_size; ++j) {
  8950. if (dist2[2*j] > d2) {
  8951. if (nhave == nwant) break;
  8952. d2 = dist2[2*j];
  8953. ++nhave;
  8954. }
  8955. ++n;
  8956. }
  8957. num_neighbors += n;
  8958. }
  8959. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8960. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8961. iq3_data[gindex].neighbours = kneighbors_q3xs;
  8962. int counter = 0;
  8963. for (int i = 0; i < kmap_size; ++i) {
  8964. if (kmap_q3xs[i] >= 0) continue;
  8965. for (int k = 0; k < 4; ++k) {
  8966. int l = (i >> 3*k) & 0x7;
  8967. pos[k] = 2*l + 1;
  8968. }
  8969. for (int j = 0; j < grid_size; ++j) {
  8970. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8971. int d2 = 0;
  8972. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8973. dist2[2*j+0] = d2;
  8974. dist2[2*j+1] = j;
  8975. }
  8976. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8977. kmap_q3xs[i] = -(counter + 1);
  8978. int d2 = dist2[0];
  8979. uint16_t * start = &kneighbors_q3xs[counter++];
  8980. int n = 0, nhave = 1;
  8981. for (int j = 0; j < grid_size; ++j) {
  8982. if (dist2[2*j] > d2) {
  8983. if (nhave == nwant) break;
  8984. d2 = dist2[2*j];
  8985. ++nhave;
  8986. }
  8987. kneighbors_q3xs[counter++] = dist2[2*j+1];
  8988. ++n;
  8989. }
  8990. *start = n;
  8991. }
  8992. free(dist2);
  8993. }
  8994. void iq3xs_free_impl(int grid_size) {
  8995. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8996. const int gindex = iq3_data_index(grid_size);
  8997. if (iq3_data[gindex].grid) {
  8998. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  8999. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  9000. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  9001. }
  9002. }
  9003. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  9004. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  9005. int num_neighbors = neighbours[0];
  9006. GGML_ASSERT(num_neighbors > 0);
  9007. float best_d2 = FLT_MAX;
  9008. int grid_index = -1;
  9009. for (int j = 1; j <= num_neighbors; ++j) {
  9010. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9011. float d2 = 0;
  9012. for (int i = 0; i < 4; ++i) {
  9013. float q = pg[i];
  9014. float diff = scale*q - xval[i];
  9015. d2 += weight[i]*diff*diff;
  9016. }
  9017. if (d2 < best_d2) {
  9018. best_d2 = d2; grid_index = neighbours[j];
  9019. }
  9020. }
  9021. GGML_ASSERT(grid_index >= 0);
  9022. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9023. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  9024. return grid_index;
  9025. }
  9026. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int n,
  9027. const float * restrict quant_weights) {
  9028. const int gindex = iq3_data_index(grid_size);
  9029. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9030. const int * kmap_q3xs = iq3_data[gindex].map;
  9031. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9032. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9033. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9034. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9035. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9036. GGML_ASSERT(n%QK_K == 0);
  9037. const int kMaxQ = 8;
  9038. const int nbl = n/QK_K;
  9039. ggml_fp16_t * dh;
  9040. uint8_t * qs;
  9041. int block_size;
  9042. if (grid_size == 256) {
  9043. block_iq3_xxs * y = vy;
  9044. dh = &y->d;
  9045. qs = y->qs;
  9046. block_size = sizeof(block_iq3_xxs);
  9047. } else {
  9048. block_iq3_s * y = vy;
  9049. dh = &y->d;
  9050. qs = y->qs;
  9051. block_size = sizeof(block_iq3_s);
  9052. }
  9053. int quant_size = block_size - sizeof(ggml_fp16_t);
  9054. float scales[QK_K/32];
  9055. float weight[32];
  9056. float xval[32];
  9057. int8_t L[32];
  9058. int8_t Laux[32];
  9059. float waux[32];
  9060. bool is_on_grid[8];
  9061. bool is_on_grid_aux[8];
  9062. uint8_t block_signs[8];
  9063. uint8_t q3[3*(QK_K/8)+QK_K/32];
  9064. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  9065. uint8_t * qh = q3 + 3*(QK_K/8);
  9066. for (int ibl = 0; ibl < nbl; ++ibl) {
  9067. dh[0] = GGML_FP32_TO_FP16(0.f);
  9068. memset(q3, 0, 3*QK_K/8+QK_K/32);
  9069. float max_scale = 0;
  9070. const float * xbl = x + QK_K*ibl;
  9071. float sumx2 = 0;
  9072. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9073. float sigma2 = 2*sumx2/QK_K;
  9074. for (int ib = 0; ib < QK_K/32; ++ib) {
  9075. const float * xb = xbl + 32*ib;
  9076. if (quant_weights) {
  9077. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  9078. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9079. } else {
  9080. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  9081. }
  9082. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  9083. for (int k = 0; k < 4; ++k) {
  9084. int nflip = 0;
  9085. uint8_t s = 0;
  9086. for (int i = 0; i < 8; ++i) {
  9087. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9088. else {
  9089. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  9090. }
  9091. }
  9092. if (nflip%2) {
  9093. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  9094. for (int i = 1; i < 8; ++i) {
  9095. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  9096. if (ax < min) {
  9097. min = ax; imin = i;
  9098. }
  9099. }
  9100. xval[8*k+imin] = -xval[8*k+imin];
  9101. s ^= (1 << imin);
  9102. }
  9103. block_signs[k] = s & 127;
  9104. }
  9105. float max = xval[0];
  9106. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  9107. if (!max) {
  9108. scales[ib] = 0;
  9109. memset(L, 0, 32);
  9110. continue;
  9111. }
  9112. float best = 0;
  9113. float scale = max/(2*kMaxQ-1);
  9114. for (int is = -15; is <= 15; ++is) {
  9115. float id = (2*kMaxQ-1+is*0.2f)/max;
  9116. float this_scale = 1/id;
  9117. for (int k = 0; k < 8; ++k) {
  9118. for (int i = 0; i < 4; ++i) {
  9119. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9120. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9121. }
  9122. uint16_t u = 0;
  9123. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9124. int grid_index = kmap_q3xs[u];
  9125. is_on_grid_aux[k] = true;
  9126. if (grid_index < 0) {
  9127. is_on_grid_aux[k] = false;
  9128. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9129. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9130. }
  9131. }
  9132. float sumqx = 0, sumq2 = 0;
  9133. for (int i = 0; i < 32; ++i) {
  9134. float w = weight[i];
  9135. float q = 2*Laux[i] + 1;
  9136. sumqx += w*xval[i]*q;
  9137. sumq2 += w*q*q;
  9138. }
  9139. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9140. scale = sumqx/sumq2; best = scale*sumqx;
  9141. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  9142. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9143. }
  9144. }
  9145. int n_not_ongrid = 0;
  9146. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9147. if (n_not_ongrid > 0 && scale > 0) {
  9148. float id = 1/scale;
  9149. for (int k = 0; k < 8; ++k) {
  9150. if (is_on_grid[k]) continue;
  9151. uint16_t u = 0;
  9152. for (int i = 0; i < 4; ++i) {
  9153. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9154. l = MAX(0, MIN(kMaxQ-1, l));
  9155. u |= (l << 3*i);
  9156. }
  9157. int grid_index = kmap_q3xs[u];
  9158. if (grid_index < 0) {
  9159. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9160. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9161. }
  9162. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9163. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9164. }
  9165. float sumqx = 0, sumq2 = 0;
  9166. for (int i = 0; i < 32; ++i) {
  9167. float w = weight[i];
  9168. float q = 2*L[i] + 1;
  9169. sumqx += w*xval[i]*q;
  9170. sumq2 += w*q*q;
  9171. }
  9172. if (sumq2 > 0) scale = sumqx/sumq2;
  9173. }
  9174. if (scale < 0) {
  9175. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9176. // and correspondingly flip quant signs.
  9177. scale = -scale;
  9178. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  9179. }
  9180. for (int k = 0; k < 8; ++k) {
  9181. uint16_t u = 0;
  9182. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9183. int grid_index = kmap_q3xs[u];
  9184. if (grid_index < 0) {
  9185. printf("Oops: found point %u not on grid:", u);
  9186. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9187. printf("\n");
  9188. GGML_ASSERT(false);
  9189. }
  9190. if (grid_size == 256) {
  9191. q3[8*ib+k] = grid_index;
  9192. } else {
  9193. q3[8*ib+k] = grid_index & 255;
  9194. qh[ib] |= ((grid_index >> 8) << k);
  9195. }
  9196. }
  9197. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  9198. GGML_ASSERT(scale >= 0);
  9199. scales[ib] = scale;
  9200. max_scale = MAX(max_scale, scale);
  9201. }
  9202. if (!max_scale) {
  9203. memset(qs, 0, quant_size);
  9204. dh += block_size/sizeof(ggml_fp16_t);
  9205. qs += block_size;
  9206. continue;
  9207. }
  9208. float d = max_scale/31;
  9209. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  9210. float id = 1/d;
  9211. for (int ib = 0; ib < QK_K/32; ++ib) {
  9212. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9213. l = MAX(0, MIN(15, l));
  9214. scales_and_signs[ib] |= ((uint32_t)l << 28);
  9215. }
  9216. memcpy(qs, q3, quant_size);
  9217. dh += block_size/sizeof(ggml_fp16_t);
  9218. qs += block_size;
  9219. }
  9220. }
  9221. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9222. GGML_ASSERT(n_per_row%QK_K == 0);
  9223. int nblock = n_per_row/QK_K;
  9224. char * qrow = (char *)dst;
  9225. for (int row = 0; row < nrow; ++row) {
  9226. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  9227. src += n_per_row;
  9228. qrow += nblock*sizeof(block_iq3_xxs);
  9229. }
  9230. return nrow * nblock * sizeof(block_iq3_xxs);
  9231. }
  9232. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int k) {
  9233. assert(k % QK_K == 0);
  9234. block_iq3_xxs * restrict y = vy;
  9235. quantize_row_iq3_xxs_reference(x, y, k);
  9236. }
  9237. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int k) {
  9238. assert(k % QK_K == 0);
  9239. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  9240. }
  9241. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  9242. const float * restrict quant_weights,
  9243. float * scales,
  9244. float * weight,
  9245. float * xval,
  9246. int8_t * L,
  9247. int8_t * Laux,
  9248. float * waux,
  9249. bool * is_on_grid,
  9250. bool * is_on_grid_aux,
  9251. uint8_t * block_signs) {
  9252. const int gindex = iq3_data_index(512);
  9253. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9254. const int * kmap_q3xs = iq3_data[gindex].map;
  9255. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9256. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9257. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9258. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9259. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9260. GGML_ASSERT(n%QK_K == 0);
  9261. const int kMaxQ = 8;
  9262. const int nbl = n/QK_K;
  9263. block_iq3_s * y = vy;
  9264. const int bs4 = block_size/4;
  9265. const int bs8 = block_size/8;
  9266. for (int ibl = 0; ibl < nbl; ++ibl) {
  9267. memset(&y[ibl], 0, sizeof(block_iq3_s));
  9268. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9269. uint8_t * qs = y[ibl].qs;
  9270. uint8_t * qh = y[ibl].qh;
  9271. uint8_t * signs = y[ibl].signs;
  9272. float max_scale = 0;
  9273. const float * xbl = x + QK_K*ibl;
  9274. float sumx2 = 0;
  9275. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9276. float sigma2 = 2*sumx2/QK_K;
  9277. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9278. const float * xb = xbl + block_size*ib;
  9279. if (quant_weights) {
  9280. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9281. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9282. } else {
  9283. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9284. }
  9285. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  9286. for (int k = 0; k < bs8; ++k) {
  9287. uint8_t s = 0;
  9288. for (int i = 0; i < 8; ++i) {
  9289. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9290. else {
  9291. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9292. }
  9293. }
  9294. block_signs[k] = s;
  9295. }
  9296. float max = xval[0];
  9297. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  9298. if (!max) {
  9299. scales[ib] = 0;
  9300. continue;
  9301. }
  9302. float best = 0;
  9303. float scale = max/(2*kMaxQ-1);
  9304. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  9305. for (int is = -9; is <= 9; ++is) {
  9306. float id = (2*kMaxQ-1+is*0.2f)/max;
  9307. float this_scale = 1/id;
  9308. for (int k = 0; k < bs4; ++k) {
  9309. for (int i = 0; i < 4; ++i) {
  9310. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9311. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9312. }
  9313. uint16_t u = 0;
  9314. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9315. int grid_index = kmap_q3xs[u];
  9316. is_on_grid_aux[k] = true;
  9317. if (grid_index < 0) {
  9318. is_on_grid_aux[k] = false;
  9319. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9320. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9321. }
  9322. }
  9323. float sumqx = 0, sumq2 = 0;
  9324. for (int i = 0; i < block_size; ++i) {
  9325. float w = weight[i];
  9326. float q = 2*Laux[i] + 1;
  9327. sumqx += w*xval[i]*q;
  9328. sumq2 += w*q*q;
  9329. }
  9330. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9331. scale = sumqx/sumq2; best = scale*sumqx;
  9332. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  9333. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9334. }
  9335. }
  9336. int n_not_ongrid = 0;
  9337. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9338. if (n_not_ongrid > 0 && scale > 0) {
  9339. float id = 1/scale;
  9340. for (int k = 0; k < bs4; ++k) {
  9341. //if (is_on_grid[k]) continue;
  9342. uint16_t u = 0;
  9343. for (int i = 0; i < 4; ++i) {
  9344. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9345. l = MAX(0, MIN(kMaxQ-1, l));
  9346. u |= (l << 3*i);
  9347. }
  9348. int grid_index = kmap_q3xs[u];
  9349. if (grid_index < 0) {
  9350. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9351. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9352. }
  9353. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9354. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9355. }
  9356. float sumqx = 0, sumq2 = 0;
  9357. for (int i = 0; i < block_size; ++i) {
  9358. float w = weight[i];
  9359. float q = 2*L[i] + 1;
  9360. sumqx += w*xval[i]*q;
  9361. sumq2 += w*q*q;
  9362. }
  9363. if (sumq2 > 0) scale = sumqx/sumq2;
  9364. }
  9365. if (scale < 0) {
  9366. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9367. // and correspondingly flip quant signs.
  9368. scale = -scale;
  9369. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  9370. }
  9371. for (int k = 0; k < bs4; ++k) {
  9372. uint16_t u = 0;
  9373. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9374. int grid_index = kmap_q3xs[u];
  9375. if (grid_index < 0) {
  9376. printf("Oops: found point %u not on grid:", u);
  9377. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9378. printf("\n");
  9379. GGML_ASSERT(false);
  9380. }
  9381. qs[k] = grid_index & 255;
  9382. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  9383. }
  9384. qs += bs4;
  9385. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  9386. signs += bs8;
  9387. GGML_ASSERT(scale >= 0);
  9388. scales[ib] = scale;
  9389. max_scale = MAX(max_scale, scale);
  9390. }
  9391. if (!max_scale) {
  9392. continue;
  9393. }
  9394. float d = max_scale/31;
  9395. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  9396. float id = 1/d;
  9397. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  9398. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  9399. l1 = MAX(0, MIN(15, l1));
  9400. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  9401. l2 = MAX(0, MIN(15, l2));
  9402. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  9403. }
  9404. }
  9405. }
  9406. #define IQ3S_BLOCK_SIZE 32
  9407. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9408. GGML_ASSERT(n_per_row%QK_K == 0);
  9409. int nblock = n_per_row/QK_K;
  9410. float scales[QK_K/IQ3S_BLOCK_SIZE];
  9411. float weight[IQ3S_BLOCK_SIZE];
  9412. float xval[IQ3S_BLOCK_SIZE];
  9413. int8_t L[IQ3S_BLOCK_SIZE];
  9414. int8_t Laux[IQ3S_BLOCK_SIZE];
  9415. float waux[IQ3S_BLOCK_SIZE];
  9416. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  9417. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  9418. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  9419. char * qrow = (char *)dst;
  9420. for (int row = 0; row < nrow; ++row) {
  9421. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  9422. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  9423. src += n_per_row;
  9424. qrow += nblock*sizeof(block_iq3_s);
  9425. }
  9426. return nrow * nblock * sizeof(block_iq3_s);
  9427. }
  9428. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int k) {
  9429. assert(k % QK_K == 0);
  9430. block_iq3_s * restrict y = vy;
  9431. quantize_row_iq3_s_reference(x, y, k);
  9432. }
  9433. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int k) {
  9434. assert(k % QK_K == 0);
  9435. quantize_iq3_s(x, y, 1, k, NULL);
  9436. }
  9437. // =================================== 1.5 bpw ===================================================
  9438. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9439. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  9440. int num_neighbors = neighbours[0];
  9441. GGML_ASSERT(num_neighbors > 0);
  9442. float best_score = 0;
  9443. int grid_index = -1;
  9444. for (int j = 1; j <= num_neighbors; ++j) {
  9445. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9446. float sumqx = 0, sumq2 = 0;
  9447. for (int i = 0; i < 8; ++i) {
  9448. float q = (pg[i] - 3)/2;
  9449. float w = weight[i];
  9450. sumqx += w*q*xval[i];
  9451. sumq2 += w*q*q;
  9452. }
  9453. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9454. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  9455. grid_index = neighbours[j];
  9456. }
  9457. }
  9458. if (grid_index < 0) {
  9459. for (int i = 0; i < ngrid; ++i) {
  9460. const int8_t * grid_i = (const int8_t *)(grid + i);
  9461. float sumqx = 0, sumq2 = 0;
  9462. for (int j = 0; j < 8; ++j) {
  9463. float w = weight[j];
  9464. float q = (grid_i[j] - 3)/2;
  9465. sumqx += w*q*xval[j];
  9466. sumq2 += w*q*q;
  9467. }
  9468. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9469. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  9470. grid_index = i;
  9471. }
  9472. }
  9473. }
  9474. if (grid_index < 0) {
  9475. printf("Oops, did not find grid point\n");
  9476. printf("Have %d neighbours\n", num_neighbors);
  9477. for (int j = 1; j <= num_neighbors; ++j) {
  9478. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9479. float sumqx = 0, sumq2 = 0;
  9480. for (int i = 0; i < 8; ++i) {
  9481. float q = (pg[i] - 3)/2;
  9482. float w = weight[i];
  9483. sumqx += w*q*xval[i];
  9484. sumq2 += w*q*q;
  9485. }
  9486. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9487. }
  9488. }
  9489. GGML_ASSERT(grid_index >= 0);
  9490. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9491. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  9492. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9493. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9494. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9495. return grid_index;
  9496. }
  9497. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9498. const float * restrict xval, const float * restrict weight, float scale, const float * restrict xg, int8_t * restrict L, int ngrid) {
  9499. int num_neighbors = neighbours[0];
  9500. GGML_ASSERT(num_neighbors > 0);
  9501. float best_score = FLT_MAX;
  9502. int grid_index = -1;
  9503. for (int j = 1; j <= num_neighbors; ++j) {
  9504. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9505. float d2 = 0;
  9506. for (int i = 0; i < 8; ++i) {
  9507. float q = xg[(pg[i] - 1)/2];
  9508. float w = weight[i];
  9509. float diff = scale*q - xval[i];
  9510. d2 += w*diff*diff;
  9511. }
  9512. if (d2 < best_score) {
  9513. best_score = d2;
  9514. grid_index = neighbours[j];
  9515. }
  9516. }
  9517. if (grid_index < 0) {
  9518. for (int i = 0; i < ngrid; ++i) {
  9519. const int8_t * grid_i = (const int8_t *)(grid + i);
  9520. float d2 = 0;
  9521. for (int j = 0; j < 8; ++j) {
  9522. float w = weight[j];
  9523. float q = xg[(grid_i[j] - 1)/2];
  9524. float diff = scale*q - xval[i];
  9525. d2 += w*diff*diff;
  9526. }
  9527. if (d2 < best_score) {
  9528. best_score = d2;
  9529. grid_index = i;
  9530. }
  9531. }
  9532. }
  9533. if (grid_index < 0) {
  9534. printf("Oops, did not find grid point\n");
  9535. printf("Have %d neighbours\n", num_neighbors);
  9536. for (int j = 1; j <= num_neighbors; ++j) {
  9537. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9538. float sumqx = 0, sumq2 = 0;
  9539. for (int i = 0; i < 8; ++i) {
  9540. float q = xg[(pg[i] - 1)/2];
  9541. float w = weight[i];
  9542. sumqx += w*q*xval[i];
  9543. sumq2 += w*q*q;
  9544. }
  9545. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9546. }
  9547. }
  9548. GGML_ASSERT(grid_index >= 0);
  9549. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9550. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9551. return grid_index;
  9552. }
  9553. static int iq1_sort_helper(const void * left, const void * right) {
  9554. const float * l = left;
  9555. const float * r = right;
  9556. return *l < *r ? -1 : *l > *r ? 1 : 0;
  9557. }
  9558. #define IQ1S_BLOCK_SIZE 32
  9559. #define IQ1M_BLOCK_SIZE 16
  9560. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights,
  9561. float * scales,
  9562. float * weight,
  9563. float * sumx,
  9564. float * sumw,
  9565. float * pairs,
  9566. int8_t * L,
  9567. uint16_t * index,
  9568. int8_t * shifts) {
  9569. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  9570. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9571. const int * kmap_q2xs = iq2_data[gindex].map;
  9572. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9573. GGML_ASSERT(quant_weights && "missing quantization weights");
  9574. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9575. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9576. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9577. GGML_ASSERT(n%QK_K == 0);
  9578. block_iq1_s * y = vy;
  9579. const int nbl = n/QK_K;
  9580. const int block_size = IQ1S_BLOCK_SIZE;
  9581. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  9582. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  9583. int * idx = (int *)(pairs + 1);
  9584. for (int ibl = 0; ibl < nbl; ++ibl) {
  9585. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9586. memset(y[ibl].qs, 0, QK_K/8);
  9587. memset(y[ibl].qh, 0, QK_K/16);
  9588. float max_scale = 0;
  9589. const float * xbl = x + QK_K*ibl;
  9590. float sumx2 = 0;
  9591. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9592. float sigma2 = 2*sumx2/QK_K;
  9593. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9594. const float * xb = xbl + block_size*ib;
  9595. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9596. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9597. float max = fabsf(xb[0]);
  9598. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9599. if (!max) {
  9600. scales[ib] = 0;
  9601. memset(L, 1, block_size);
  9602. continue;
  9603. }
  9604. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9605. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9606. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9607. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9608. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9609. // for each possible and score for each split.
  9610. for (int j = 0; j < block_size; ++j) {
  9611. pairs[2*j] = xb[j];
  9612. idx[2*j] = j;
  9613. }
  9614. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9615. {
  9616. sumx[0] = sumw[0] = 0;
  9617. for (int j = 0; j < block_size; ++j) {
  9618. int i = idx[2*j];
  9619. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  9620. sumw[j+1] = sumw[j] + weight[i];
  9621. }
  9622. }
  9623. float best_score = 0, scale = max;
  9624. int besti1 = -1, besti2 = -1, best_shift = 0;
  9625. for (int i1 = 0; i1 <= block_size; ++i1) {
  9626. for (int i2 = i1; i2 <= block_size; ++i2) {
  9627. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[block_size] - sumx[i2])*x_p[2];
  9628. 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];
  9629. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9630. scale = sumqx/sumq2; best_score = scale*sumqx;
  9631. besti1 = i1; besti2 = i2; best_shift = 1;
  9632. }
  9633. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[block_size] - sumx[i2])*x_m[2];
  9634. 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];
  9635. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9636. scale = sumqx/sumq2; best_score = scale*sumqx;
  9637. besti1 = i1; besti2 = i2; best_shift = -1;
  9638. }
  9639. }
  9640. }
  9641. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  9642. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9643. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9644. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9645. if (scale < 0) {
  9646. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9647. scale = -scale; best_shift = -best_shift;
  9648. }
  9649. bool all_on_grid = true;
  9650. const float * xx = best_shift == 1 ? x_p : x_m;
  9651. for (int k = 0; k < block_size/8; ++k) {
  9652. uint16_t u = 0;
  9653. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9654. int grid_index = kmap_q2xs[u];
  9655. if (grid_index < 0) {
  9656. all_on_grid = false;
  9657. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9658. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9659. GGML_ASSERT(grid_index >= 0);
  9660. }
  9661. index[k] = grid_index;
  9662. }
  9663. if (!all_on_grid) {
  9664. float sumqx = 0, sumq2 = 0;
  9665. for (int k = 0; k < block_size/8; ++k) {
  9666. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9667. for (int j = 0; j < 8; ++j) {
  9668. float w = weight[8*k + j];
  9669. float q = xx[(pg[j] - 1)/2];
  9670. sumqx += w*q*xb[8*k+j];
  9671. sumq2 += w*q*q;
  9672. }
  9673. }
  9674. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  9675. }
  9676. uint16_t h = 0;
  9677. for (int k = 0; k < block_size/8; ++k) {
  9678. y[ibl].qs[(block_size/8)*ib + k] = index[k] & 255;
  9679. h |= (index[k] >> 8) << 3*k;
  9680. }
  9681. y[ibl].qh[ib] = h;
  9682. GGML_ASSERT(scale >= 0);
  9683. scales[ib] = scale;
  9684. shifts[ib] = best_shift;
  9685. max_scale = MAX(max_scale, scale);
  9686. }
  9687. if (!max_scale) {
  9688. continue;
  9689. }
  9690. float d = max_scale/15;
  9691. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.125f is another fudge factor. Don't ask me why it is needed.
  9692. float id = 1/d;
  9693. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9694. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9695. l = MAX(0, MIN(7, l));
  9696. if (shifts[ib] == -1) l |= 8;
  9697. y[ibl].qh[ib] |= (l << 12);
  9698. }
  9699. }
  9700. }
  9701. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9702. GGML_ASSERT(n_per_row%QK_K == 0);
  9703. float scales[QK_K/IQ1S_BLOCK_SIZE];
  9704. float weight[IQ1S_BLOCK_SIZE];
  9705. int8_t L[IQ1S_BLOCK_SIZE];
  9706. float sumx[IQ1S_BLOCK_SIZE+1];
  9707. float sumw[IQ1S_BLOCK_SIZE+1];
  9708. float pairs[2*IQ1S_BLOCK_SIZE];
  9709. uint16_t index[IQ1S_BLOCK_SIZE/8];
  9710. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  9711. int nblock = n_per_row/QK_K;
  9712. char * qrow = (char *)dst;
  9713. for (int row = 0; row < nrow; ++row) {
  9714. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights, scales, weight, sumx, sumw, pairs, L, index, shifts);
  9715. src += n_per_row;
  9716. qrow += nblock*sizeof(block_iq1_s);
  9717. }
  9718. return nrow * nblock * sizeof(block_iq1_s);
  9719. }
  9720. static void quantize_row_iq1_m_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights,
  9721. float * scales,
  9722. float * weight,
  9723. float * pairs,
  9724. int8_t * L,
  9725. uint16_t * index,
  9726. int8_t * shifts) {
  9727. const int gindex = iq2_data_index(GGML_TYPE_IQ1_M);
  9728. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9729. const int * kmap_q2xs = iq2_data[gindex].map;
  9730. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9731. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9732. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9733. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9734. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9735. GGML_ASSERT(n%QK_K == 0);
  9736. block_iq1_m * y = vy;
  9737. const int nbl = n/QK_K;
  9738. const int block_size = IQ1M_BLOCK_SIZE;
  9739. const float x_p[3] = {-1 + IQ1M_DELTA, IQ1M_DELTA, 1 + IQ1M_DELTA};
  9740. const float x_m[3] = {-1 - IQ1M_DELTA, -IQ1M_DELTA, 1 - IQ1M_DELTA};
  9741. const uint8_t masks[4] = {0x00, 0x80, 0x08, 0x88};
  9742. int * idx = (int *)(pairs + 1);
  9743. float sumqx[4], sumq2[4];
  9744. iq1m_scale_t s;
  9745. const float * xx;
  9746. for (int ibl = 0; ibl < nbl; ++ibl) {
  9747. //y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9748. memset(y[ibl].qs, 0, QK_K/8);
  9749. memset(y[ibl].qh, 0, QK_K/16);
  9750. memset(y[ibl].scales, 0, QK_K/32);
  9751. float max_scale = 0;
  9752. const float * xbl = x + QK_K*ibl;
  9753. float sumx2 = 0;
  9754. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9755. float sigma2 = 2*sumx2/QK_K;
  9756. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9757. const float * xb = xbl + block_size*ib;
  9758. if (quant_weights) {
  9759. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9760. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9761. } else {
  9762. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9763. }
  9764. float max = fabsf(xb[0]);
  9765. for (int i = 1; i < block_size; ++i) max = MAX(max, fabsf(xb[i]));
  9766. if (!max) {
  9767. scales[ib] = 0;
  9768. memset(L, 1, block_size);
  9769. continue;
  9770. }
  9771. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9772. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9773. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9774. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9775. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9776. // for each possible and score for each split.
  9777. for (int j = 0; j < block_size; ++j) {
  9778. pairs[2*j] = xb[j];
  9779. idx[2*j] = j;
  9780. }
  9781. qsort(pairs, block_size, 2*sizeof(float), iq1_sort_helper);
  9782. float best_score = 0, scale = max;
  9783. int besti1 = -1, besti2 = -1, best_k = -1;
  9784. // 0: +, +
  9785. // 1: +, -
  9786. // 2: -, +
  9787. // 3: -, -
  9788. for (int i1 = 0; i1 <= block_size; ++i1) {
  9789. for (int i2 = i1; i2 <= block_size; ++i2) {
  9790. memset(sumqx, 0, 4*sizeof(float));
  9791. memset(sumq2, 0, 4*sizeof(float));
  9792. for (int j = 0; j < i1; ++j) {
  9793. int i = idx[2*j];
  9794. if (i < block_size/2) {
  9795. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9796. sumqx[1] += weight[i]*x_p[0]*xb[i];
  9797. sumqx[2] += weight[i]*x_m[0]*xb[i];
  9798. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9799. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9800. sumq2[1] += weight[i]*x_p[0]*x_p[0];
  9801. sumq2[2] += weight[i]*x_m[0]*x_m[0];
  9802. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9803. } else {
  9804. sumqx[0] += weight[i]*x_p[0]*xb[i];
  9805. sumqx[2] += weight[i]*x_p[0]*xb[i];
  9806. sumqx[1] += weight[i]*x_m[0]*xb[i];
  9807. sumqx[3] += weight[i]*x_m[0]*xb[i];
  9808. sumq2[0] += weight[i]*x_p[0]*x_p[0];
  9809. sumq2[2] += weight[i]*x_p[0]*x_p[0];
  9810. sumq2[1] += weight[i]*x_m[0]*x_m[0];
  9811. sumq2[3] += weight[i]*x_m[0]*x_m[0];
  9812. }
  9813. }
  9814. for (int j = i1; j < i2; ++j) {
  9815. int i = idx[2*j];
  9816. if (i < block_size/2) {
  9817. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9818. sumqx[1] += weight[i]*x_p[1]*xb[i];
  9819. sumqx[2] += weight[i]*x_m[1]*xb[i];
  9820. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9821. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9822. sumq2[1] += weight[i]*x_p[1]*x_p[1];
  9823. sumq2[2] += weight[i]*x_m[1]*x_m[1];
  9824. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9825. } else {
  9826. sumqx[0] += weight[i]*x_p[1]*xb[i];
  9827. sumqx[2] += weight[i]*x_p[1]*xb[i];
  9828. sumqx[1] += weight[i]*x_m[1]*xb[i];
  9829. sumqx[3] += weight[i]*x_m[1]*xb[i];
  9830. sumq2[0] += weight[i]*x_p[1]*x_p[1];
  9831. sumq2[2] += weight[i]*x_p[1]*x_p[1];
  9832. sumq2[1] += weight[i]*x_m[1]*x_m[1];
  9833. sumq2[3] += weight[i]*x_m[1]*x_m[1];
  9834. }
  9835. }
  9836. for (int j = i2; j < block_size; ++j) {
  9837. int i = idx[2*j];
  9838. if (i < block_size/2) {
  9839. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9840. sumqx[1] += weight[i]*x_p[2]*xb[i];
  9841. sumqx[2] += weight[i]*x_m[2]*xb[i];
  9842. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9843. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9844. sumq2[1] += weight[i]*x_p[2]*x_p[2];
  9845. sumq2[2] += weight[i]*x_m[2]*x_m[2];
  9846. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9847. } else {
  9848. sumqx[0] += weight[i]*x_p[2]*xb[i];
  9849. sumqx[2] += weight[i]*x_p[2]*xb[i];
  9850. sumqx[1] += weight[i]*x_m[2]*xb[i];
  9851. sumqx[3] += weight[i]*x_m[2]*xb[i];
  9852. sumq2[0] += weight[i]*x_p[2]*x_p[2];
  9853. sumq2[2] += weight[i]*x_p[2]*x_p[2];
  9854. sumq2[1] += weight[i]*x_m[2]*x_m[2];
  9855. sumq2[3] += weight[i]*x_m[2]*x_m[2];
  9856. }
  9857. }
  9858. for (int k = 0; k < 4; ++k) {
  9859. if (sumq2[k] > 0 && sumqx[k]*sumqx[k] > best_score*sumq2[k]) {
  9860. scale = sumqx[k]/sumq2[k]; best_score = scale*sumqx[k];
  9861. besti1 = i1; besti2 = i2; best_k = k;
  9862. }
  9863. }
  9864. }
  9865. }
  9866. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_k >= 0);
  9867. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9868. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9869. for (int j = besti2; j < block_size; ++j) L[idx[2*j]] = 2;
  9870. if (scale < 0) {
  9871. for (int j = 0; j < block_size; ++j) L[j] = 2 - L[j];
  9872. scale = -scale;
  9873. best_k = best_k == 0 ? 3 : best_k == 1 ? 2 : best_k == 2 ? 1 : 0;
  9874. }
  9875. bool all_on_grid = true;
  9876. for (int k = 0; k < block_size/8; ++k) {
  9877. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9878. else xx = best_k%2 == 0 ? x_p : x_m;
  9879. uint16_t u = 0;
  9880. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9881. int grid_index = kmap_q2xs[u];
  9882. if (grid_index < 0) {
  9883. all_on_grid = false;
  9884. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9885. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9886. GGML_ASSERT(grid_index >= 0);
  9887. }
  9888. index[k] = grid_index;
  9889. }
  9890. if (!all_on_grid) {
  9891. float sumqx_f = 0, sumq2_f = 0;
  9892. for (int k = 0; k < block_size/8; ++k) {
  9893. if (k == 0) xx = best_k < 2 ? x_p : x_m;
  9894. else xx = best_k%2 == 0 ? x_p : x_m;
  9895. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9896. for (int j = 0; j < 8; ++j) {
  9897. float w = weight[8*k + j];
  9898. float q = xx[(pg[j] - 1)/2];
  9899. sumqx_f += w*q*xb[8*k+j];
  9900. sumq2_f += w*q*q;
  9901. }
  9902. }
  9903. if (sumqx_f > 0 && sumq2_f > 0) scale = sumqx_f/sumq2_f;
  9904. }
  9905. y[ibl].qs[2*ib + 0] = index[0] & 255;
  9906. y[ibl].qs[2*ib + 1] = index[1] & 255;
  9907. y[ibl].qh[ib] = (index[0] >> 8) | ((index[1] >> 8) << 4);
  9908. GGML_ASSERT(scale >= 0);
  9909. scales[ib] = scale;
  9910. shifts[ib] = best_k;
  9911. max_scale = MAX(max_scale, scale);
  9912. }
  9913. if (!max_scale) {
  9914. continue;
  9915. }
  9916. uint16_t * sc = (uint16_t *)y[ibl].scales;
  9917. float d = max_scale/15;
  9918. float id = 1/d;
  9919. float sumqx_f = 0, sumq2_f = 0;
  9920. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9921. int l = nearest_int(0.5f*(id*scales[ib+0]-1));
  9922. l = MAX(0, MIN(7, l));
  9923. sc[ib/4] |= (l << 3*(ib%4));
  9924. y[ibl].qh[ib] |= masks[shifts[ib]];
  9925. const float * xb = xbl + block_size*ib;
  9926. if (quant_weights) {
  9927. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9928. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9929. } else {
  9930. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9931. }
  9932. for (int k = 0; k < block_size/8; ++k) {
  9933. if (k == 0) xx = shifts[ib] < 2 ? x_p : x_m;
  9934. else xx = shifts[ib]%2 == 0 ? x_p : x_m;
  9935. const int8_t * pg = (const int8_t *)(kgrid_q2xs + y[ibl].qs[2*ib+k] + ((y[ibl].qh[ib] << (8 - 4*k)) & 0x700));
  9936. for (int j = 0; j < 8; ++j) {
  9937. float w = weight[8*k + j];
  9938. float q = xx[(pg[j] - 1)/2]*(2*l+1);
  9939. sumqx_f += w*q*xb[8*k+j];
  9940. sumq2_f += w*q*q;
  9941. }
  9942. }
  9943. }
  9944. if (sumq2_f > 0) d = sumqx_f/sumq2_f;
  9945. s.f16 = GGML_FP32_TO_FP16(d*1.1125f); // 1.1125f is another fudge factor. Don't ask me why it is needed.
  9946. sc[0] |= ((s.u16 & 0x000f) << 12);
  9947. sc[1] |= ((s.u16 & 0x00f0) << 8);
  9948. sc[2] |= ((s.u16 & 0x0f00) << 4);
  9949. sc[3] |= ((s.u16 & 0xf000) << 0);
  9950. }
  9951. }
  9952. size_t quantize_iq1_m(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9953. GGML_ASSERT(n_per_row%QK_K == 0);
  9954. float scales[QK_K/IQ1M_BLOCK_SIZE];
  9955. float weight[IQ1M_BLOCK_SIZE];
  9956. int8_t L[IQ1M_BLOCK_SIZE];
  9957. float pairs[2*IQ1M_BLOCK_SIZE];
  9958. uint16_t index[IQ1M_BLOCK_SIZE/8];
  9959. int8_t shifts[QK_K/IQ1M_BLOCK_SIZE];
  9960. int nblock = n_per_row/QK_K;
  9961. char * qrow = (char *)dst;
  9962. for (int row = 0; row < nrow; ++row) {
  9963. quantize_row_iq1_m_impl(src, qrow, n_per_row, quant_weights, scales, weight, pairs, L, index, shifts);
  9964. src += n_per_row;
  9965. qrow += nblock*sizeof(block_iq1_m);
  9966. }
  9967. return nrow * nblock * sizeof(block_iq1_m);
  9968. }
  9969. // ============================ 4-bit non-linear quants
  9970. static inline int best_index_int8(int n, const int8_t * val, float x) {
  9971. if (x <= val[0]) return 0;
  9972. if (x >= val[n-1]) return n-1;
  9973. int ml = 0, mu = n-1;
  9974. while (mu-ml > 1) {
  9975. int mav = (ml+mu)/2;
  9976. if (x < val[mav]) mu = mav; else ml = mav;
  9977. }
  9978. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  9979. }
  9980. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  9981. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  9982. float * scales, float * weight, uint8_t * L,
  9983. const int8_t * values,
  9984. const float * quant_weights,
  9985. const int ntry) {
  9986. float sigma2 = 0;
  9987. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  9988. sigma2 *= 2.f/super_block_size;
  9989. memset(q4, 0, super_block_size/2);
  9990. dh[0] = GGML_FP32_TO_FP16(0.f);
  9991. float max_scale = 0, amax_scale = 0;
  9992. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9993. const float * xb = x + ib*block_size;
  9994. uint8_t * Lb = L + ib*block_size;
  9995. if (quant_weights) {
  9996. const float * qw = quant_weights + ib*block_size;
  9997. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  9998. } else {
  9999. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  10000. }
  10001. float amax = 0, max = 0;
  10002. for (int j = 0; j < block_size; ++j) {
  10003. float ax = fabsf(xb[j]);
  10004. if (ax > amax) {
  10005. amax = ax; max = xb[j];
  10006. }
  10007. }
  10008. if (!amax) {
  10009. scales[ib] = 0;
  10010. continue;
  10011. }
  10012. float d = ntry > 0 ? -max/values[0] : max/values[0];
  10013. float id = 1/d;
  10014. float sumqx = 0, sumq2 = 0;
  10015. for (int j = 0; j < block_size; ++j) {
  10016. float al = id*xb[j];
  10017. int l = best_index_int8(16, values, al);
  10018. Lb[j] = l;
  10019. float q = values[l];
  10020. float w = weight[j];
  10021. sumqx += w*q*xb[j];
  10022. sumq2 += w*q*q;
  10023. }
  10024. d = sumqx/sumq2;
  10025. float best = d*sumqx;
  10026. for (int itry = -ntry; itry <= ntry; ++itry) {
  10027. id = (itry + values[0])/max;
  10028. sumqx = sumq2 = 0;
  10029. for (int j = 0; j < block_size; ++j) {
  10030. float al = id*xb[j];
  10031. int l = best_index_int8(16, values, al);
  10032. float q = values[l];
  10033. float w = weight[j];
  10034. sumqx += w*q*xb[j];
  10035. sumq2 += w*q*q;
  10036. }
  10037. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10038. d = sumqx/sumq2; best = d * sumqx;
  10039. }
  10040. }
  10041. scales[ib] = d;
  10042. float abs_d = fabsf(d);
  10043. if (abs_d > amax_scale) {
  10044. amax_scale = abs_d; max_scale = d;
  10045. }
  10046. }
  10047. if (super_block_size/block_size > 1) {
  10048. int nb = super_block_size/block_size;
  10049. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  10050. float d = -max_scale/32;
  10051. dh[0] = GGML_FP32_TO_FP16(d);
  10052. float id = d ? 1/d : 0.f;
  10053. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  10054. int l = nearest_int(id*scales[ib]);
  10055. l = MAX(-32, MIN(31, l));
  10056. float dl = d * l;
  10057. float idl = dl ? 1/dl : 0.f;
  10058. uint8_t * Lb = L + ib*block_size;
  10059. const float * xb = x + ib*block_size;
  10060. for (int j = 0; j < block_size; ++j) {
  10061. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  10062. }
  10063. l += 32;
  10064. uint8_t l_l = l & 0xf;
  10065. uint8_t l_h = l >> 4;
  10066. if (ib%2 == 0) scales_l[ib/2] = l_l;
  10067. else scales_l[ib/2] |= (l_l << 4);
  10068. scales_h[ib/8] |= (l_h << 2*(ib%8));
  10069. }
  10070. } else {
  10071. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  10072. if (ntry > 0) {
  10073. float id = scales[0] ? 1/scales[0] : 0;
  10074. for (int j = 0; j < super_block_size; ++j) {
  10075. L[j] = best_index_int8(16, values, id*x[j]);
  10076. }
  10077. }
  10078. }
  10079. for (int i = 0; i < super_block_size/32; ++i) {
  10080. for (int j = 0; j < 16; ++j) {
  10081. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  10082. }
  10083. }
  10084. }
  10085. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  10086. GGML_ASSERT(n_per_row%QK4_NL == 0);
  10087. int nblock = n_per_row/QK4_NL;
  10088. char * qrow = (char *)dst;
  10089. uint8_t L[QK4_NL];
  10090. float weight[QK4_NL];
  10091. uint16_t unused_h;
  10092. uint8_t * unused_l = NULL;
  10093. float scale;
  10094. for (int row = 0; row < nrow; ++row) {
  10095. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  10096. for (int ibl = 0; ibl < nblock; ++ibl) {
  10097. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  10098. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  10099. &scale, weight, L, kvalues_iq4nl, qw, 7);
  10100. }
  10101. src += n_per_row;
  10102. qrow += nblock*sizeof(block_iq4_nl);
  10103. }
  10104. return nrow * nblock * sizeof(block_iq4_nl);
  10105. }
  10106. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int k) {
  10107. GGML_ASSERT(k%QK4_NL == 0);
  10108. int nblock = k/QK4_NL;
  10109. uint8_t L[QK4_NL];
  10110. float weight[QK4_NL];
  10111. uint16_t unused_h;
  10112. uint8_t * unused_l = NULL;
  10113. float scale;
  10114. block_iq4_nl * iq4 = (block_iq4_nl *)vy;
  10115. for (int ibl = 0; ibl < nblock; ++ibl) {
  10116. quantize_row_iq4_nl_impl(QK4_NL, 32, x + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  10117. &scale, weight, L, kvalues_iq4nl, NULL, -1);
  10118. }
  10119. }
  10120. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int k) {
  10121. assert(k % QK4_NL == 0);
  10122. quantize_row_iq4_nl(x, y, k);
  10123. }
  10124. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  10125. #if QK_K == 64
  10126. return quantize_iq4_nl(src, dst, nrow, n_per_row, quant_weights);
  10127. #else
  10128. GGML_ASSERT(n_per_row%QK_K == 0);
  10129. int nblock = n_per_row/QK_K;
  10130. char * qrow = (char *)dst;
  10131. uint8_t L[QK_K];
  10132. float weight[32];
  10133. float scales[QK_K/32];
  10134. for (int row = 0; row < nrow; ++row) {
  10135. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  10136. for (int ibl = 0; ibl < nblock; ++ibl) {
  10137. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  10138. 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,
  10139. scales, weight, L, kvalues_iq4nl, qw, 7);
  10140. }
  10141. src += n_per_row;
  10142. qrow += nblock*sizeof(block_iq4_xs);
  10143. }
  10144. return nrow * nblock * sizeof(block_iq4_xs);
  10145. #endif
  10146. }
  10147. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int k) {
  10148. assert(k % QK_K == 0);
  10149. block_iq4_xs * restrict y = vy;
  10150. quantize_row_iq4_xs_reference(x, y, k);
  10151. }
  10152. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int k) {
  10153. assert(k % QK_K == 0);
  10154. quantize_iq4_xs(x, y, 1, k, NULL);
  10155. }
  10156. // =============================== 2.5625 bpw
  10157. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  10158. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  10159. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  10160. const int * kmap_q2xs = iq2_data[gindex].map;
  10161. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  10162. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  10163. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  10164. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  10165. GGML_ASSERT(n%QK_K == 0);
  10166. const int kMaxQ = 3;
  10167. const int nbl = n/QK_K;
  10168. block_iq2_s * y = vy;
  10169. float scales[QK_K/16];
  10170. float weight[16];
  10171. float xval[16];
  10172. int8_t L[16];
  10173. int8_t Laux[16];
  10174. float waux[16];
  10175. bool is_on_grid[2];
  10176. bool is_on_grid_aux[2];
  10177. uint8_t block_signs[2];
  10178. for (int ibl = 0; ibl < nbl; ++ibl) {
  10179. memset(&y[ibl], 0, sizeof(block_iq2_s));
  10180. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  10181. float max_scale = 0;
  10182. const float * xbl = x + QK_K*ibl;
  10183. float sumx2 = 0;
  10184. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  10185. float sigma2 = 2*sumx2/QK_K;
  10186. for (int ib = 0; ib < QK_K/16; ++ib) {
  10187. const float * xb = xbl + 16*ib;
  10188. if (quant_weights) {
  10189. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  10190. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  10191. } else {
  10192. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  10193. }
  10194. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  10195. for (int k = 0; k < 2; ++k) {
  10196. uint8_t s = 0;
  10197. for (int i = 0; i < 8; ++i) {
  10198. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  10199. else {
  10200. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  10201. }
  10202. }
  10203. block_signs[k] = s;
  10204. }
  10205. float max = xval[0];
  10206. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  10207. if (!max) {
  10208. scales[ib] = 0;
  10209. continue;
  10210. }
  10211. float best = 0;
  10212. float scale = max/(2*kMaxQ-1);
  10213. is_on_grid[0] = is_on_grid[1] = true;
  10214. for (int is = -9; is <= 9; ++is) {
  10215. float id = (2*kMaxQ-1+is*0.1f)/max;
  10216. float this_scale = 1/id;
  10217. for (int k = 0; k < 2; ++k) {
  10218. for (int i = 0; i < 8; ++i) {
  10219. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10220. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  10221. }
  10222. uint16_t u = 0;
  10223. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  10224. int grid_index = kmap_q2xs[u];
  10225. is_on_grid_aux[k] = true;
  10226. if (grid_index < 0) {
  10227. is_on_grid_aux[k] = false;
  10228. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10229. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  10230. }
  10231. }
  10232. float sumqx = 0, sumq2 = 0;
  10233. for (int i = 0; i < 16; ++i) {
  10234. float w = weight[i];
  10235. float q = 2*Laux[i] + 1;
  10236. sumqx += w*xval[i]*q;
  10237. sumq2 += w*q*q;
  10238. }
  10239. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  10240. scale = sumqx/sumq2; best = scale*sumqx;
  10241. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  10242. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  10243. }
  10244. }
  10245. int n_not_ongrid = 0;
  10246. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  10247. if (n_not_ongrid > 0 && scale > 0) {
  10248. float id = 1/scale;
  10249. for (int k = 0; k < 2; ++k) {
  10250. if (is_on_grid[k]) continue;
  10251. uint16_t u = 0;
  10252. for (int i = 0; i < 8; ++i) {
  10253. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  10254. l = MAX(0, MIN(kMaxQ-1, l));
  10255. u |= (l << 2*i);
  10256. L[8*k + i] = l;
  10257. }
  10258. int grid_index = kmap_q2xs[u];
  10259. if (grid_index < 0) {
  10260. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  10261. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  10262. }
  10263. }
  10264. float sumqx = 0, sumq2 = 0;
  10265. for (int i = 0; i < 16; ++i) {
  10266. float w = weight[i];
  10267. float q = 2*L[i] + 1;
  10268. sumqx += w*xval[i]*q;
  10269. sumq2 += w*q*q;
  10270. }
  10271. if (sumq2 > 0) scale = sumqx/sumq2;
  10272. }
  10273. if (scale < 0) {
  10274. scale = -scale;
  10275. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  10276. }
  10277. for (int k = 0; k < 2; ++k) {
  10278. uint16_t u = 0;
  10279. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  10280. int grid_index = kmap_q2xs[u];
  10281. if (grid_index < 0) {
  10282. printf("Oops: found point %u not on grid:", u);
  10283. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  10284. printf("\n");
  10285. GGML_ASSERT(false);
  10286. }
  10287. const int i8 = 2*ib + k;
  10288. y[ibl].qs[i8] = grid_index & 255;
  10289. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  10290. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  10291. }
  10292. GGML_ASSERT(scale >= 0);
  10293. scales[ib] = scale;
  10294. max_scale = MAX(max_scale, scale);
  10295. }
  10296. if (!max_scale) {
  10297. continue;
  10298. }
  10299. float d = max_scale/31;
  10300. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  10301. float id = 1/d;
  10302. for (int ib = 0; ib < QK_K/16; ++ib) {
  10303. int l = nearest_int(0.5f*(id*scales[ib]-1));
  10304. l = MAX(0, MIN(15, l));
  10305. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  10306. else y[ibl].scales[ib/2] |= (l << 4);
  10307. }
  10308. }
  10309. }
  10310. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  10311. GGML_ASSERT(n_per_row%QK_K == 0);
  10312. int nblock = n_per_row/QK_K;
  10313. char * qrow = (char *)dst;
  10314. for (int row = 0; row < nrow; ++row) {
  10315. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  10316. src += n_per_row;
  10317. qrow += nblock*sizeof(block_iq2_s);
  10318. }
  10319. return nrow * nblock * sizeof(block_iq2_s);
  10320. }
  10321. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int k) {
  10322. assert(k % QK_K == 0);
  10323. quantize_iq2_s(x, y, 1, k, NULL);
  10324. }
  10325. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int k) {
  10326. assert(k % QK_K == 0);
  10327. block_iq2_s * restrict y = vy;
  10328. quantize_row_iq2_s_reference(x, y, k);
  10329. }