ggml-quants.c 487 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067
  1. #include "ggml-quants.h"
  2. #include "ggml-impl.h"
  3. #define GGML_COMMON_IMPL_C
  4. #include "ggml-common.h"
  5. #include <math.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <float.h>
  9. #include <stdlib.h> // for qsort
  10. #include <stdio.h> // for GGML_ASSERT
  11. #ifdef __ARM_NEON
  12. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  13. //
  14. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  15. //
  16. #include <arm_neon.h>
  17. #else
  18. #ifdef __wasm_simd128__
  19. #include <wasm_simd128.h>
  20. #else
  21. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  22. #include <altivec.h>
  23. #undef bool
  24. #define bool _Bool
  25. #else
  26. #if defined(_MSC_VER) || defined(__MINGW32__)
  27. #include <intrin.h>
  28. #else
  29. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  30. #if !defined(__riscv)
  31. #include <immintrin.h>
  32. #endif
  33. #endif
  34. #endif
  35. #endif
  36. #endif
  37. #endif
  38. #ifdef __riscv_v_intrinsic
  39. #include <riscv_vector.h>
  40. #endif
  41. #undef MIN
  42. #undef MAX
  43. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  44. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  45. #define UNUSED GGML_UNUSED
  46. // some compilers don't provide _mm256_set_m128i, e.g. gcc 7
  47. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  48. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  49. // multiply int8_t, add results pairwise twice
  50. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  51. // Get absolute values of x vectors
  52. const __m128i ax = _mm_sign_epi8(x, x);
  53. // Sign the values of the y vectors
  54. const __m128i sy = _mm_sign_epi8(y, x);
  55. // Perform multiplication and create 16-bit values
  56. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  57. const __m128i ones = _mm_set1_epi16(1);
  58. return _mm_madd_epi16(ones, dot);
  59. }
  60. #if __AVX__ || __AVX2__ || __AVX512F__
  61. // horizontally add 8 floats
  62. static inline float hsum_float_8(const __m256 x) {
  63. __m128 res = _mm256_extractf128_ps(x, 1);
  64. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  65. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  66. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  67. return _mm_cvtss_f32(res);
  68. }
  69. // horizontally add 8 int32_t
  70. static inline int hsum_i32_8(const __m256i a) {
  71. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  72. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  73. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  74. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  75. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  76. }
  77. // horizontally add 4 int32_t
  78. static inline int hsum_i32_4(const __m128i a) {
  79. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  80. const __m128i sum64 = _mm_add_epi32(hi64, a);
  81. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  82. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  83. }
  84. #if defined(__AVX2__) || defined(__AVX512F__)
  85. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  86. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  87. uint32_t x32;
  88. memcpy(&x32, x, sizeof(uint32_t));
  89. const __m256i shuf_mask = _mm256_set_epi64x(
  90. 0x0303030303030303, 0x0202020202020202,
  91. 0x0101010101010101, 0x0000000000000000);
  92. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  93. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  94. bytes = _mm256_or_si256(bytes, bit_mask);
  95. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  96. }
  97. // Unpack 32 4-bit fields into 32 bytes
  98. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  99. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  100. {
  101. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  102. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  103. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  104. return _mm256_and_si256(lowMask, bytes);
  105. }
  106. // add int16_t pairwise and return as float vector
  107. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  108. const __m256i ones = _mm256_set1_epi16(1);
  109. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  110. return _mm256_cvtepi32_ps(summed_pairs);
  111. }
  112. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  113. #if __AVXVNNI__
  114. const __m256i zero = _mm256_setzero_si256();
  115. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  116. return _mm256_cvtepi32_ps(summed_pairs);
  117. #else
  118. // Perform multiplication and create 16-bit values
  119. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  120. return sum_i16_pairs_float(dot);
  121. #endif
  122. }
  123. // multiply int8_t, add results pairwise twice and return as float vector
  124. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  125. #if __AVXVNNIINT8__
  126. const __m256i zero = _mm256_setzero_si256();
  127. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  128. return _mm256_cvtepi32_ps(summed_pairs);
  129. #else
  130. // Get absolute values of x vectors
  131. const __m256i ax = _mm256_sign_epi8(x, x);
  132. // Sign the values of the y vectors
  133. const __m256i sy = _mm256_sign_epi8(y, x);
  134. return mul_sum_us8_pairs_float(ax, sy);
  135. #endif
  136. }
  137. static inline __m128i packNibbles( __m256i bytes )
  138. {
  139. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  140. #if __AVX512F__
  141. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  142. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  143. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  144. #else
  145. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  146. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  147. __m256i low = _mm256_and_si256( lowByte, bytes );
  148. high = _mm256_srli_epi16( high, 4 );
  149. bytes = _mm256_or_si256( low, high );
  150. // Compress uint16_t lanes into bytes
  151. __m128i r0 = _mm256_castsi256_si128( bytes );
  152. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  153. return _mm_packus_epi16( r0, r1 );
  154. #endif
  155. }
  156. #elif defined(__AVX__)
  157. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  158. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  159. uint32_t x32;
  160. memcpy(&x32, x, sizeof(uint32_t));
  161. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  162. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  163. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  164. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  165. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  166. bytesl = _mm_or_si128(bytesl, bit_mask);
  167. bytesh = _mm_or_si128(bytesh, bit_mask);
  168. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  169. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  170. return MM256_SET_M128I(bytesh, bytesl);
  171. }
  172. // Unpack 32 4-bit fields into 32 bytes
  173. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  174. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  175. {
  176. // Load 16 bytes from memory
  177. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  178. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  179. const __m128i lowMask = _mm_set1_epi8(0xF);
  180. tmpl = _mm_and_si128(lowMask, tmpl);
  181. tmph = _mm_and_si128(lowMask, tmph);
  182. return MM256_SET_M128I(tmph, tmpl);
  183. }
  184. // add int16_t pairwise and return as float vector
  185. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  186. const __m128i ones = _mm_set1_epi16(1);
  187. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  188. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  189. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  190. return _mm256_cvtepi32_ps(summed_pairs);
  191. }
  192. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  193. const __m128i axl = _mm256_castsi256_si128(ax);
  194. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  195. const __m128i syl = _mm256_castsi256_si128(sy);
  196. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  197. // Perform multiplication and create 16-bit values
  198. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  199. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  200. return sum_i16_pairs_float(doth, dotl);
  201. }
  202. // multiply int8_t, add results pairwise twice and return as float vector
  203. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  204. const __m128i xl = _mm256_castsi256_si128(x);
  205. const __m128i xh = _mm256_extractf128_si256(x, 1);
  206. const __m128i yl = _mm256_castsi256_si128(y);
  207. const __m128i yh = _mm256_extractf128_si256(y, 1);
  208. // Get absolute values of x vectors
  209. const __m128i axl = _mm_sign_epi8(xl, xl);
  210. const __m128i axh = _mm_sign_epi8(xh, xh);
  211. // Sign the values of the y vectors
  212. const __m128i syl = _mm_sign_epi8(yl, xl);
  213. const __m128i syh = _mm_sign_epi8(yh, xh);
  214. // Perform multiplication and create 16-bit values
  215. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  216. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  217. return sum_i16_pairs_float(doth, dotl);
  218. }
  219. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  220. {
  221. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  222. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  223. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  224. __m128i low = _mm_and_si128( lowByte, bytes1 );
  225. high = _mm_srli_epi16( high, 4 );
  226. bytes1 = _mm_or_si128( low, high );
  227. high = _mm_andnot_si128( lowByte, bytes2 );
  228. low = _mm_and_si128( lowByte, bytes2 );
  229. high = _mm_srli_epi16( high, 4 );
  230. bytes2 = _mm_or_si128( low, high );
  231. return _mm_packus_epi16( bytes1, bytes2);
  232. }
  233. #endif
  234. #elif defined(__SSSE3__)
  235. // horizontally add 4x4 floats
  236. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  237. __m128 res_0 =_mm_hadd_ps(a, b);
  238. __m128 res_1 =_mm_hadd_ps(c, d);
  239. __m128 res =_mm_hadd_ps(res_0, res_1);
  240. res =_mm_hadd_ps(res, res);
  241. res =_mm_hadd_ps(res, res);
  242. return _mm_cvtss_f32(res);
  243. }
  244. #endif // __AVX__ || __AVX2__ || __AVX512F__
  245. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  246. #if defined(__ARM_NEON)
  247. #ifdef _MSC_VER
  248. #define ggml_vld1q_u32(w,x,y,z) { ((w) + ((uint64_t)(x) << 32)), ((y) + ((uint64_t)(z) << 32)) }
  249. #else
  250. #define ggml_vld1q_u32(w,x,y,z) { (w), (x), (y), (z) }
  251. #endif
  252. #if !defined(__aarch64__)
  253. // 64-bit compatibility
  254. // vaddvq_s16
  255. // vpaddq_s16
  256. // vpaddq_s32
  257. // vaddvq_s32
  258. // vaddvq_f32
  259. // vmaxvq_f32
  260. // vcvtnq_s32_f32
  261. // vzip1_u8
  262. // vzip2_u8
  263. inline static int32_t vaddvq_s16(int16x8_t v) {
  264. return
  265. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  266. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  267. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  268. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  269. }
  270. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  271. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  272. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  273. return vcombine_s16(a0, b0);
  274. }
  275. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  276. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  277. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  278. return vcombine_s32(a0, b0);
  279. }
  280. inline static int32_t vaddvq_s32(int32x4_t v) {
  281. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  282. }
  283. inline static float vaddvq_f32(float32x4_t v) {
  284. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  285. }
  286. inline static float vmaxvq_f32(float32x4_t v) {
  287. return
  288. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  289. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  290. }
  291. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  292. int32x4_t res;
  293. res[0] = roundf(vgetq_lane_f32(v, 0));
  294. res[1] = roundf(vgetq_lane_f32(v, 1));
  295. res[2] = roundf(vgetq_lane_f32(v, 2));
  296. res[3] = roundf(vgetq_lane_f32(v, 3));
  297. return res;
  298. }
  299. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  300. uint8x8_t res;
  301. res[0] = a[0]; res[1] = b[0];
  302. res[2] = a[1]; res[3] = b[1];
  303. res[4] = a[2]; res[5] = b[2];
  304. res[6] = a[3]; res[7] = b[3];
  305. return res;
  306. }
  307. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  308. uint8x8_t res;
  309. res[0] = a[4]; res[1] = b[4];
  310. res[2] = a[5]; res[3] = b[5];
  311. res[4] = a[6]; res[5] = b[6];
  312. res[6] = a[7]; res[7] = b[7];
  313. return res;
  314. }
  315. // vld1q_s16_x2
  316. // vld1q_u8_x2
  317. // vld1q_u8_x4
  318. // vld1q_s8_x2
  319. // vld1q_s8_x4
  320. // TODO: double-check these work correctly
  321. typedef struct ggml_int16x8x2_t {
  322. int16x8_t val[2];
  323. } ggml_int16x8x2_t;
  324. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  325. ggml_int16x8x2_t res;
  326. res.val[0] = vld1q_s16(ptr + 0);
  327. res.val[1] = vld1q_s16(ptr + 8);
  328. return res;
  329. }
  330. typedef struct ggml_uint8x16x2_t {
  331. uint8x16_t val[2];
  332. } ggml_uint8x16x2_t;
  333. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  334. ggml_uint8x16x2_t res;
  335. res.val[0] = vld1q_u8(ptr + 0);
  336. res.val[1] = vld1q_u8(ptr + 16);
  337. return res;
  338. }
  339. typedef struct ggml_uint8x16x4_t {
  340. uint8x16_t val[4];
  341. } ggml_uint8x16x4_t;
  342. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  343. ggml_uint8x16x4_t res;
  344. res.val[0] = vld1q_u8(ptr + 0);
  345. res.val[1] = vld1q_u8(ptr + 16);
  346. res.val[2] = vld1q_u8(ptr + 32);
  347. res.val[3] = vld1q_u8(ptr + 48);
  348. return res;
  349. }
  350. typedef struct ggml_int8x16x2_t {
  351. int8x16_t val[2];
  352. } ggml_int8x16x2_t;
  353. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  354. ggml_int8x16x2_t res;
  355. res.val[0] = vld1q_s8(ptr + 0);
  356. res.val[1] = vld1q_s8(ptr + 16);
  357. return res;
  358. }
  359. typedef struct ggml_int8x16x4_t {
  360. int8x16_t val[4];
  361. } ggml_int8x16x4_t;
  362. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  363. ggml_int8x16x4_t res;
  364. res.val[0] = vld1q_s8(ptr + 0);
  365. res.val[1] = vld1q_s8(ptr + 16);
  366. res.val[2] = vld1q_s8(ptr + 32);
  367. res.val[3] = vld1q_s8(ptr + 48);
  368. return res;
  369. }
  370. // NOTE: not tested
  371. inline static int8x16_t ggml_vqtbl1q_s8(int8x16_t a, uint8x16_t b) {
  372. int8x16_t res;
  373. res[ 0] = a[b[ 0]];
  374. res[ 1] = a[b[ 1]];
  375. res[ 2] = a[b[ 2]];
  376. res[ 3] = a[b[ 3]];
  377. res[ 4] = a[b[ 4]];
  378. res[ 5] = a[b[ 5]];
  379. res[ 6] = a[b[ 6]];
  380. res[ 7] = a[b[ 7]];
  381. res[ 8] = a[b[ 8]];
  382. res[ 9] = a[b[ 9]];
  383. res[10] = a[b[10]];
  384. res[11] = a[b[11]];
  385. res[12] = a[b[12]];
  386. res[13] = a[b[13]];
  387. res[14] = a[b[14]];
  388. res[15] = a[b[15]];
  389. return res;
  390. }
  391. // NOTE: not tested
  392. inline static uint8x16_t ggml_vqtbl1q_u8(uint8x16_t a, uint8x16_t b) {
  393. uint8x16_t res;
  394. res[ 0] = a[b[ 0]];
  395. res[ 1] = a[b[ 1]];
  396. res[ 2] = a[b[ 2]];
  397. res[ 3] = a[b[ 3]];
  398. res[ 4] = a[b[ 4]];
  399. res[ 5] = a[b[ 5]];
  400. res[ 6] = a[b[ 6]];
  401. res[ 7] = a[b[ 7]];
  402. res[ 8] = a[b[ 8]];
  403. res[ 9] = a[b[ 9]];
  404. res[10] = a[b[10]];
  405. res[11] = a[b[11]];
  406. res[12] = a[b[12]];
  407. res[13] = a[b[13]];
  408. res[14] = a[b[14]];
  409. res[15] = a[b[15]];
  410. return res;
  411. }
  412. #else
  413. #define ggml_int16x8x2_t int16x8x2_t
  414. #define ggml_uint8x16x2_t uint8x16x2_t
  415. #define ggml_uint8x16x4_t uint8x16x4_t
  416. #define ggml_int8x16x2_t int8x16x2_t
  417. #define ggml_int8x16x4_t int8x16x4_t
  418. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  419. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  420. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  421. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  422. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  423. #define ggml_vqtbl1q_s8 vqtbl1q_s8
  424. #define ggml_vqtbl1q_u8 vqtbl1q_u8
  425. #endif
  426. #if !defined(__ARM_FEATURE_DOTPROD)
  427. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  428. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  429. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  430. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  431. }
  432. #else
  433. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  434. #endif
  435. #endif
  436. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  437. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  438. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  439. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  440. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  441. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  442. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  443. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  444. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  445. // precomputed tables for expanding 8bits to 8 bytes:
  446. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  447. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  448. #endif
  449. // reference implementation for deterministic creation of model files
  450. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  451. static const int qk = QK4_0;
  452. assert(k % qk == 0);
  453. const int nb = k / qk;
  454. for (int i = 0; i < nb; i++) {
  455. float amax = 0.0f; // absolute max
  456. float max = 0.0f;
  457. for (int j = 0; j < qk; j++) {
  458. const float v = x[i*qk + j];
  459. if (amax < fabsf(v)) {
  460. amax = fabsf(v);
  461. max = v;
  462. }
  463. }
  464. const float d = max / -8;
  465. const float id = d ? 1.0f/d : 0.0f;
  466. y[i].d = GGML_FP32_TO_FP16(d);
  467. for (int j = 0; j < qk/2; ++j) {
  468. const float x0 = x[i*qk + 0 + j]*id;
  469. const float x1 = x[i*qk + qk/2 + j]*id;
  470. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  471. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  472. y[i].qs[j] = xi0;
  473. y[i].qs[j] |= xi1 << 4;
  474. }
  475. }
  476. }
  477. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  478. quantize_row_q4_0_reference(x, y, k);
  479. }
  480. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  481. const int qk = QK4_1;
  482. assert(k % qk == 0);
  483. const int nb = k / qk;
  484. for (int i = 0; i < nb; i++) {
  485. float min = FLT_MAX;
  486. float max = -FLT_MAX;
  487. for (int j = 0; j < qk; j++) {
  488. const float v = x[i*qk + j];
  489. if (v < min) min = v;
  490. if (v > max) max = v;
  491. }
  492. const float d = (max - min) / ((1 << 4) - 1);
  493. const float id = d ? 1.0f/d : 0.0f;
  494. y[i].d = GGML_FP32_TO_FP16(d);
  495. y[i].m = GGML_FP32_TO_FP16(min);
  496. for (int j = 0; j < qk/2; ++j) {
  497. const float x0 = (x[i*qk + 0 + j] - min)*id;
  498. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  499. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  500. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  501. y[i].qs[j] = xi0;
  502. y[i].qs[j] |= xi1 << 4;
  503. }
  504. }
  505. }
  506. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  507. quantize_row_q4_1_reference(x, y, k);
  508. }
  509. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  510. static const int qk = QK5_0;
  511. assert(k % qk == 0);
  512. const int nb = k / qk;
  513. for (int i = 0; i < nb; i++) {
  514. float amax = 0.0f; // absolute max
  515. float max = 0.0f;
  516. for (int j = 0; j < qk; j++) {
  517. const float v = x[i*qk + j];
  518. if (amax < fabsf(v)) {
  519. amax = fabsf(v);
  520. max = v;
  521. }
  522. }
  523. const float d = max / -16;
  524. const float id = d ? 1.0f/d : 0.0f;
  525. y[i].d = GGML_FP32_TO_FP16(d);
  526. uint32_t qh = 0;
  527. for (int j = 0; j < qk/2; ++j) {
  528. const float x0 = x[i*qk + 0 + j]*id;
  529. const float x1 = x[i*qk + qk/2 + j]*id;
  530. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  531. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  532. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  533. // get the 5-th bit and store it in qh at the right position
  534. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  535. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  536. }
  537. memcpy(&y[i].qh, &qh, sizeof(qh));
  538. }
  539. }
  540. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  541. quantize_row_q5_0_reference(x, y, k);
  542. }
  543. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  544. const int qk = QK5_1;
  545. assert(k % qk == 0);
  546. const int nb = k / qk;
  547. for (int i = 0; i < nb; i++) {
  548. float min = FLT_MAX;
  549. float max = -FLT_MAX;
  550. for (int j = 0; j < qk; j++) {
  551. const float v = x[i*qk + j];
  552. if (v < min) min = v;
  553. if (v > max) max = v;
  554. }
  555. const float d = (max - min) / ((1 << 5) - 1);
  556. const float id = d ? 1.0f/d : 0.0f;
  557. y[i].d = GGML_FP32_TO_FP16(d);
  558. y[i].m = GGML_FP32_TO_FP16(min);
  559. uint32_t qh = 0;
  560. for (int j = 0; j < qk/2; ++j) {
  561. const float x0 = (x[i*qk + 0 + j] - min)*id;
  562. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  563. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  564. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  565. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  566. // get the 5-th bit and store it in qh at the right position
  567. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  568. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  569. }
  570. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  571. }
  572. }
  573. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  574. quantize_row_q5_1_reference(x, y, k);
  575. }
  576. // reference implementation for deterministic creation of model files
  577. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  578. assert(k % QK8_0 == 0);
  579. const int nb = k / QK8_0;
  580. for (int i = 0; i < nb; i++) {
  581. float amax = 0.0f; // absolute max
  582. for (int j = 0; j < QK8_0; j++) {
  583. const float v = x[i*QK8_0 + j];
  584. amax = MAX(amax, fabsf(v));
  585. }
  586. const float d = amax / ((1 << 7) - 1);
  587. const float id = d ? 1.0f/d : 0.0f;
  588. y[i].d = GGML_FP32_TO_FP16(d);
  589. for (int j = 0; j < QK8_0; ++j) {
  590. const float x0 = x[i*QK8_0 + j]*id;
  591. y[i].qs[j] = roundf(x0);
  592. }
  593. }
  594. }
  595. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  596. assert(QK8_0 == 32);
  597. assert(k % QK8_0 == 0);
  598. const int nb = k / QK8_0;
  599. block_q8_0 * restrict y = vy;
  600. #if defined(__ARM_NEON)
  601. for (int i = 0; i < nb; i++) {
  602. float32x4_t srcv [8];
  603. float32x4_t asrcv[8];
  604. float32x4_t amaxv[8];
  605. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  606. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  607. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  608. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  609. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  610. const float amax = vmaxvq_f32(amaxv[0]);
  611. const float d = amax / ((1 << 7) - 1);
  612. const float id = d ? 1.0f/d : 0.0f;
  613. y[i].d = GGML_FP32_TO_FP16(d);
  614. for (int j = 0; j < 8; j++) {
  615. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  616. const int32x4_t vi = vcvtnq_s32_f32(v);
  617. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  618. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  619. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  620. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  621. }
  622. }
  623. #elif defined(__wasm_simd128__)
  624. for (int i = 0; i < nb; i++) {
  625. v128_t srcv [8];
  626. v128_t asrcv[8];
  627. v128_t amaxv[8];
  628. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  629. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  630. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  631. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  632. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  633. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  634. wasm_f32x4_extract_lane(amaxv[0], 1)),
  635. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  636. wasm_f32x4_extract_lane(amaxv[0], 3)));
  637. const float d = amax / ((1 << 7) - 1);
  638. const float id = d ? 1.0f/d : 0.0f;
  639. y[i].d = GGML_FP32_TO_FP16(d);
  640. for (int j = 0; j < 8; j++) {
  641. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  642. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  643. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  644. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  645. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  646. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  647. }
  648. }
  649. #elif defined(__AVX2__) || defined(__AVX__)
  650. for (int i = 0; i < nb; i++) {
  651. // Load elements into 4 AVX vectors
  652. __m256 v0 = _mm256_loadu_ps( x );
  653. __m256 v1 = _mm256_loadu_ps( x + 8 );
  654. __m256 v2 = _mm256_loadu_ps( x + 16 );
  655. __m256 v3 = _mm256_loadu_ps( x + 24 );
  656. x += 32;
  657. // Compute max(abs(e)) for the block
  658. const __m256 signBit = _mm256_set1_ps( -0.0f );
  659. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  660. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  661. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  662. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  663. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  664. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  665. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  666. const float maxScalar = _mm_cvtss_f32( max4 );
  667. // Quantize these floats
  668. const float d = maxScalar / 127.f;
  669. y[i].d = GGML_FP32_TO_FP16(d);
  670. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  671. const __m256 mul = _mm256_set1_ps( id );
  672. // Apply the multiplier
  673. v0 = _mm256_mul_ps( v0, mul );
  674. v1 = _mm256_mul_ps( v1, mul );
  675. v2 = _mm256_mul_ps( v2, mul );
  676. v3 = _mm256_mul_ps( v3, mul );
  677. // Round to nearest integer
  678. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  679. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  680. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  681. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  682. // Convert floats to integers
  683. __m256i i0 = _mm256_cvtps_epi32( v0 );
  684. __m256i i1 = _mm256_cvtps_epi32( v1 );
  685. __m256i i2 = _mm256_cvtps_epi32( v2 );
  686. __m256i i3 = _mm256_cvtps_epi32( v3 );
  687. #if defined(__AVX2__)
  688. // Convert int32 to int16
  689. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  690. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  691. // Convert int16 to int8
  692. 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
  693. // We got our precious signed bytes, but the order is now wrong
  694. // These AVX2 pack instructions process 16-byte pieces independently
  695. // The following instruction is fixing the order
  696. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  697. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  698. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  699. #else
  700. // Since we don't have in AVX some necessary functions,
  701. // we split the registers in half and call AVX2 analogs from SSE
  702. __m128i ni0 = _mm256_castsi256_si128( i0 );
  703. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  704. __m128i ni2 = _mm256_castsi256_si128( i1 );
  705. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  706. __m128i ni4 = _mm256_castsi256_si128( i2 );
  707. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  708. __m128i ni6 = _mm256_castsi256_si128( i3 );
  709. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  710. // Convert int32 to int16
  711. ni0 = _mm_packs_epi32( ni0, ni1 );
  712. ni2 = _mm_packs_epi32( ni2, ni3 );
  713. ni4 = _mm_packs_epi32( ni4, ni5 );
  714. ni6 = _mm_packs_epi32( ni6, ni7 );
  715. // Convert int16 to int8
  716. ni0 = _mm_packs_epi16( ni0, ni2 );
  717. ni4 = _mm_packs_epi16( ni4, ni6 );
  718. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  719. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  720. #endif
  721. }
  722. #elif defined(__riscv_v_intrinsic)
  723. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  724. for (int i = 0; i < nb; i++) {
  725. // load elements
  726. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  727. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  728. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  729. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  730. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  731. const float d = amax / ((1 << 7) - 1);
  732. const float id = d ? 1.0f/d : 0.0f;
  733. y[i].d = GGML_FP32_TO_FP16(d);
  734. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  735. // convert to integer
  736. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  737. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  738. // store result
  739. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  740. }
  741. #else
  742. GGML_UNUSED(nb);
  743. // scalar
  744. quantize_row_q8_0_reference(x, y, k);
  745. #endif
  746. }
  747. // reference implementation for deterministic creation of model files
  748. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  749. assert(QK8_1 == 32);
  750. assert(k % QK8_1 == 0);
  751. const int nb = k / QK8_1;
  752. for (int i = 0; i < nb; i++) {
  753. float amax = 0.0f; // absolute max
  754. for (int j = 0; j < QK8_1; j++) {
  755. const float v = x[i*QK8_1 + j];
  756. amax = MAX(amax, fabsf(v));
  757. }
  758. const float d = amax / ((1 << 7) - 1);
  759. const float id = d ? 1.0f/d : 0.0f;
  760. y[i].d = d;
  761. int sum = 0;
  762. for (int j = 0; j < QK8_1/2; ++j) {
  763. const float v0 = x[i*QK8_1 + j]*id;
  764. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  765. y[i].qs[ j] = roundf(v0);
  766. y[i].qs[QK8_1/2 + j] = roundf(v1);
  767. sum += y[i].qs[ j];
  768. sum += y[i].qs[QK8_1/2 + j];
  769. }
  770. y[i].s = sum*d;
  771. }
  772. }
  773. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  774. assert(k % QK8_1 == 0);
  775. const int nb = k / QK8_1;
  776. block_q8_1 * restrict y = vy;
  777. #if defined(__ARM_NEON)
  778. for (int i = 0; i < nb; i++) {
  779. float32x4_t srcv [8];
  780. float32x4_t asrcv[8];
  781. float32x4_t amaxv[8];
  782. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  783. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  784. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  785. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  786. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  787. const float amax = vmaxvq_f32(amaxv[0]);
  788. const float d = amax / ((1 << 7) - 1);
  789. const float id = d ? 1.0f/d : 0.0f;
  790. y[i].d = d;
  791. int32x4_t accv = vdupq_n_s32(0);
  792. for (int j = 0; j < 8; j++) {
  793. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  794. const int32x4_t vi = vcvtnq_s32_f32(v);
  795. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  796. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  797. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  798. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  799. accv = vaddq_s32(accv, vi);
  800. }
  801. y[i].s = d * vaddvq_s32(accv);
  802. }
  803. #elif defined(__wasm_simd128__)
  804. for (int i = 0; i < nb; i++) {
  805. v128_t srcv [8];
  806. v128_t asrcv[8];
  807. v128_t amaxv[8];
  808. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  809. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  810. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  811. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  812. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  813. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  814. wasm_f32x4_extract_lane(amaxv[0], 1)),
  815. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  816. wasm_f32x4_extract_lane(amaxv[0], 3)));
  817. const float d = amax / ((1 << 7) - 1);
  818. const float id = d ? 1.0f/d : 0.0f;
  819. y[i].d = d;
  820. v128_t accv = wasm_i32x4_splat(0);
  821. for (int j = 0; j < 8; j++) {
  822. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  823. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  824. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  825. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  826. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  827. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  828. accv = wasm_i32x4_add(accv, vi);
  829. }
  830. y[i].s = d * (wasm_i32x4_extract_lane(accv, 0) +
  831. wasm_i32x4_extract_lane(accv, 1) +
  832. wasm_i32x4_extract_lane(accv, 2) +
  833. wasm_i32x4_extract_lane(accv, 3));
  834. }
  835. #elif defined(__AVX2__) || defined(__AVX__)
  836. for (int i = 0; i < nb; i++) {
  837. // Load elements into 4 AVX vectors
  838. __m256 v0 = _mm256_loadu_ps( x );
  839. __m256 v1 = _mm256_loadu_ps( x + 8 );
  840. __m256 v2 = _mm256_loadu_ps( x + 16 );
  841. __m256 v3 = _mm256_loadu_ps( x + 24 );
  842. x += 32;
  843. // Compute max(abs(e)) for the block
  844. const __m256 signBit = _mm256_set1_ps( -0.0f );
  845. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  846. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  847. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  848. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  849. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  850. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  851. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  852. const float maxScalar = _mm_cvtss_f32( max4 );
  853. // Quantize these floats
  854. const float d = maxScalar / 127.f;
  855. y[i].d = d;
  856. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  857. const __m256 mul = _mm256_set1_ps( id );
  858. // Apply the multiplier
  859. v0 = _mm256_mul_ps( v0, mul );
  860. v1 = _mm256_mul_ps( v1, mul );
  861. v2 = _mm256_mul_ps( v2, mul );
  862. v3 = _mm256_mul_ps( v3, mul );
  863. // Round to nearest integer
  864. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  865. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  866. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  867. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  868. // Convert floats to integers
  869. __m256i i0 = _mm256_cvtps_epi32( v0 );
  870. __m256i i1 = _mm256_cvtps_epi32( v1 );
  871. __m256i i2 = _mm256_cvtps_epi32( v2 );
  872. __m256i i3 = _mm256_cvtps_epi32( v3 );
  873. #if defined(__AVX2__)
  874. // Compute the sum of the quants and set y[i].s
  875. y[i].s = d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)));
  876. // Convert int32 to int16
  877. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  878. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  879. // Convert int16 to int8
  880. 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
  881. // We got our precious signed bytes, but the order is now wrong
  882. // These AVX2 pack instructions process 16-byte pieces independently
  883. // The following instruction is fixing the order
  884. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  885. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  886. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  887. #else
  888. // Since we don't have in AVX some necessary functions,
  889. // we split the registers in half and call AVX2 analogs from SSE
  890. __m128i ni0 = _mm256_castsi256_si128( i0 );
  891. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  892. __m128i ni2 = _mm256_castsi256_si128( i1 );
  893. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  894. __m128i ni4 = _mm256_castsi256_si128( i2 );
  895. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  896. __m128i ni6 = _mm256_castsi256_si128( i3 );
  897. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  898. // Compute the sum of the quants and set y[i].s
  899. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  900. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  901. y[i].s = d * hsum_i32_4(_mm_add_epi32(s0, s1));
  902. // Convert int32 to int16
  903. ni0 = _mm_packs_epi32( ni0, ni1 );
  904. ni2 = _mm_packs_epi32( ni2, ni3 );
  905. ni4 = _mm_packs_epi32( ni4, ni5 );
  906. ni6 = _mm_packs_epi32( ni6, ni7 );
  907. // Convert int16 to int8
  908. ni0 = _mm_packs_epi16( ni0, ni2 );
  909. ni4 = _mm_packs_epi16( ni4, ni6 );
  910. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  911. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  912. #endif
  913. }
  914. #elif defined(__riscv_v_intrinsic)
  915. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  916. for (int i = 0; i < nb; i++) {
  917. // load elements
  918. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  919. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  920. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  921. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  922. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  923. const float d = amax / ((1 << 7) - 1);
  924. const float id = d ? 1.0f/d : 0.0f;
  925. y[i].d = d;
  926. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  927. // convert to integer
  928. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  929. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  930. // store result
  931. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  932. // compute sum for y[i].s
  933. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  934. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  935. // set y[i].s
  936. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  937. y[i].s = sum*d;
  938. }
  939. #else
  940. GGML_UNUSED(nb);
  941. // scalar
  942. quantize_row_q8_1_reference(x, y, k);
  943. #endif
  944. }
  945. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  946. static const int qk = QK4_0;
  947. assert(k % qk == 0);
  948. const int nb = k / qk;
  949. for (int i = 0; i < nb; i++) {
  950. const float d = GGML_FP16_TO_FP32(x[i].d);
  951. for (int j = 0; j < qk/2; ++j) {
  952. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  953. const int x1 = (x[i].qs[j] >> 4) - 8;
  954. y[i*qk + j + 0 ] = x0*d;
  955. y[i*qk + j + qk/2] = x1*d;
  956. }
  957. }
  958. }
  959. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  960. static const int qk = QK4_1;
  961. assert(k % qk == 0);
  962. const int nb = k / qk;
  963. for (int i = 0; i < nb; i++) {
  964. const float d = GGML_FP16_TO_FP32(x[i].d);
  965. const float m = GGML_FP16_TO_FP32(x[i].m);
  966. for (int j = 0; j < qk/2; ++j) {
  967. const int x0 = (x[i].qs[j] & 0x0F);
  968. const int x1 = (x[i].qs[j] >> 4);
  969. y[i*qk + j + 0 ] = x0*d + m;
  970. y[i*qk + j + qk/2] = x1*d + m;
  971. }
  972. }
  973. }
  974. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  975. static const int qk = QK5_0;
  976. assert(k % qk == 0);
  977. const int nb = k / qk;
  978. for (int i = 0; i < nb; i++) {
  979. const float d = GGML_FP16_TO_FP32(x[i].d);
  980. uint32_t qh;
  981. memcpy(&qh, x[i].qh, sizeof(qh));
  982. for (int j = 0; j < qk/2; ++j) {
  983. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  984. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  985. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  986. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  987. y[i*qk + j + 0 ] = x0*d;
  988. y[i*qk + j + qk/2] = x1*d;
  989. }
  990. }
  991. }
  992. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  993. static const int qk = QK5_1;
  994. assert(k % qk == 0);
  995. const int nb = k / qk;
  996. for (int i = 0; i < nb; i++) {
  997. const float d = GGML_FP16_TO_FP32(x[i].d);
  998. const float m = GGML_FP16_TO_FP32(x[i].m);
  999. uint32_t qh;
  1000. memcpy(&qh, x[i].qh, sizeof(qh));
  1001. for (int j = 0; j < qk/2; ++j) {
  1002. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  1003. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  1004. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  1005. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  1006. y[i*qk + j + 0 ] = x0*d + m;
  1007. y[i*qk + j + qk/2] = x1*d + m;
  1008. }
  1009. }
  1010. }
  1011. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  1012. static const int qk = QK8_0;
  1013. assert(k % qk == 0);
  1014. const int nb = k / qk;
  1015. for (int i = 0; i < nb; i++) {
  1016. const float d = GGML_FP16_TO_FP32(x[i].d);
  1017. for (int j = 0; j < qk; ++j) {
  1018. y[i*qk + j] = x[i].qs[j]*d;
  1019. }
  1020. }
  1021. }
  1022. //
  1023. // 2-6 bit quantization in super-blocks
  1024. //
  1025. //
  1026. // ===================== Helper functions
  1027. //
  1028. static inline int nearest_int(float fval) {
  1029. assert(fval <= 4194303.f);
  1030. float val = fval + 12582912.f;
  1031. int i; memcpy(&i, &val, sizeof(int));
  1032. return (i & 0x007fffff) - 0x00400000;
  1033. }
  1034. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  1035. const float * restrict qw) {
  1036. float max = 0;
  1037. float amax = 0;
  1038. for (int i = 0; i < n; ++i) {
  1039. float ax = fabsf(x[i]);
  1040. if (ax > amax) { amax = ax; max = x[i]; }
  1041. }
  1042. if (amax < 1e-30f) { // all zero
  1043. for (int i = 0; i < n; ++i) {
  1044. L[i] = 0;
  1045. }
  1046. return 0.f;
  1047. }
  1048. float iscale = -nmax / max;
  1049. if (rmse_type == 0) {
  1050. for (int i = 0; i < n; ++i) {
  1051. int l = nearest_int(iscale * x[i]);
  1052. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1053. }
  1054. return 1/iscale;
  1055. }
  1056. bool return_early = false;
  1057. if (rmse_type < 0) {
  1058. rmse_type = -rmse_type;
  1059. return_early = true;
  1060. }
  1061. float sumlx = 0;
  1062. float suml2 = 0;
  1063. #ifdef HAVE_BUGGY_APPLE_LINKER
  1064. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1065. for (volatile int i = 0; i < n; ++i) {
  1066. #else
  1067. for (int i = 0; i < n; ++i) {
  1068. #endif
  1069. int l = nearest_int(iscale * x[i]);
  1070. l = MAX(-nmax, MIN(nmax-1, l));
  1071. L[i] = l + nmax;
  1072. 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]));
  1073. sumlx += w*x[i]*l;
  1074. suml2 += w*l*l;
  1075. }
  1076. float scale = sumlx/suml2;
  1077. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1078. float best = scale * sumlx;
  1079. for (int is = -9; is <= 9; ++is) {
  1080. if (is == 0) {
  1081. continue;
  1082. }
  1083. iscale = -(nmax + 0.1f*is) / max;
  1084. sumlx = suml2 = 0;
  1085. for (int i = 0; i < n; ++i) {
  1086. int l = nearest_int(iscale * x[i]);
  1087. l = MAX(-nmax, MIN(nmax-1, l));
  1088. 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]));
  1089. sumlx += w*x[i]*l;
  1090. suml2 += w*l*l;
  1091. }
  1092. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1093. for (int i = 0; i < n; ++i) {
  1094. int l = nearest_int(iscale * x[i]);
  1095. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1096. }
  1097. scale = sumlx/suml2; best = scale*sumlx;
  1098. }
  1099. }
  1100. return scale;
  1101. }
  1102. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1103. float max = 0;
  1104. float amax = 0;
  1105. for (int i = 0; i < n; ++i) {
  1106. float ax = fabsf(x[i]);
  1107. if (ax > amax) { amax = ax; max = x[i]; }
  1108. }
  1109. if (!amax) { // all zero
  1110. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1111. return 0.f;
  1112. }
  1113. float iscale = -nmax / max;
  1114. if (do_rmse) {
  1115. float sumlx = 0;
  1116. float suml2 = 0;
  1117. for (int i = 0; i < n; ++i) {
  1118. int l = nearest_int(iscale * x[i]);
  1119. l = MAX(-nmax, MIN(nmax-1, l));
  1120. L[i] = l;
  1121. float w = x[i]*x[i];
  1122. sumlx += w*x[i]*l;
  1123. suml2 += w*l*l;
  1124. }
  1125. for (int itry = 0; itry < 5; ++itry) {
  1126. int n_changed = 0;
  1127. for (int i = 0; i < n; ++i) {
  1128. float w = x[i]*x[i];
  1129. float slx = sumlx - w*x[i]*L[i];
  1130. if (slx > 0) {
  1131. float sl2 = suml2 - w*L[i]*L[i];
  1132. int new_l = nearest_int(x[i] * sl2 / slx);
  1133. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1134. if (new_l != L[i]) {
  1135. slx += w*x[i]*new_l;
  1136. sl2 += w*new_l*new_l;
  1137. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1138. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1139. ++n_changed;
  1140. }
  1141. }
  1142. }
  1143. }
  1144. if (!n_changed) {
  1145. break;
  1146. }
  1147. }
  1148. for (int i = 0; i < n; ++i) {
  1149. L[i] += nmax;
  1150. }
  1151. return sumlx / suml2;
  1152. }
  1153. for (int i = 0; i < n; ++i) {
  1154. int l = nearest_int(iscale * x[i]);
  1155. l = MAX(-nmax, MIN(nmax-1, l));
  1156. L[i] = l + nmax;
  1157. }
  1158. return 1/iscale;
  1159. }
  1160. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1161. int ntry, float alpha) {
  1162. float min = x[0];
  1163. float max = x[0];
  1164. for (int i = 1; i < n; ++i) {
  1165. if (x[i] < min) min = x[i];
  1166. if (x[i] > max) max = x[i];
  1167. }
  1168. if (max == min) {
  1169. for (int i = 0; i < n; ++i) L[i] = 0;
  1170. *the_min = 0;
  1171. return 0.f;
  1172. }
  1173. if (min > 0) min = 0;
  1174. float iscale = nmax/(max - min);
  1175. float scale = 1/iscale;
  1176. for (int itry = 0; itry < ntry; ++itry) {
  1177. float sumlx = 0; int suml2 = 0;
  1178. bool did_change = false;
  1179. for (int i = 0; i < n; ++i) {
  1180. int l = nearest_int(iscale*(x[i] - min));
  1181. l = MAX(0, MIN(nmax, l));
  1182. if (l != L[i]) {
  1183. L[i] = l;
  1184. did_change = true;
  1185. }
  1186. sumlx += (x[i] - min)*l;
  1187. suml2 += l*l;
  1188. }
  1189. scale = sumlx/suml2;
  1190. float sum = 0;
  1191. for (int i = 0; i < n; ++i) {
  1192. sum += x[i] - scale*L[i];
  1193. }
  1194. min = alpha*min + (1 - alpha)*sum/n;
  1195. if (min > 0) min = 0;
  1196. iscale = 1/scale;
  1197. if (!did_change) break;
  1198. }
  1199. *the_min = -min;
  1200. return scale;
  1201. }
  1202. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1203. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1204. float rmin, float rdelta, int nstep, bool use_mad) {
  1205. float min = x[0];
  1206. float max = x[0];
  1207. float sum_w = weights[0];
  1208. float sum_x = sum_w * x[0];
  1209. #ifdef HAVE_BUGGY_APPLE_LINKER
  1210. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1211. for (volatile int i = 1; i < n; ++i) {
  1212. #else
  1213. for (int i = 1; i < n; ++i) {
  1214. #endif
  1215. if (x[i] < min) min = x[i];
  1216. if (x[i] > max) max = x[i];
  1217. float w = weights[i];
  1218. sum_w += w;
  1219. sum_x += w * x[i];
  1220. }
  1221. if (min > 0) min = 0;
  1222. if (max == min) {
  1223. for (int i = 0; i < n; ++i) L[i] = 0;
  1224. *the_min = -min;
  1225. return 0.f;
  1226. }
  1227. float iscale = nmax/(max - min);
  1228. float scale = 1/iscale;
  1229. float best_mad = 0;
  1230. for (int i = 0; i < n; ++i) {
  1231. int l = nearest_int(iscale*(x[i] - min));
  1232. L[i] = MAX(0, MIN(nmax, l));
  1233. float diff = scale * L[i] + min - x[i];
  1234. diff = use_mad ? fabsf(diff) : diff * diff;
  1235. float w = weights[i];
  1236. best_mad += w * diff;
  1237. }
  1238. if (nstep < 1) {
  1239. *the_min = -min;
  1240. return scale;
  1241. }
  1242. for (int is = 0; is <= nstep; ++is) {
  1243. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1244. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1245. for (int i = 0; i < n; ++i) {
  1246. int l = nearest_int(iscale*(x[i] - min));
  1247. l = MAX(0, MIN(nmax, l));
  1248. Laux[i] = l;
  1249. float w = weights[i];
  1250. sum_l += w*l;
  1251. sum_l2 += w*l*l;
  1252. sum_xl += w*l*x[i];
  1253. }
  1254. float D = sum_w * sum_l2 - sum_l * sum_l;
  1255. if (D > 0) {
  1256. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1257. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1258. if (this_min > 0) {
  1259. this_min = 0;
  1260. this_scale = sum_xl / sum_l2;
  1261. }
  1262. float mad = 0;
  1263. for (int i = 0; i < n; ++i) {
  1264. float diff = this_scale * Laux[i] + this_min - x[i];
  1265. diff = use_mad ? fabsf(diff) : diff * diff;
  1266. float w = weights[i];
  1267. mad += w * diff;
  1268. }
  1269. if (mad < best_mad) {
  1270. for (int i = 0; i < n; ++i) {
  1271. L[i] = Laux[i];
  1272. }
  1273. best_mad = mad;
  1274. scale = this_scale;
  1275. min = this_min;
  1276. }
  1277. }
  1278. }
  1279. *the_min = -min;
  1280. return scale;
  1281. }
  1282. #if QK_K == 256
  1283. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1284. if (j < 4) {
  1285. *d = q[j] & 63; *m = q[j + 4] & 63;
  1286. } else {
  1287. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1288. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1289. }
  1290. }
  1291. #endif
  1292. //========================- 2-bit (de)-quantization
  1293. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1294. assert(k % QK_K == 0);
  1295. const int nb = k / QK_K;
  1296. uint8_t L[QK_K];
  1297. uint8_t Laux[16];
  1298. float weights[16];
  1299. float mins[QK_K/16];
  1300. float scales[QK_K/16];
  1301. const float q4scale = 15.f;
  1302. for (int i = 0; i < nb; i++) {
  1303. float max_scale = 0; // as we are deducting the min, scales are always positive
  1304. float max_min = 0;
  1305. for (int j = 0; j < QK_K/16; ++j) {
  1306. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1307. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1308. float scale = scales[j];
  1309. if (scale > max_scale) {
  1310. max_scale = scale;
  1311. }
  1312. float min = mins[j];
  1313. if (min > max_min) {
  1314. max_min = min;
  1315. }
  1316. }
  1317. if (max_scale > 0) {
  1318. float iscale = q4scale/max_scale;
  1319. for (int j = 0; j < QK_K/16; ++j) {
  1320. int l = nearest_int(iscale*scales[j]);
  1321. y[i].scales[j] = l;
  1322. }
  1323. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1324. } else {
  1325. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1326. y[i].d = GGML_FP32_TO_FP16(0.f);
  1327. }
  1328. if (max_min > 0) {
  1329. float iscale = q4scale/max_min;
  1330. for (int j = 0; j < QK_K/16; ++j) {
  1331. int l = nearest_int(iscale*mins[j]);
  1332. y[i].scales[j] |= (l << 4);
  1333. }
  1334. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1335. } else {
  1336. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1337. }
  1338. for (int j = 0; j < QK_K/16; ++j) {
  1339. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1340. if (!d) continue;
  1341. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1342. for (int ii = 0; ii < 16; ++ii) {
  1343. int l = nearest_int((x[16*j + ii] + dm)/d);
  1344. l = MAX(0, MIN(3, l));
  1345. L[16*j + ii] = l;
  1346. }
  1347. }
  1348. #if QK_K == 256
  1349. for (int j = 0; j < QK_K; j += 128) {
  1350. for (int l = 0; l < 32; ++l) {
  1351. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1352. }
  1353. }
  1354. #else
  1355. for (int l = 0; l < 16; ++l) {
  1356. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1357. }
  1358. #endif
  1359. x += QK_K;
  1360. }
  1361. }
  1362. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1363. assert(k % QK_K == 0);
  1364. const int nb = k / QK_K;
  1365. for (int i = 0; i < nb; i++) {
  1366. const float d = GGML_FP16_TO_FP32(x[i].d);
  1367. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1368. const uint8_t * q = x[i].qs;
  1369. #if QK_K == 256
  1370. int is = 0;
  1371. float dl, ml;
  1372. for (int n = 0; n < QK_K; n += 128) {
  1373. int shift = 0;
  1374. for (int j = 0; j < 4; ++j) {
  1375. uint8_t sc = x[i].scales[is++];
  1376. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1377. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1378. 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+16] >> shift) & 3)) - ml;
  1381. shift += 2;
  1382. }
  1383. q += 32;
  1384. }
  1385. #else
  1386. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1387. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1388. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1389. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1390. for (int l = 0; l < 16; ++l) {
  1391. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1392. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1393. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1394. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1395. }
  1396. y += QK_K;
  1397. #endif
  1398. }
  1399. }
  1400. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1401. quantize_row_q2_K_reference(x, vy, k);
  1402. }
  1403. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1404. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1405. float rmin, float rdelta, int nstep, bool use_mad) {
  1406. float min = x[0];
  1407. float max = x[0];
  1408. float sum_w = weights ? weights[0] : x[0]*x[0];
  1409. float sum_x = sum_w * x[0];
  1410. #ifdef HAVE_BUGGY_APPLE_LINKER
  1411. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1412. for (volatile int i = 1; i < n; ++i) {
  1413. #else
  1414. for (int i = 1; i < n; ++i) {
  1415. #endif
  1416. if (x[i] < min) min = x[i];
  1417. if (x[i] > max) max = x[i];
  1418. float w = weights ? weights[i] : x[i]*x[i];
  1419. sum_w += w;
  1420. sum_x += w * x[i];
  1421. }
  1422. if (min > 0) {
  1423. min = 0;
  1424. }
  1425. if (max <= min) {
  1426. memset(L, 0, n);
  1427. *the_min = -min;
  1428. return 0.f;
  1429. }
  1430. float iscale = nmax/(max - min);
  1431. float scale = 1/iscale;
  1432. float best_mad = 0;
  1433. for (int i = 0; i < n; ++i) {
  1434. int l = nearest_int(iscale*(x[i] - min));
  1435. L[i] = MAX(0, MIN(nmax, l));
  1436. float diff = scale * L[i] + min - x[i];
  1437. diff = use_mad ? fabsf(diff) : diff*diff;
  1438. float w = weights ? weights[i] : x[i]*x[i];
  1439. best_mad += w * diff;
  1440. }
  1441. if (nstep < 1) {
  1442. *the_min = -min;
  1443. return scale;
  1444. }
  1445. for (int is = 0; is <= nstep; ++is) {
  1446. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1447. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1448. for (int i = 0; i < n; ++i) {
  1449. int l = nearest_int(iscale*(x[i] - min));
  1450. l = MAX(0, MIN(nmax, l));
  1451. Laux[i] = l;
  1452. float w = weights ? weights[i] : x[i]*x[i];
  1453. sum_l += w*l;
  1454. sum_l2 += w*l*l;
  1455. sum_xl += w*l*x[i];
  1456. }
  1457. float D = sum_w * sum_l2 - sum_l * sum_l;
  1458. if (D > 0) {
  1459. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1460. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1461. if (this_min > 0) {
  1462. this_min = 0;
  1463. this_scale = sum_xl / sum_l2;
  1464. }
  1465. float mad = 0;
  1466. for (int i = 0; i < n; ++i) {
  1467. float diff = this_scale * Laux[i] + this_min - x[i];
  1468. diff = use_mad ? fabsf(diff) : diff*diff;
  1469. float w = weights ? weights[i] : x[i]*x[i];
  1470. mad += w * diff;
  1471. }
  1472. if (mad < best_mad) {
  1473. for (int i = 0; i < n; ++i) {
  1474. L[i] = Laux[i];
  1475. }
  1476. best_mad = mad;
  1477. scale = this_scale;
  1478. min = this_min;
  1479. }
  1480. }
  1481. }
  1482. *the_min = -min;
  1483. return scale;
  1484. }
  1485. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1486. float max = 0;
  1487. for (int i = 0; i < n; ++i) {
  1488. max = MAX(max, x[i]);
  1489. }
  1490. if (!max) { // all zero
  1491. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1492. return 0.f;
  1493. }
  1494. float iscale = nmax / max;
  1495. for (int i = 0; i < n; ++i) {
  1496. L[i] = nearest_int(iscale * x[i]);
  1497. }
  1498. float scale = 1/iscale;
  1499. float best_mse = 0;
  1500. for (int i = 0; i < n; ++i) {
  1501. float diff = x[i] - scale*L[i];
  1502. float w = quant_weights[i];
  1503. best_mse += w*diff*diff;
  1504. }
  1505. for (int is = -4; is <= 4; ++is) {
  1506. if (is == 0) continue;
  1507. float iscale_is = (0.1f*is + nmax)/max;
  1508. float scale_is = 1/iscale_is;
  1509. float mse = 0;
  1510. for (int i = 0; i < n; ++i) {
  1511. int l = nearest_int(iscale_is*x[i]);
  1512. l = MIN(nmax, l);
  1513. float diff = x[i] - scale_is*l;
  1514. float w = quant_weights[i];
  1515. mse += w*diff*diff;
  1516. }
  1517. if (mse < best_mse) {
  1518. best_mse = mse;
  1519. iscale = iscale_is;
  1520. }
  1521. }
  1522. float sumlx = 0;
  1523. float suml2 = 0;
  1524. for (int i = 0; i < n; ++i) {
  1525. int l = nearest_int(iscale * x[i]);
  1526. l = MIN(nmax, l);
  1527. L[i] = l;
  1528. float w = quant_weights[i];
  1529. sumlx += w*x[i]*l;
  1530. suml2 += w*l*l;
  1531. }
  1532. for (int itry = 0; itry < 5; ++itry) {
  1533. int n_changed = 0;
  1534. for (int i = 0; i < n; ++i) {
  1535. float w = quant_weights[i];
  1536. float slx = sumlx - w*x[i]*L[i];
  1537. float sl2 = suml2 - w*L[i]*L[i];
  1538. if (slx > 0 && sl2 > 0) {
  1539. int new_l = nearest_int(x[i] * sl2 / slx);
  1540. new_l = MIN(nmax, new_l);
  1541. if (new_l != L[i]) {
  1542. slx += w*x[i]*new_l;
  1543. sl2 += w*new_l*new_l;
  1544. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1545. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1546. ++n_changed;
  1547. }
  1548. }
  1549. }
  1550. }
  1551. if (!n_changed) {
  1552. break;
  1553. }
  1554. }
  1555. return sumlx / suml2;
  1556. }
  1557. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1558. GGML_ASSERT(quant_weights);
  1559. assert(k % QK_K == 0);
  1560. const int nb = k / QK_K;
  1561. const bool requantize = true;
  1562. uint8_t L[QK_K];
  1563. uint8_t Laux[16];
  1564. float mins[QK_K/16];
  1565. float scales[QK_K/16];
  1566. float sw[QK_K/16];
  1567. float weight[16];
  1568. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1569. for (int i = 0; i < nb; i++) {
  1570. memset(sw, 0, QK_K/16*sizeof(float));
  1571. float sumx2 = 0;
  1572. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1573. float sigma2 = sumx2/QK_K;
  1574. for (int j = 0; j < QK_K/16; ++j) {
  1575. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1576. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1577. for (int l = 0; l < QK_K/16; ++l) sw[j] += weight[l];
  1578. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1579. }
  1580. float dm, mm;
  1581. #if QK_K == 64
  1582. float max_scale = 0, max_min = 0;
  1583. for (int j = 0; j < QK_K/16; ++j) {
  1584. max_scale = MAX(max_scale, scales[j]);
  1585. max_min = MAX(max_min, mins[j]);
  1586. }
  1587. dm = max_scale/15;
  1588. mm = max_min/15;
  1589. if (max_scale) {
  1590. float id = 1/dm;
  1591. for (int j = 0; j < QK_K/16; ++j) {
  1592. int l = nearest_int(id*scales[j]);
  1593. Ls[j] = MAX(0, MIN(15, l));
  1594. }
  1595. } else {
  1596. memset(Ls, 0, QK_K/16);
  1597. }
  1598. if (max_min) {
  1599. float id = 1/mm;
  1600. for (int j = 0; j < QK_K/16; ++j) {
  1601. int l = nearest_int(id*mins[j]);
  1602. Lm[j] = MAX(0, MIN(15, l));
  1603. }
  1604. } else {
  1605. memset(Lm, 0, QK_K/16);
  1606. }
  1607. #else
  1608. dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1609. mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1610. #endif
  1611. y[i].d = GGML_FP32_TO_FP16(dm);
  1612. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1613. dm = GGML_FP16_TO_FP32(y[i].d);
  1614. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1615. for (int j = 0; j < QK_K/16; ++j) {
  1616. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1617. }
  1618. if (requantize) {
  1619. for (int j = 0; j < QK_K/16; ++j) {
  1620. const float d = dm * (y[i].scales[j] & 0xF);
  1621. if (!d) continue;
  1622. const float m = mm * (y[i].scales[j] >> 4);
  1623. for (int ii = 0; ii < 16; ++ii) {
  1624. int l = nearest_int((x[16*j + ii] + m)/d);
  1625. l = MAX(0, MIN(3, l));
  1626. L[16*j + ii] = l;
  1627. }
  1628. }
  1629. }
  1630. #if QK_K == 256
  1631. for (int j = 0; j < QK_K; j += 128) {
  1632. for (int l = 0; l < 32; ++l) {
  1633. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1634. }
  1635. }
  1636. #else
  1637. for (int l = 0; l < 16; ++l) {
  1638. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1639. }
  1640. #endif
  1641. x += QK_K;
  1642. }
  1643. }
  1644. size_t quantize_q2_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  1645. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1646. if (!quant_weights) {
  1647. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1648. }
  1649. else {
  1650. char * qrow = (char *)dst;
  1651. for (int row = 0; row < nrow; ++row) {
  1652. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1653. src += n_per_row;
  1654. qrow += row_size;
  1655. }
  1656. }
  1657. return nrow * row_size;
  1658. }
  1659. //========================= 3-bit (de)-quantization
  1660. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1661. assert(k % QK_K == 0);
  1662. const int nb = k / QK_K;
  1663. int8_t L[QK_K];
  1664. float scales[QK_K / 16];
  1665. for (int i = 0; i < nb; i++) {
  1666. float max_scale = 0;
  1667. float amax = 0;
  1668. for (int j = 0; j < QK_K/16; ++j) {
  1669. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1670. float scale = fabsf(scales[j]);
  1671. if (scale > amax) {
  1672. amax = scale; max_scale = scales[j];
  1673. }
  1674. }
  1675. #if QK_K == 256
  1676. memset(y[i].scales, 0, 12);
  1677. if (max_scale) {
  1678. float iscale = -32.f/max_scale;
  1679. for (int j = 0; j < QK_K/16; ++j) {
  1680. int8_t l = nearest_int(iscale*scales[j]);
  1681. l = MAX(-32, MIN(31, l)) + 32;
  1682. if (j < 8) {
  1683. y[i].scales[j] = l & 0xF;
  1684. } else {
  1685. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1686. }
  1687. l >>= 4;
  1688. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1689. }
  1690. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1691. } else {
  1692. y[i].d = GGML_FP32_TO_FP16(0.f);
  1693. }
  1694. int8_t sc;
  1695. for (int j = 0; j < QK_K/16; ++j) {
  1696. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1697. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1698. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1699. if (!d) {
  1700. continue;
  1701. }
  1702. for (int ii = 0; ii < 16; ++ii) {
  1703. int l = nearest_int(x[16*j + ii]/d);
  1704. l = MAX(-4, MIN(3, l));
  1705. L[16*j + ii] = l + 4;
  1706. }
  1707. }
  1708. #else
  1709. if (max_scale) {
  1710. float iscale = -8.f/max_scale;
  1711. for (int j = 0; j < QK_K/16; j+=2) {
  1712. int l1 = nearest_int(iscale*scales[j]);
  1713. l1 = 8 + MAX(-8, MIN(7, l1));
  1714. int l2 = nearest_int(iscale*scales[j+1]);
  1715. l2 = 8 + MAX(-8, MIN(7, l2));
  1716. y[i].scales[j/2] = l1 | (l2 << 4);
  1717. }
  1718. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1719. } else {
  1720. for (int j = 0; j < QK_K/16; j+=2) {
  1721. y[i].scales[j/2] = 0;
  1722. }
  1723. y[i].d = GGML_FP32_TO_FP16(0.f);
  1724. }
  1725. for (int j = 0; j < QK_K/16; ++j) {
  1726. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1727. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1728. if (!d) {
  1729. continue;
  1730. }
  1731. for (int ii = 0; ii < 16; ++ii) {
  1732. int l = nearest_int(x[16*j + ii]/d);
  1733. l = MAX(-4, MIN(3, l));
  1734. L[16*j + ii] = l + 4;
  1735. }
  1736. }
  1737. #endif
  1738. memset(y[i].hmask, 0, QK_K/8);
  1739. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1740. int m = 0;
  1741. uint8_t hm = 1;
  1742. for (int j = 0; j < QK_K; ++j) {
  1743. if (L[j] > 3) {
  1744. y[i].hmask[m] |= hm;
  1745. L[j] -= 4;
  1746. }
  1747. if (++m == QK_K/8) {
  1748. m = 0; hm <<= 1;
  1749. }
  1750. }
  1751. #if QK_K == 256
  1752. for (int j = 0; j < QK_K; j += 128) {
  1753. for (int l = 0; l < 32; ++l) {
  1754. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1755. }
  1756. }
  1757. #else
  1758. for (int l = 0; l < 16; ++l) {
  1759. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1760. }
  1761. #endif
  1762. x += QK_K;
  1763. }
  1764. }
  1765. #if QK_K == 256
  1766. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1767. assert(k % QK_K == 0);
  1768. const int nb = k / QK_K;
  1769. const uint32_t kmask1 = 0x03030303;
  1770. const uint32_t kmask2 = 0x0f0f0f0f;
  1771. uint32_t aux[4];
  1772. const int8_t * scales = (const int8_t*)aux;
  1773. for (int i = 0; i < nb; i++) {
  1774. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1775. const uint8_t * restrict q = x[i].qs;
  1776. const uint8_t * restrict hm = x[i].hmask;
  1777. uint8_t m = 1;
  1778. memcpy(aux, x[i].scales, 12);
  1779. uint32_t tmp = aux[2];
  1780. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1781. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1782. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1783. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1784. int is = 0;
  1785. float dl;
  1786. for (int n = 0; n < QK_K; n += 128) {
  1787. int shift = 0;
  1788. for (int j = 0; j < 4; ++j) {
  1789. dl = d_all * (scales[is++] - 32);
  1790. for (int l = 0; l < 16; ++l) {
  1791. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1792. }
  1793. dl = d_all * (scales[is++] - 32);
  1794. for (int l = 0; l < 16; ++l) {
  1795. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1796. }
  1797. shift += 2;
  1798. m <<= 1;
  1799. }
  1800. q += 32;
  1801. }
  1802. }
  1803. }
  1804. #else
  1805. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1806. assert(k % QK_K == 0);
  1807. assert(QK_K == 64);
  1808. const int nb = k / QK_K;
  1809. for (int i = 0; i < nb; i++) {
  1810. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1811. const uint8_t * restrict q = x[i].qs;
  1812. const uint8_t * restrict hm = x[i].hmask;
  1813. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1814. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1815. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1816. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1817. for (int l=0; l<8; ++l) {
  1818. uint8_t h = hm[l];
  1819. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1820. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1821. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1822. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1823. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1824. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1825. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1826. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1827. }
  1828. y += QK_K;
  1829. }
  1830. }
  1831. #endif
  1832. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1833. quantize_row_q3_K_reference(x, vy, k);
  1834. }
  1835. 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) {
  1836. #if QK_K != 256
  1837. (void)quant_weights;
  1838. quantize_row_q3_K_reference(x, y, n_per_row);
  1839. #else
  1840. assert(n_per_row % QK_K == 0);
  1841. const int nb = n_per_row / QK_K;
  1842. int8_t L[QK_K];
  1843. float scales[QK_K / 16];
  1844. float weight[16];
  1845. float sw[QK_K / 16];
  1846. int8_t Ls[QK_K / 16];
  1847. for (int i = 0; i < nb; i++) {
  1848. float sumx2 = 0;
  1849. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1850. float sigma2 = 2*sumx2/QK_K;
  1851. for (int j = 0; j < QK_K/16; ++j) {
  1852. if (quant_weights) {
  1853. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1854. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1855. } else {
  1856. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1857. }
  1858. float sumw = 0;
  1859. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1860. sw[j] = sumw;
  1861. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1862. }
  1863. memset(y[i].scales, 0, 12);
  1864. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1865. for (int j = 0; j < QK_K/16; ++j) {
  1866. int l = Ls[j];
  1867. if (j < 8) {
  1868. y[i].scales[j] = l & 0xF;
  1869. } else {
  1870. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1871. }
  1872. l >>= 4;
  1873. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1874. }
  1875. y[i].d = GGML_FP32_TO_FP16(d_block);
  1876. int8_t sc;
  1877. for (int j = 0; j < QK_K/16; ++j) {
  1878. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1879. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1880. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1881. if (!d) {
  1882. continue;
  1883. }
  1884. for (int ii = 0; ii < 16; ++ii) {
  1885. int l = nearest_int(x[16*j + ii]/d);
  1886. l = MAX(-4, MIN(3, l));
  1887. L[16*j + ii] = l + 4;
  1888. }
  1889. }
  1890. memset(y[i].hmask, 0, QK_K/8);
  1891. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1892. int m = 0;
  1893. uint8_t hm = 1;
  1894. for (int j = 0; j < QK_K; ++j) {
  1895. if (L[j] > 3) {
  1896. y[i].hmask[m] |= hm;
  1897. L[j] -= 4;
  1898. }
  1899. if (++m == QK_K/8) {
  1900. m = 0; hm <<= 1;
  1901. }
  1902. }
  1903. for (int j = 0; j < QK_K; j += 128) {
  1904. for (int l = 0; l < 32; ++l) {
  1905. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1906. }
  1907. }
  1908. x += QK_K;
  1909. }
  1910. #endif
  1911. }
  1912. size_t quantize_q3_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  1913. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1914. if (!quant_weights) {
  1915. quantize_row_q3_K_reference(src, dst, nrow*n_per_row);
  1916. }
  1917. else {
  1918. char * qrow = (char *)dst;
  1919. for (int row = 0; row < nrow; ++row) {
  1920. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1921. src += n_per_row;
  1922. qrow += row_size;
  1923. }
  1924. }
  1925. return nrow * row_size;
  1926. }
  1927. // ====================== 4-bit (de)-quantization
  1928. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1929. assert(k % QK_K == 0);
  1930. const int nb = k / QK_K;
  1931. uint8_t L[QK_K];
  1932. uint8_t Laux[32];
  1933. float weights[32];
  1934. float mins[QK_K/32];
  1935. float scales[QK_K/32];
  1936. for (int i = 0; i < nb; i++) {
  1937. float max_scale = 0; // as we are deducting the min, scales are always positive
  1938. float max_min = 0;
  1939. for (int j = 0; j < QK_K/32; ++j) {
  1940. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1941. float sum_x2 = 0;
  1942. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1943. float av_x = sqrtf(sum_x2/32);
  1944. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1945. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1946. float scale = scales[j];
  1947. if (scale > max_scale) {
  1948. max_scale = scale;
  1949. }
  1950. float min = mins[j];
  1951. if (min > max_min) {
  1952. max_min = min;
  1953. }
  1954. }
  1955. #if QK_K == 256
  1956. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1957. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1958. for (int j = 0; j < QK_K/32; ++j) {
  1959. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1960. uint8_t lm = nearest_int(inv_min*mins[j]);
  1961. ls = MIN(63, ls);
  1962. lm = MIN(63, lm);
  1963. if (j < 4) {
  1964. y[i].scales[j] = ls;
  1965. y[i].scales[j+4] = lm;
  1966. } else {
  1967. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1968. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1969. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1970. }
  1971. }
  1972. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1973. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1974. uint8_t sc, m;
  1975. for (int j = 0; j < QK_K/32; ++j) {
  1976. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1977. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1978. if (!d) continue;
  1979. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1980. for (int ii = 0; ii < 32; ++ii) {
  1981. int l = nearest_int((x[32*j + ii] + dm)/d);
  1982. l = MAX(0, MIN(15, l));
  1983. L[32*j + ii] = l;
  1984. }
  1985. }
  1986. #else
  1987. const float s_factor = 15.f;
  1988. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1989. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1990. int d1 = nearest_int(inv_scale*scales[0]);
  1991. int m1 = nearest_int(inv_min*mins[0]);
  1992. int d2 = nearest_int(inv_scale*scales[1]);
  1993. int m2 = nearest_int(inv_min*mins[1]);
  1994. y[i].scales[0] = d1 | (m1 << 4);
  1995. y[i].scales[1] = d2 | (m2 << 4);
  1996. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1997. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1998. float sumlx = 0;
  1999. int suml2 = 0;
  2000. for (int j = 0; j < QK_K/32; ++j) {
  2001. const uint8_t sd = y[i].scales[j] & 0xF;
  2002. const uint8_t sm = y[i].scales[j] >> 4;
  2003. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  2004. if (!d) continue;
  2005. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  2006. for (int ii = 0; ii < 32; ++ii) {
  2007. int l = nearest_int((x[32*j + ii] + m)/d);
  2008. l = MAX(0, MIN(15, l));
  2009. L[32*j + ii] = l;
  2010. sumlx += (x[32*j + ii] + m)*l*sd;
  2011. suml2 += l*l*sd*sd;
  2012. }
  2013. }
  2014. if (suml2) {
  2015. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  2016. }
  2017. #endif
  2018. uint8_t * q = y[i].qs;
  2019. for (int j = 0; j < QK_K; j += 64) {
  2020. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2021. q += 32;
  2022. }
  2023. x += QK_K;
  2024. }
  2025. }
  2026. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  2027. assert(k % QK_K == 0);
  2028. const int nb = k / QK_K;
  2029. for (int i = 0; i < nb; i++) {
  2030. const uint8_t * q = x[i].qs;
  2031. #if QK_K == 256
  2032. const float d = GGML_FP16_TO_FP32(x[i].d);
  2033. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2034. int is = 0;
  2035. uint8_t sc, m;
  2036. for (int j = 0; j < QK_K; j += 64) {
  2037. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2038. const float d1 = d * sc; const float m1 = min * m;
  2039. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2040. const float d2 = d * sc; const float m2 = min * m;
  2041. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  2042. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  2043. q += 32; is += 2;
  2044. }
  2045. #else
  2046. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  2047. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  2048. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  2049. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  2050. for (int l = 0; l < 32; ++l) {
  2051. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  2052. y[l+32] = d2 * (q[l] >> 4) - m2;
  2053. }
  2054. y += QK_K;
  2055. #endif
  2056. }
  2057. }
  2058. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  2059. assert(k % QK_K == 0);
  2060. block_q4_K * restrict y = vy;
  2061. quantize_row_q4_K_reference(x, y, k);
  2062. }
  2063. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int n_per_row, const float * quant_weights) {
  2064. #if QK_K != 256
  2065. (void)quant_weights;
  2066. quantize_row_q4_K_reference(x, y, n_per_row);
  2067. #else
  2068. assert(n_per_row % QK_K == 0);
  2069. const int nb = n_per_row / QK_K;
  2070. uint8_t L[QK_K];
  2071. uint8_t Laux[32];
  2072. uint8_t Ls[QK_K/32];
  2073. uint8_t Lm[QK_K/32];
  2074. float weights[32];
  2075. float sw[QK_K/32];
  2076. float mins[QK_K/32];
  2077. float scales[QK_K/32];
  2078. for (int i = 0; i < nb; i++) {
  2079. float sum_x2 = 0;
  2080. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2081. float sigma2 = 2*sum_x2/QK_K;
  2082. float av_x = sqrtf(sigma2);
  2083. for (int j = 0; j < QK_K/32; ++j) {
  2084. if (quant_weights) {
  2085. const float * qw = quant_weights + QK_K*i + 32*j;
  2086. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2087. } else {
  2088. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2089. }
  2090. float sumw = 0;
  2091. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2092. sw[j] = sumw;
  2093. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2094. }
  2095. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2096. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2097. for (int j = 0; j < QK_K/32; ++j) {
  2098. uint8_t ls = Ls[j];
  2099. uint8_t lm = Lm[j];
  2100. if (j < 4) {
  2101. y[i].scales[j] = ls;
  2102. y[i].scales[j+4] = lm;
  2103. } else {
  2104. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2105. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2106. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2107. }
  2108. }
  2109. y[i].d = GGML_FP32_TO_FP16(d_block);
  2110. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2111. uint8_t sc, m;
  2112. for (int j = 0; j < QK_K/32; ++j) {
  2113. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2114. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2115. if (!d) continue;
  2116. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2117. for (int ii = 0; ii < 32; ++ii) {
  2118. int l = nearest_int((x[32*j + ii] + dm)/d);
  2119. l = MAX(0, MIN(15, l));
  2120. L[32*j + ii] = l;
  2121. }
  2122. }
  2123. uint8_t * q = y[i].qs;
  2124. for (int j = 0; j < QK_K; j += 64) {
  2125. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2126. q += 32;
  2127. }
  2128. x += QK_K;
  2129. }
  2130. #endif
  2131. }
  2132. size_t quantize_q4_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2133. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2134. if (!quant_weights) {
  2135. quantize_row_q4_K_reference(src, dst, nrow*n_per_row);
  2136. }
  2137. else {
  2138. char * qrow = (char *)dst;
  2139. for (int row = 0; row < nrow; ++row) {
  2140. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2141. src += n_per_row;
  2142. qrow += row_size;
  2143. }
  2144. }
  2145. return nrow * row_size;
  2146. }
  2147. // ====================== 5-bit (de)-quantization
  2148. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  2149. assert(k % QK_K == 0);
  2150. const int nb = k / QK_K;
  2151. #if QK_K == 256
  2152. uint8_t L[QK_K];
  2153. float mins[QK_K/32];
  2154. float scales[QK_K/32];
  2155. float weights[32];
  2156. uint8_t Laux[32];
  2157. #else
  2158. int8_t L[QK_K];
  2159. float scales[QK_K/16];
  2160. #endif
  2161. for (int i = 0; i < nb; i++) {
  2162. #if QK_K == 256
  2163. float max_scale = 0; // as we are deducting the min, scales are always positive
  2164. float max_min = 0;
  2165. for (int j = 0; j < QK_K/32; ++j) {
  2166. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2167. float sum_x2 = 0;
  2168. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2169. float av_x = sqrtf(sum_x2/32);
  2170. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2171. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2172. float scale = scales[j];
  2173. if (scale > max_scale) {
  2174. max_scale = scale;
  2175. }
  2176. float min = mins[j];
  2177. if (min > max_min) {
  2178. max_min = min;
  2179. }
  2180. }
  2181. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2182. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2183. for (int j = 0; j < QK_K/32; ++j) {
  2184. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2185. uint8_t lm = nearest_int(inv_min*mins[j]);
  2186. ls = MIN(63, ls);
  2187. lm = MIN(63, lm);
  2188. if (j < 4) {
  2189. y[i].scales[j] = ls;
  2190. y[i].scales[j+4] = lm;
  2191. } else {
  2192. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2193. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2194. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2195. }
  2196. }
  2197. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2198. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2199. uint8_t sc, m;
  2200. for (int j = 0; j < QK_K/32; ++j) {
  2201. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2202. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2203. if (!d) continue;
  2204. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2205. for (int ii = 0; ii < 32; ++ii) {
  2206. int l = nearest_int((x[32*j + ii] + dm)/d);
  2207. l = MAX(0, MIN(31, l));
  2208. L[32*j + ii] = l;
  2209. }
  2210. }
  2211. uint8_t * restrict qh = y[i].qh;
  2212. uint8_t * restrict ql = y[i].qs;
  2213. memset(qh, 0, QK_K/8);
  2214. uint8_t m1 = 1, m2 = 2;
  2215. for (int n = 0; n < QK_K; n += 64) {
  2216. for (int j = 0; j < 32; ++j) {
  2217. int l1 = L[n + j];
  2218. if (l1 > 15) {
  2219. l1 -= 16; qh[j] |= m1;
  2220. }
  2221. int l2 = L[n + j + 32];
  2222. if (l2 > 15) {
  2223. l2 -= 16; qh[j] |= m2;
  2224. }
  2225. ql[j] = l1 | (l2 << 4);
  2226. }
  2227. m1 <<= 2; m2 <<= 2;
  2228. ql += 32;
  2229. }
  2230. #else
  2231. float max_scale = 0, amax = 0;
  2232. for (int j = 0; j < QK_K/16; ++j) {
  2233. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2234. float abs_scale = fabsf(scales[j]);
  2235. if (abs_scale > amax) {
  2236. amax = abs_scale;
  2237. max_scale = scales[j];
  2238. }
  2239. }
  2240. float iscale = -128.f/max_scale;
  2241. for (int j = 0; j < QK_K/16; ++j) {
  2242. int l = nearest_int(iscale*scales[j]);
  2243. y[i].scales[j] = MAX(-128, MIN(127, l));
  2244. }
  2245. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2246. for (int j = 0; j < QK_K/16; ++j) {
  2247. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2248. if (!d) continue;
  2249. for (int ii = 0; ii < 16; ++ii) {
  2250. int l = nearest_int(x[16*j + ii]/d);
  2251. l = MAX(-16, MIN(15, l));
  2252. L[16*j + ii] = l + 16;
  2253. }
  2254. }
  2255. uint8_t * restrict qh = y[i].qh;
  2256. uint8_t * restrict ql = y[i].qs;
  2257. memset(qh, 0, QK_K/8);
  2258. for (int j = 0; j < 32; ++j) {
  2259. int jm = j%8;
  2260. int is = j/8;
  2261. int l1 = L[j];
  2262. if (l1 > 15) {
  2263. l1 -= 16; qh[jm] |= (1 << is);
  2264. }
  2265. int l2 = L[j + 32];
  2266. if (l2 > 15) {
  2267. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2268. }
  2269. ql[j] = l1 | (l2 << 4);
  2270. }
  2271. #endif
  2272. x += QK_K;
  2273. }
  2274. }
  2275. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2276. assert(k % QK_K == 0);
  2277. const int nb = k / QK_K;
  2278. for (int i = 0; i < nb; i++) {
  2279. const uint8_t * ql = x[i].qs;
  2280. const uint8_t * qh = x[i].qh;
  2281. #if QK_K == 256
  2282. const float d = GGML_FP16_TO_FP32(x[i].d);
  2283. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2284. int is = 0;
  2285. uint8_t sc, m;
  2286. uint8_t u1 = 1, u2 = 2;
  2287. for (int j = 0; j < QK_K; j += 64) {
  2288. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2289. const float d1 = d * sc; const float m1 = min * m;
  2290. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2291. const float d2 = d * sc; const float m2 = min * m;
  2292. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2293. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2294. ql += 32; is += 2;
  2295. u1 <<= 2; u2 <<= 2;
  2296. }
  2297. #else
  2298. float d = GGML_FP16_TO_FP32(x[i].d);
  2299. const int8_t * restrict s = x[i].scales;
  2300. for (int l = 0; l < 8; ++l) {
  2301. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2302. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2303. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2304. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2305. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2306. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2307. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2308. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2309. }
  2310. y += QK_K;
  2311. #endif
  2312. }
  2313. }
  2314. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2315. assert(k % QK_K == 0);
  2316. block_q5_K * restrict y = vy;
  2317. quantize_row_q5_K_reference(x, y, k);
  2318. }
  2319. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int n_per_row, const float * quant_weights) {
  2320. #if QK_K != 256
  2321. (void)quant_weights;
  2322. quantize_row_q5_K_reference(x, y, n_per_row);
  2323. #else
  2324. assert(n_per_row % QK_K == 0);
  2325. const int nb = n_per_row / QK_K;
  2326. uint8_t L[QK_K];
  2327. uint8_t Laux[32];
  2328. uint8_t Ls[QK_K/32];
  2329. uint8_t Lm[QK_K/32];
  2330. float mins[QK_K/32];
  2331. float scales[QK_K/32];
  2332. float sw[QK_K/32];
  2333. float weights[32];
  2334. for (int i = 0; i < nb; i++) {
  2335. float sum_x2 = 0;
  2336. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2337. float sigma2 = 2*sum_x2/QK_K;
  2338. float av_x = sqrtf(sigma2);
  2339. for (int j = 0; j < QK_K/32; ++j) {
  2340. if (quant_weights) {
  2341. const float * qw = quant_weights + QK_K*i + 32*j;
  2342. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2343. } else {
  2344. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2345. }
  2346. float sumw = 0;
  2347. for (int l = 0; l < 32; ++l) sumw += weights[l];
  2348. sw[j] = sumw;
  2349. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2350. }
  2351. float d_block = make_qp_quants(QK_K/32, 63, scales, Ls, sw);
  2352. float m_block = make_qp_quants(QK_K/32, 63, mins, Lm, sw);
  2353. for (int j = 0; j < QK_K/32; ++j) {
  2354. uint8_t ls = Ls[j];
  2355. uint8_t lm = Lm[j];
  2356. ls = MIN(63, ls);
  2357. lm = MIN(63, lm);
  2358. if (j < 4) {
  2359. y[i].scales[j] = ls;
  2360. y[i].scales[j+4] = lm;
  2361. } else {
  2362. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2363. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2364. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2365. }
  2366. }
  2367. y[i].d = GGML_FP32_TO_FP16(d_block);
  2368. y[i].dmin = GGML_FP32_TO_FP16(m_block);
  2369. uint8_t sc, m;
  2370. for (int j = 0; j < QK_K/32; ++j) {
  2371. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2372. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2373. if (!d) continue;
  2374. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2375. for (int ii = 0; ii < 32; ++ii) {
  2376. int l = nearest_int((x[32*j + ii] + dm)/d);
  2377. l = MAX(0, MIN(31, l));
  2378. L[32*j + ii] = l;
  2379. }
  2380. }
  2381. uint8_t * restrict qh = y[i].qh;
  2382. uint8_t * restrict ql = y[i].qs;
  2383. memset(qh, 0, QK_K/8);
  2384. uint8_t m1 = 1, m2 = 2;
  2385. for (int n = 0; n < QK_K; n += 64) {
  2386. for (int j = 0; j < 32; ++j) {
  2387. int l1 = L[n + j];
  2388. if (l1 > 15) {
  2389. l1 -= 16; qh[j] |= m1;
  2390. }
  2391. int l2 = L[n + j + 32];
  2392. if (l2 > 15) {
  2393. l2 -= 16; qh[j] |= m2;
  2394. }
  2395. ql[j] = l1 | (l2 << 4);
  2396. }
  2397. m1 <<= 2; m2 <<= 2;
  2398. ql += 32;
  2399. }
  2400. x += QK_K;
  2401. }
  2402. #endif
  2403. }
  2404. size_t quantize_q5_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2405. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2406. if (!quant_weights) {
  2407. quantize_row_q5_K_reference(src, dst, nrow*n_per_row);
  2408. }
  2409. else {
  2410. char * qrow = (char *)dst;
  2411. for (int row = 0; row < nrow; ++row) {
  2412. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2413. src += n_per_row;
  2414. qrow += row_size;
  2415. }
  2416. }
  2417. return nrow * row_size;
  2418. }
  2419. // ====================== 6-bit (de)-quantization
  2420. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2421. assert(k % QK_K == 0);
  2422. const int nb = k / QK_K;
  2423. int8_t L[QK_K];
  2424. float scales[QK_K/16];
  2425. for (int i = 0; i < nb; i++) {
  2426. float max_scale = 0;
  2427. float max_abs_scale = 0;
  2428. for (int ib = 0; ib < QK_K/16; ++ib) {
  2429. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2430. scales[ib] = scale;
  2431. const float abs_scale = fabsf(scale);
  2432. if (abs_scale > max_abs_scale) {
  2433. max_abs_scale = abs_scale;
  2434. max_scale = scale;
  2435. }
  2436. }
  2437. if (!max_abs_scale) {
  2438. memset(&y[i], 0, sizeof(block_q6_K));
  2439. y[i].d = GGML_FP32_TO_FP16(0.f);
  2440. x += QK_K;
  2441. continue;
  2442. }
  2443. float iscale = -128.f/max_scale;
  2444. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2445. for (int ib = 0; ib < QK_K/16; ++ib) {
  2446. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2447. }
  2448. for (int j = 0; j < QK_K/16; ++j) {
  2449. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2450. if (!d) {
  2451. continue;
  2452. }
  2453. for (int ii = 0; ii < 16; ++ii) {
  2454. int l = nearest_int(x[16*j + ii]/d);
  2455. l = MAX(-32, MIN(31, l));
  2456. L[16*j + ii] = l + 32;
  2457. }
  2458. }
  2459. uint8_t * restrict ql = y[i].ql;
  2460. uint8_t * restrict qh = y[i].qh;
  2461. #if QK_K == 256
  2462. for (int j = 0; j < QK_K; j += 128) {
  2463. for (int l = 0; l < 32; ++l) {
  2464. const uint8_t q1 = L[j + l + 0] & 0xF;
  2465. const uint8_t q2 = L[j + l + 32] & 0xF;
  2466. const uint8_t q3 = L[j + l + 64] & 0xF;
  2467. const uint8_t q4 = L[j + l + 96] & 0xF;
  2468. ql[l+ 0] = q1 | (q3 << 4);
  2469. ql[l+32] = q2 | (q4 << 4);
  2470. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2471. }
  2472. ql += 64;
  2473. qh += 32;
  2474. }
  2475. #else
  2476. for (int l = 0; l < 32; ++l) {
  2477. const uint8_t q1 = L[l + 0] & 0xF;
  2478. const uint8_t q2 = L[l + 32] & 0xF;
  2479. ql[l] = q1 | (q2 << 4);
  2480. }
  2481. for (int l = 0; l < 16; ++l) {
  2482. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2483. }
  2484. #endif
  2485. x += QK_K;
  2486. }
  2487. }
  2488. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2489. assert(k % QK_K == 0);
  2490. const int nb = k / QK_K;
  2491. for (int i = 0; i < nb; i++) {
  2492. const float d = GGML_FP16_TO_FP32(x[i].d);
  2493. const uint8_t * restrict ql = x[i].ql;
  2494. const uint8_t * restrict qh = x[i].qh;
  2495. const int8_t * restrict sc = x[i].scales;
  2496. #if QK_K == 256
  2497. for (int n = 0; n < QK_K; n += 128) {
  2498. for (int l = 0; l < 32; ++l) {
  2499. int is = l/16;
  2500. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2501. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2502. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2503. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2504. y[l + 0] = d * sc[is + 0] * q1;
  2505. y[l + 32] = d * sc[is + 2] * q2;
  2506. y[l + 64] = d * sc[is + 4] * q3;
  2507. y[l + 96] = d * sc[is + 6] * q4;
  2508. }
  2509. y += 128;
  2510. ql += 64;
  2511. qh += 32;
  2512. sc += 8;
  2513. }
  2514. #else
  2515. for (int l = 0; l < 16; ++l) {
  2516. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2517. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2518. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2519. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2520. y[l+ 0] = d * sc[0] * q1;
  2521. y[l+16] = d * sc[1] * q2;
  2522. y[l+32] = d * sc[2] * q3;
  2523. y[l+48] = d * sc[3] * q4;
  2524. }
  2525. y += 64;
  2526. #endif
  2527. }
  2528. }
  2529. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2530. assert(k % QK_K == 0);
  2531. block_q6_K * restrict y = vy;
  2532. quantize_row_q6_K_reference(x, y, k);
  2533. }
  2534. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int n_per_row, const float * quant_weights) {
  2535. #if QK_K != 256
  2536. (void)quant_weights;
  2537. quantize_row_q6_K_reference(x, y, n_per_row);
  2538. #else
  2539. assert(n_per_row % QK_K == 0);
  2540. const int nb = n_per_row / QK_K;
  2541. int8_t L[QK_K];
  2542. float scales[QK_K/16];
  2543. //float weights[16];
  2544. for (int i = 0; i < nb; i++) {
  2545. //float sum_x2 = 0;
  2546. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2547. //float sigma2 = sum_x2/QK_K;
  2548. float max_scale = 0;
  2549. float max_abs_scale = 0;
  2550. for (int ib = 0; ib < QK_K/16; ++ib) {
  2551. float scale;
  2552. if (quant_weights) {
  2553. const float * qw = quant_weights + QK_K*i + 16*ib;
  2554. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2555. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2556. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2557. } else {
  2558. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2559. }
  2560. scales[ib] = scale;
  2561. const float abs_scale = fabsf(scale);
  2562. if (abs_scale > max_abs_scale) {
  2563. max_abs_scale = abs_scale;
  2564. max_scale = scale;
  2565. }
  2566. }
  2567. if (!max_abs_scale) {
  2568. memset(&y[i], 0, sizeof(block_q6_K));
  2569. y[i].d = GGML_FP32_TO_FP16(0.f);
  2570. x += QK_K;
  2571. continue;
  2572. }
  2573. float iscale = -128.f/max_scale;
  2574. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2575. for (int ib = 0; ib < QK_K/16; ++ib) {
  2576. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2577. }
  2578. for (int j = 0; j < QK_K/16; ++j) {
  2579. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2580. if (!d) {
  2581. continue;
  2582. }
  2583. for (int ii = 0; ii < 16; ++ii) {
  2584. int l = nearest_int(x[16*j + ii]/d);
  2585. l = MAX(-32, MIN(31, l));
  2586. L[16*j + ii] = l + 32;
  2587. }
  2588. }
  2589. uint8_t * restrict ql = y[i].ql;
  2590. uint8_t * restrict qh = y[i].qh;
  2591. for (int j = 0; j < QK_K; j += 128) {
  2592. for (int l = 0; l < 32; ++l) {
  2593. const uint8_t q1 = L[j + l + 0] & 0xF;
  2594. const uint8_t q2 = L[j + l + 32] & 0xF;
  2595. const uint8_t q3 = L[j + l + 64] & 0xF;
  2596. const uint8_t q4 = L[j + l + 96] & 0xF;
  2597. ql[l+ 0] = q1 | (q3 << 4);
  2598. ql[l+32] = q2 | (q4 << 4);
  2599. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2600. }
  2601. ql += 64;
  2602. qh += 32;
  2603. }
  2604. x += QK_K;
  2605. }
  2606. #endif
  2607. }
  2608. size_t quantize_q6_K(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2609. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2610. if (!quant_weights) {
  2611. quantize_row_q6_K_reference(src, dst, nrow*n_per_row);
  2612. }
  2613. else {
  2614. char * qrow = (char *)dst;
  2615. for (int row = 0; row < nrow; ++row) {
  2616. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2617. src += n_per_row;
  2618. qrow += row_size;
  2619. }
  2620. }
  2621. return nrow * row_size;
  2622. }
  2623. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int n_per_row, const float * quant_weights) {
  2624. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2625. if (!quant_weights) {
  2626. quantize_row_q4_0_reference(x, y, n_per_row);
  2627. return;
  2628. }
  2629. float weight[QK4_0];
  2630. int8_t L[QK4_0];
  2631. float sum_x2 = 0;
  2632. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2633. float sigma2 = sum_x2/n_per_row;
  2634. const int nb = n_per_row/QK4_0;
  2635. for (int ib = 0; ib < nb; ++ib) {
  2636. const float * xb = x + QK4_0 * ib;
  2637. const float * qw = quant_weights + QK4_0 * ib;
  2638. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2639. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2640. y[ib].d = GGML_FP32_TO_FP16(d);
  2641. for (int j = 0; j < 16; ++j) {
  2642. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2643. }
  2644. }
  2645. }
  2646. size_t quantize_q4_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2647. if (!quant_weights) {
  2648. quantize_row_q4_0_reference(src, dst, nrow*n_per_row);
  2649. return nrow * ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2650. }
  2651. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2652. char * qrow = (char *)dst;
  2653. for (int row = 0; row < nrow; ++row) {
  2654. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2655. src += n_per_row;
  2656. qrow += row_size;
  2657. }
  2658. return nrow * row_size;
  2659. }
  2660. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int n_per_row, const float * quant_weights) {
  2661. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2662. if (!quant_weights) {
  2663. quantize_row_q4_1_reference(x, y, n_per_row);
  2664. return;
  2665. }
  2666. float weight[QK4_1];
  2667. uint8_t L[QK4_1], Laux[QK4_1];
  2668. float sum_x2 = 0;
  2669. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2670. float sigma2 = sum_x2/n_per_row;
  2671. const int nb = n_per_row/QK4_1;
  2672. for (int ib = 0; ib < nb; ++ib) {
  2673. const float * xb = x + QK4_1 * ib;
  2674. const float * qw = quant_weights + QK4_1 * ib;
  2675. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2676. float min;
  2677. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2678. y[ib].d = GGML_FP32_TO_FP16(d);
  2679. y[ib].m = GGML_FP32_TO_FP16(-min);
  2680. for (int j = 0; j < 16; ++j) {
  2681. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2682. }
  2683. }
  2684. }
  2685. size_t quantize_q4_1(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2686. if (!quant_weights) {
  2687. quantize_row_q4_1_reference(src, dst, nrow*n_per_row);
  2688. return nrow * ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2689. }
  2690. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2691. char * qrow = (char *)dst;
  2692. for (int row = 0; row < nrow; ++row) {
  2693. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2694. src += n_per_row;
  2695. qrow += row_size;
  2696. }
  2697. return nrow * row_size;
  2698. }
  2699. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int n_per_row, const float * quant_weights) {
  2700. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2701. if (!quant_weights) {
  2702. quantize_row_q5_0_reference(x, y, n_per_row);
  2703. return;
  2704. }
  2705. float weight[QK5_0];
  2706. int8_t L[QK5_0];
  2707. float sum_x2 = 0;
  2708. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2709. float sigma2 = sum_x2/n_per_row;
  2710. const int nb = n_per_row/QK5_0;
  2711. for (int ib = 0; ib < nb; ++ib) {
  2712. const float * xb = x + QK5_0 * ib;
  2713. const float * qw = quant_weights + QK5_0 * ib;
  2714. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2715. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2716. y[ib].d = GGML_FP32_TO_FP16(d);
  2717. uint32_t qh = 0;
  2718. for (int j = 0; j < 16; ++j) {
  2719. const uint8_t xi0 = L[j];
  2720. const uint8_t xi1 = L[j+16];
  2721. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2722. // get the 5-th bit and store it in qh at the right position
  2723. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2724. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2725. }
  2726. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2727. }
  2728. }
  2729. size_t quantize_q5_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2730. if (!quant_weights) {
  2731. quantize_row_q5_0_reference(src, dst, nrow*n_per_row);
  2732. return nrow * ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2733. }
  2734. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2735. char * qrow = (char *)dst;
  2736. for (int row = 0; row < nrow; ++row) {
  2737. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2738. src += n_per_row;
  2739. qrow += row_size;
  2740. }
  2741. return nrow * row_size;
  2742. }
  2743. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int n_per_row, const float * quant_weights) {
  2744. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2745. if (!quant_weights) {
  2746. quantize_row_q5_1_reference(x, y, n_per_row);
  2747. return;
  2748. }
  2749. float weight[QK5_1];
  2750. uint8_t L[QK5_1], Laux[QK5_1];
  2751. float sum_x2 = 0;
  2752. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2753. float sigma2 = sum_x2/n_per_row;
  2754. const int nb = n_per_row/QK5_1;
  2755. for (int ib = 0; ib < nb; ++ib) {
  2756. const float * xb = x + QK5_1 * ib;
  2757. const float * qw = quant_weights + QK5_1 * ib;
  2758. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2759. float min;
  2760. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2761. y[ib].d = GGML_FP32_TO_FP16(d);
  2762. y[ib].m = GGML_FP32_TO_FP16(-min);
  2763. uint32_t qh = 0;
  2764. for (int j = 0; j < 16; ++j) {
  2765. const uint8_t xi0 = L[j];
  2766. const uint8_t xi1 = L[j+16];
  2767. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2768. // get the 5-th bit and store it in qh at the right position
  2769. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2770. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2771. }
  2772. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2773. }
  2774. }
  2775. size_t quantize_q5_1(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2776. if (!quant_weights) {
  2777. quantize_row_q5_1_reference(src, dst, nrow*n_per_row);
  2778. return nrow * ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2779. }
  2780. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2781. char * qrow = (char *)dst;
  2782. for (int row = 0; row < nrow; ++row) {
  2783. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2784. src += n_per_row;
  2785. qrow += row_size;
  2786. }
  2787. return nrow * row_size;
  2788. }
  2789. size_t quantize_q8_0(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  2790. (void)quant_weights; // not used
  2791. const size_t row_size = ggml_row_size(GGML_TYPE_Q8_0, n_per_row);
  2792. quantize_row_q8_0_reference(src, dst, nrow*n_per_row);
  2793. return nrow * row_size;
  2794. }
  2795. // ====================== "True" 2-bit (de)-quantization
  2796. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2797. assert(k % QK_K == 0);
  2798. const int nb = k / QK_K;
  2799. uint32_t aux32[2];
  2800. const uint8_t * aux8 = (const uint8_t *)aux32;
  2801. for (int i = 0; i < nb; i++) {
  2802. const float d = GGML_FP16_TO_FP32(x[i].d);
  2803. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2804. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2805. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2806. for (int l = 0; l < 4; ++l) {
  2807. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2808. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2809. for (int j = 0; j < 8; ++j) {
  2810. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2811. }
  2812. y += 8;
  2813. }
  2814. }
  2815. }
  2816. }
  2817. // ====================== 2.3125 bpw (de)-quantization
  2818. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2819. assert(k % QK_K == 0);
  2820. const int nb = k / QK_K;
  2821. float db[2];
  2822. for (int i = 0; i < nb; i++) {
  2823. const float d = GGML_FP16_TO_FP32(x[i].d);
  2824. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2825. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2826. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2827. for (int l = 0; l < 4; ++l) {
  2828. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  2829. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  2830. for (int j = 0; j < 8; ++j) {
  2831. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2832. }
  2833. y += 8;
  2834. }
  2835. }
  2836. }
  2837. }
  2838. // ====================== 2.5625 bpw (de)-quantization
  2839. void dequantize_row_iq2_s(const block_iq2_s * restrict x, float * restrict y, int k) {
  2840. assert(k % QK_K == 0);
  2841. const int nb = k / QK_K;
  2842. float db[2];
  2843. for (int i = 0; i < nb; i++) {
  2844. const float d = GGML_FP16_TO_FP32(x[i].d);
  2845. const uint8_t * qs = x[i].qs;
  2846. const uint8_t * qh = x[i].qh;
  2847. const uint8_t * signs = qs + QK_K/8;
  2848. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2849. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  2850. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  2851. for (int l = 0; l < 4; ++l) {
  2852. const float dl = db[l/2];
  2853. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  2854. for (int j = 0; j < 8; ++j) {
  2855. y[j] = dl * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1.f : 1.f);
  2856. }
  2857. y += 8;
  2858. }
  2859. qs += 4;
  2860. signs += 4;
  2861. }
  2862. }
  2863. }
  2864. // ====================== 3.0625 bpw (de)-quantization
  2865. void dequantize_row_iq3_xxs(const block_iq3_xxs * restrict x, float * restrict y, int k) {
  2866. assert(k % QK_K == 0);
  2867. const int nb = k / QK_K;
  2868. uint32_t aux32;
  2869. for (int i = 0; i < nb; i++) {
  2870. const float d = GGML_FP16_TO_FP32(x[i].d);
  2871. const uint8_t * qs = x[i].qs;
  2872. const uint8_t * scales_and_signs = qs + QK_K/4;
  2873. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2874. memcpy(&aux32, scales_and_signs + 4*ib32, sizeof(uint32_t));
  2875. const float db = d * (0.5f + (aux32 >> 28)) * 0.5f;
  2876. for (int l = 0; l < 4; ++l) {
  2877. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  2878. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + qs[2*l+0]);
  2879. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + qs[2*l+1]);
  2880. for (int j = 0; j < 4; ++j) {
  2881. y[j+0] = db * grid1[j] * (signs & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2882. y[j+4] = db * grid2[j] * (signs & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2883. }
  2884. y += 8;
  2885. }
  2886. qs += 8;
  2887. }
  2888. }
  2889. }
  2890. // ====================== 3.3125 bpw (de)-quantization
  2891. void dequantize_row_iq3_s(const block_iq3_s * restrict x, float * restrict y, int k) {
  2892. assert(k % QK_K == 0);
  2893. const int nb = k / QK_K;
  2894. for (int i = 0; i < nb; i++) {
  2895. const float d = GGML_FP16_TO_FP32(x[i].d);
  2896. const uint8_t * qs = x[i].qs;
  2897. const uint8_t * qh = x[i].qh;
  2898. const uint8_t * signs = x[i].signs;
  2899. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  2900. const float db1 = d * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  2901. const float db2 = d * (1 + 2*(x[i].scales[ib32/2] >> 4));
  2902. for (int l = 0; l < 4; ++l) {
  2903. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[0] << (8-2*l)) & 256)));
  2904. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[0] << (7-2*l)) & 256)));
  2905. for (int j = 0; j < 4; ++j) {
  2906. y[j+0] = db1 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2907. y[j+4] = db1 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2908. }
  2909. y += 8;
  2910. }
  2911. qs += 8;
  2912. signs += 4;
  2913. for (int l = 0; l < 4; ++l) {
  2914. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[1] << (8-2*l)) & 256)));
  2915. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[1] << (7-2*l)) & 256)));
  2916. for (int j = 0; j < 4; ++j) {
  2917. y[j+0] = db2 * grid1[j] * (signs[l] & kmask_iq2xs[j+0] ? -1.f : 1.f);
  2918. y[j+4] = db2 * grid2[j] * (signs[l] & kmask_iq2xs[j+4] ? -1.f : 1.f);
  2919. }
  2920. y += 8;
  2921. }
  2922. qh += 2;
  2923. qs += 8;
  2924. signs += 4;
  2925. }
  2926. }
  2927. }
  2928. // ====================== 1.5625 bpw (de)-quantization
  2929. void dequantize_row_iq1_s(const block_iq1_s * restrict x, float * restrict y, int k) {
  2930. assert(k % QK_K == 0);
  2931. const int nb = k / QK_K;
  2932. for (int i = 0; i < nb; i++) {
  2933. const float d = GGML_FP16_TO_FP32(x[i].d);
  2934. const uint8_t * qs = x[i].qs;
  2935. const uint16_t * qh = x[i].qh;
  2936. for (int ib = 0; ib < QK_K/32; ++ib) {
  2937. const float dl = d * (2*((qh[ib] >> 12) & 7) + 1);
  2938. const float delta = qh[ib] & 0x8000 ? -IQ1S_DELTA : IQ1S_DELTA;
  2939. for (int l = 0; l < 4; ++l) {
  2940. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  2941. for (int j = 0; j < 8; ++j) {
  2942. y[j] = dl * (grid[j] + delta);
  2943. }
  2944. y += 8;
  2945. }
  2946. qs += 4;
  2947. }
  2948. }
  2949. }
  2950. static const int8_t kvalues_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
  2951. void dequantize_row_iq4_nl(const block_iq4_nl * restrict x, float * restrict y, int k) {
  2952. assert(k % QK4_NL == 0);
  2953. const int nb = k / QK4_NL;
  2954. for (int i = 0; i < nb; i++) {
  2955. const uint8_t * qs = x[i].qs;
  2956. const float d = GGML_FP16_TO_FP32(x[i].d);
  2957. for (int j = 0; j < QK4_NL/2; ++j) {
  2958. y[j+ 0] = d * kvalues_iq4nl[qs[j] & 0xf];
  2959. y[j+QK4_NL/2] = d * kvalues_iq4nl[qs[j] >> 4];
  2960. }
  2961. y += QK4_NL;
  2962. qs += QK4_NL/2;
  2963. }
  2964. }
  2965. void dequantize_row_iq4_xs(const block_iq4_xs * restrict x, float * restrict y, int k) {
  2966. assert(k % QK_K == 0);
  2967. #if QK_K == 64
  2968. dequantize_row_iq4_nl((const block_iq4_nl *)x, y, k);
  2969. #else
  2970. const int nb = k / QK_K;
  2971. for (int i = 0; i < nb; i++) {
  2972. const uint8_t * qs = x[i].qs;
  2973. const float d = GGML_FP16_TO_FP32(x[i].d);
  2974. for (int ib = 0; ib < QK_K/32; ++ib) {
  2975. const int ls = ((x[i].scales_l[ib/2] >> 4*(ib%2)) & 0xf) | (((x[i].scales_h >> 2*ib) & 3) << 4);
  2976. const float dl = d * (ls - 32);
  2977. for (int j = 0; j < 16; ++j) {
  2978. y[j+ 0] = dl * kvalues_iq4nl[qs[j] & 0xf];
  2979. y[j+16] = dl * kvalues_iq4nl[qs[j] >> 4];
  2980. }
  2981. y += 32;
  2982. qs += 16;
  2983. }
  2984. }
  2985. #endif
  2986. }
  2987. //===================================== Q8_K ==============================================
  2988. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  2989. assert(k % QK_K == 0);
  2990. const int nb = k / QK_K;
  2991. for (int i = 0; i < nb; i++) {
  2992. float max = 0;
  2993. float amax = 0;
  2994. for (int j = 0; j < QK_K; ++j) {
  2995. float ax = fabsf(x[j]);
  2996. if (ax > amax) {
  2997. amax = ax; max = x[j];
  2998. }
  2999. }
  3000. if (!amax) {
  3001. y[i].d = 0;
  3002. memset(y[i].qs, 0, QK_K);
  3003. x += QK_K;
  3004. continue;
  3005. }
  3006. //const float iscale = -128.f/max;
  3007. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3008. const float iscale = -127.f/max;
  3009. for (int j = 0; j < QK_K; ++j) {
  3010. int v = nearest_int(iscale*x[j]);
  3011. y[i].qs[j] = MIN(127, v);
  3012. }
  3013. for (int j = 0; j < QK_K/16; ++j) {
  3014. int sum = 0;
  3015. for (int ii = 0; ii < 16; ++ii) {
  3016. sum += y[i].qs[j*16 + ii];
  3017. }
  3018. y[i].bsums[j] = sum;
  3019. }
  3020. y[i].d = 1/iscale;
  3021. x += QK_K;
  3022. }
  3023. }
  3024. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  3025. assert(k % QK_K == 0);
  3026. const int nb = k / QK_K;
  3027. for (int i = 0; i < nb; i++) {
  3028. for (int j = 0; j < QK_K; ++j) {
  3029. *y++ = x[i].d * x[i].qs[j];
  3030. }
  3031. }
  3032. }
  3033. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  3034. quantize_row_q8_K_reference(x, y, k);
  3035. }
  3036. //===================================== Dot ptoducts =================================
  3037. //
  3038. // Helper functions
  3039. //
  3040. #if __AVX__ || __AVX2__ || __AVX512F__
  3041. // shuffles to pick the required scales in dot products
  3042. static inline __m256i get_scale_shuffle_q3k(int i) {
  3043. static const uint8_t k_shuffle[128] = {
  3044. 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,
  3045. 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,
  3046. 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,
  3047. 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,
  3048. };
  3049. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3050. }
  3051. static inline __m256i get_scale_shuffle_k4(int i) {
  3052. static const uint8_t k_shuffle[256] = {
  3053. 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,
  3054. 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,
  3055. 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,
  3056. 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,
  3057. 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,
  3058. 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,
  3059. 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,
  3060. 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
  3061. };
  3062. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3063. }
  3064. static inline __m128i get_scale_shuffle(int i) {
  3065. static const uint8_t k_shuffle[128] = {
  3066. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3067. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3068. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3069. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3070. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3071. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3072. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3073. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3074. };
  3075. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3076. }
  3077. #endif
  3078. 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) {
  3079. const int qk = QK8_0;
  3080. const int nb = n / qk;
  3081. assert(n % qk == 0);
  3082. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3083. assert((nrc == 2) || (nrc == 1));
  3084. #else
  3085. assert(nrc == 1);
  3086. #endif
  3087. UNUSED(nrc);
  3088. UNUSED(bx);
  3089. UNUSED(by);
  3090. UNUSED(bs);
  3091. const block_q4_0 * restrict x = vx;
  3092. const block_q8_0 * restrict y = vy;
  3093. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3094. if (nrc == 2) {
  3095. const block_q4_0 * restrict vx0 = vx;
  3096. const block_q4_0 * restrict vx1 = vx + bx;
  3097. const block_q8_0 * restrict vy0 = vy;
  3098. const block_q8_0 * restrict vy1 = vy + by;
  3099. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3100. for (int i = 0; i < nb; i++) {
  3101. const block_q4_0 * restrict b_x0 = &vx0[i];
  3102. const block_q4_0 * restrict b_x1 = &vx1[i];
  3103. const block_q8_0 * restrict b_y0 = &vy0[i];
  3104. const block_q8_0 * restrict b_y1 = &vy1[i];
  3105. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3106. const int8x16_t s8b = vdupq_n_s8(0x8);
  3107. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3108. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3109. // 4-bit -> 8-bit
  3110. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3111. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3112. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3113. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3114. // sub 8
  3115. const int8x16_t x0_l = vsubq_s8(v0_0l, s8b);
  3116. const int8x16_t x0_h = vsubq_s8(v0_0h, s8b);
  3117. const int8x16_t x1_l = vsubq_s8(v0_1l, s8b);
  3118. const int8x16_t x1_h = vsubq_s8(v0_1h, s8b);
  3119. // load y
  3120. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3121. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3122. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3123. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3124. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3125. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  3126. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  3127. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  3128. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3129. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3130. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3131. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3132. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3133. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3134. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3135. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3136. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3137. l1, r1)), l2, r2)), l3, r3))), scale);
  3138. }
  3139. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3140. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3141. vst1_f32(s, vget_low_f32(sumv2));
  3142. vst1_f32(s + bs, vget_high_f32(sumv2));
  3143. return;
  3144. }
  3145. #endif
  3146. #if defined(__ARM_NEON)
  3147. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3148. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3149. assert(nb % 2 == 0); // TODO: handle odd nb
  3150. for (int i = 0; i < nb; i += 2) {
  3151. const block_q4_0 * restrict x0 = &x[i + 0];
  3152. const block_q4_0 * restrict x1 = &x[i + 1];
  3153. const block_q8_0 * restrict y0 = &y[i + 0];
  3154. const block_q8_0 * restrict y1 = &y[i + 1];
  3155. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3156. const int8x16_t s8b = vdupq_n_s8(0x8);
  3157. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3158. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3159. // 4-bit -> 8-bit
  3160. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3161. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3162. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3163. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3164. // sub 8
  3165. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3166. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3167. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3168. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3169. // load y
  3170. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3171. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3172. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3173. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3174. // dot product into int32x4_t
  3175. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3176. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3177. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3178. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3179. }
  3180. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3181. #elif defined(__AVX2__)
  3182. // Initialize accumulator with zeros
  3183. __m256 acc = _mm256_setzero_ps();
  3184. // Main loop
  3185. for (int i = 0; i < nb; ++i) {
  3186. /* Compute combined scale for the block */
  3187. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3188. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3189. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3190. const __m256i off = _mm256_set1_epi8( 8 );
  3191. qx = _mm256_sub_epi8( qx, off );
  3192. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3193. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3194. /* Multiply q with scale and accumulate */
  3195. acc = _mm256_fmadd_ps( d, q, acc );
  3196. }
  3197. *s = hsum_float_8(acc);
  3198. #elif defined(__AVX__)
  3199. // Initialize accumulator with zeros
  3200. __m256 acc = _mm256_setzero_ps();
  3201. // Main loop
  3202. for (int i = 0; i < nb; ++i) {
  3203. // Compute combined scale for the block
  3204. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3205. const __m128i lowMask = _mm_set1_epi8(0xF);
  3206. const __m128i off = _mm_set1_epi8(8);
  3207. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3208. __m128i bx_0 = _mm_and_si128(lowMask, tmp);
  3209. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3210. bx_0 = _mm_sub_epi8(bx_0, off);
  3211. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3212. bx_0 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3213. by_0 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3214. bx_0 = _mm_sub_epi8(bx_0, off);
  3215. const __m128i i32_1 = mul_sum_i8_pairs(bx_0, by_0);
  3216. // Convert int32_t to float
  3217. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3218. // Apply the scale, and accumulate
  3219. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3220. }
  3221. *s = hsum_float_8(acc);
  3222. #elif defined(__SSSE3__)
  3223. // set constants
  3224. const __m128i lowMask = _mm_set1_epi8(0xF);
  3225. const __m128i off = _mm_set1_epi8(8);
  3226. // Initialize accumulator with zeros
  3227. __m128 acc_0 = _mm_setzero_ps();
  3228. __m128 acc_1 = _mm_setzero_ps();
  3229. __m128 acc_2 = _mm_setzero_ps();
  3230. __m128 acc_3 = _mm_setzero_ps();
  3231. // First round without accumulation
  3232. {
  3233. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3234. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3235. // Compute combined scale for the block 0 and 1
  3236. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3237. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3238. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3239. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3240. bx_0 = _mm_sub_epi8(bx_0, off);
  3241. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3242. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3243. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3244. bx_1 = _mm_sub_epi8(bx_1, off);
  3245. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3246. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3247. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3248. // Compute combined scale for the block 2 and 3
  3249. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3250. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3251. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3252. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3253. bx_2 = _mm_sub_epi8(bx_2, off);
  3254. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3255. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3256. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3257. bx_3 = _mm_sub_epi8(bx_3, off);
  3258. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3259. // Convert int32_t to float
  3260. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3261. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3262. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3263. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3264. // Apply the scale
  3265. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3266. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3267. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3268. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3269. }
  3270. assert(nb % 2 == 0); // TODO: handle odd nb
  3271. // Main loop
  3272. for (int i = 2; i < nb; i+=2) {
  3273. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3274. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3275. // Compute combined scale for the block 0 and 1
  3276. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3277. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3278. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3279. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3280. bx_0 = _mm_sub_epi8(bx_0, off);
  3281. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3282. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3283. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3284. bx_1 = _mm_sub_epi8(bx_1, off);
  3285. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3286. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3287. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3288. // Compute combined scale for the block 2 and 3
  3289. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3290. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3291. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3292. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3293. bx_2 = _mm_sub_epi8(bx_2, off);
  3294. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3295. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3296. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3297. bx_3 = _mm_sub_epi8(bx_3, off);
  3298. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3299. // Convert int32_t to float
  3300. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3301. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3302. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3303. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3304. // Apply the scale
  3305. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3306. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3307. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3308. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3309. // Acummulate
  3310. acc_0 = _mm_add_ps(p0_d, acc_0);
  3311. acc_1 = _mm_add_ps(p1_d, acc_1);
  3312. acc_2 = _mm_add_ps(p2_d, acc_2);
  3313. acc_3 = _mm_add_ps(p3_d, acc_3);
  3314. }
  3315. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3316. #elif defined(__riscv_v_intrinsic)
  3317. float sumf = 0.0;
  3318. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3319. for (int i = 0; i < nb; i++) {
  3320. // load elements
  3321. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3322. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3323. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3324. // mask and store lower part of x, and then upper part
  3325. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3326. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3327. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3328. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3329. // subtract offset
  3330. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3331. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3332. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3333. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3334. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3335. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3336. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3337. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3338. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3339. }
  3340. *s = sumf;
  3341. #else
  3342. // scalar
  3343. float sumf = 0.0;
  3344. for (int i = 0; i < nb; i++) {
  3345. int sumi = 0;
  3346. for (int j = 0; j < qk/2; ++j) {
  3347. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3348. const int v1 = (x[i].qs[j] >> 4) - 8;
  3349. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3350. }
  3351. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3352. }
  3353. *s = sumf;
  3354. #endif
  3355. }
  3356. 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) {
  3357. const int qk = QK8_1;
  3358. const int nb = n / qk;
  3359. assert(n % qk == 0);
  3360. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3361. assert((nrc == 2) || (nrc == 1));
  3362. #else
  3363. assert(nrc == 1);
  3364. #endif
  3365. UNUSED(nrc);
  3366. UNUSED(bx);
  3367. UNUSED(by);
  3368. UNUSED(bs);
  3369. const block_q4_1 * restrict x = vx;
  3370. const block_q8_1 * restrict y = vy;
  3371. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3372. if (nrc == 2) {
  3373. const block_q4_1 * restrict vx0 = vx;
  3374. const block_q4_1 * restrict vx1 = vx + bx;
  3375. const block_q8_1 * restrict vy0 = vy;
  3376. const block_q8_1 * restrict vy1 = vy + by;
  3377. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3378. float32x4_t summs0 = vdupq_n_f32(0.0f);
  3379. for (int i = 0; i < nb; i++) {
  3380. const block_q4_1 * restrict b_x0 = &vx0[i];
  3381. const block_q4_1 * restrict b_x1 = &vx1[i];
  3382. const block_q8_1 * restrict b_y0 = &vy0[i];
  3383. const block_q8_1 * restrict b_y1 = &vy1[i];
  3384. float32x4_t summs_t = {GGML_FP16_TO_FP32(b_x0->m) * b_y0->s,
  3385. GGML_FP16_TO_FP32(b_x1->m) * b_y0->s,
  3386. GGML_FP16_TO_FP32(b_x0->m) * b_y1->s,
  3387. GGML_FP16_TO_FP32(b_x1->m) * b_y1->s};
  3388. summs0 += summs_t;
  3389. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3390. const uint8x16_t v0_0 = vld1q_u8(b_x0->qs);
  3391. const uint8x16_t v0_1 = vld1q_u8(b_x1->qs);
  3392. // 4-bit -> 8-bit
  3393. const int8x16_t x0_l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3394. const int8x16_t x0_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3395. const int8x16_t x1_l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3396. const int8x16_t x1_h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3397. // load y
  3398. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3399. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3400. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3401. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3402. // mmla into int32x4_t
  3403. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*b_y0->d,
  3404. GGML_FP16_TO_FP32(b_x0->d)*b_y1->d,
  3405. GGML_FP16_TO_FP32(b_x1->d)*b_y0->d,
  3406. GGML_FP16_TO_FP32(b_x1->d)*b_y1->d};
  3407. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3408. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  3409. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3410. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  3411. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3412. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  3413. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3414. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  3415. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  3416. l1, r1)), l2, r2)), l3, r3))), scale);
  3417. }
  3418. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  3419. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  3420. sumv2 = sumv2 + summs0;
  3421. vst1_f32(s, vget_low_f32(sumv2));
  3422. vst1_f32(s + bs, vget_high_f32(sumv2));
  3423. return;
  3424. }
  3425. #endif
  3426. // TODO: add WASM SIMD
  3427. #if defined(__ARM_NEON)
  3428. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3429. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3430. float summs = 0;
  3431. assert(nb % 2 == 0); // TODO: handle odd nb
  3432. for (int i = 0; i < nb; i += 2) {
  3433. const block_q4_1 * restrict x0 = &x[i + 0];
  3434. const block_q4_1 * restrict x1 = &x[i + 1];
  3435. const block_q8_1 * restrict y0 = &y[i + 0];
  3436. const block_q8_1 * restrict y1 = &y[i + 1];
  3437. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  3438. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3439. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3440. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3441. // 4-bit -> 8-bit
  3442. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3443. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3444. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3445. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3446. // load y
  3447. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3448. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3449. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3450. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3451. // dot product into int32x4_t
  3452. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3453. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3454. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3455. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3456. }
  3457. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3458. #elif defined(__AVX2__) || defined(__AVX__)
  3459. // Initialize accumulator with zeros
  3460. __m256 acc = _mm256_setzero_ps();
  3461. float summs = 0;
  3462. // Main loop
  3463. for (int i = 0; i < nb; ++i) {
  3464. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3465. const float d1 = y[i].d;
  3466. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3467. const __m256 d0v = _mm256_set1_ps( d0 );
  3468. const __m256 d1v = _mm256_set1_ps( d1 );
  3469. // Compute combined scales
  3470. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3471. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3472. const __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3473. const __m256i qy = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3474. const __m256 xy = mul_sum_us8_pairs_float(qx, qy);
  3475. // Accumulate d0*d1*x*y
  3476. #if defined(__AVX2__)
  3477. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3478. #else
  3479. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3480. #endif
  3481. }
  3482. *s = hsum_float_8(acc) + summs;
  3483. #elif defined(__riscv_v_intrinsic)
  3484. float sumf = 0.0;
  3485. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3486. for (int i = 0; i < nb; i++) {
  3487. // load elements
  3488. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3489. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3490. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3491. // mask and store lower part of x, and then upper part
  3492. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3493. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3494. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3495. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3496. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3497. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3498. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3499. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3500. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3501. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3502. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3503. }
  3504. *s = sumf;
  3505. #else
  3506. // scalar
  3507. float sumf = 0.0;
  3508. for (int i = 0; i < nb; i++) {
  3509. int sumi = 0;
  3510. for (int j = 0; j < qk/2; ++j) {
  3511. const int v0 = (x[i].qs[j] & 0x0F);
  3512. const int v1 = (x[i].qs[j] >> 4);
  3513. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3514. }
  3515. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3516. }
  3517. *s = sumf;
  3518. #endif
  3519. }
  3520. 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) {
  3521. const int qk = QK8_0;
  3522. const int nb = n / qk;
  3523. assert(n % qk == 0);
  3524. assert(qk == QK5_0);
  3525. assert(nrc == 1);
  3526. UNUSED(nrc);
  3527. UNUSED(bx);
  3528. UNUSED(by);
  3529. UNUSED(bs);
  3530. const block_q5_0 * restrict x = vx;
  3531. const block_q8_0 * restrict y = vy;
  3532. #if defined(__ARM_NEON)
  3533. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3534. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3535. uint32_t qh0;
  3536. uint32_t qh1;
  3537. uint64_t tmp0[4];
  3538. uint64_t tmp1[4];
  3539. assert(nb % 2 == 0); // TODO: handle odd nb
  3540. for (int i = 0; i < nb; i += 2) {
  3541. const block_q5_0 * restrict x0 = &x[i];
  3542. const block_q5_0 * restrict x1 = &x[i + 1];
  3543. const block_q8_0 * restrict y0 = &y[i];
  3544. const block_q8_0 * restrict y1 = &y[i + 1];
  3545. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3546. // extract the 5th bit via lookup table ((!b) << 4)
  3547. memcpy(&qh0, x0->qh, sizeof(qh0));
  3548. memcpy(&qh1, x1->qh, sizeof(qh1));
  3549. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3550. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3551. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3552. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3553. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3554. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3555. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3556. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3557. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3558. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3559. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3560. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3561. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3562. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3563. // 4-bit -> 8-bit
  3564. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3565. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3566. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3567. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3568. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3569. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3570. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3571. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3572. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3573. // load y
  3574. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3575. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3576. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3577. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3578. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3579. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3580. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3581. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3582. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3583. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3584. }
  3585. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3586. #elif defined(__wasm_simd128__)
  3587. v128_t sumv = wasm_f32x4_splat(0.0f);
  3588. uint32_t qh;
  3589. uint64_t tmp[4];
  3590. // TODO: check if unrolling this is better
  3591. for (int i = 0; i < nb; ++i) {
  3592. const block_q5_0 * restrict x0 = &x[i];
  3593. const block_q8_0 * restrict y0 = &y[i];
  3594. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3595. // extract the 5th bit
  3596. memcpy(&qh, x0->qh, sizeof(qh));
  3597. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3598. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3599. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3600. tmp[3] = table_b2b_1[(qh >> 24) ];
  3601. const v128_t qhl = wasm_v128_load(tmp + 0);
  3602. const v128_t qhh = wasm_v128_load(tmp + 2);
  3603. const v128_t v0 = wasm_v128_load(x0->qs);
  3604. // 4-bit -> 8-bit
  3605. const v128_t v0l = wasm_v128_and (v0, m4b);
  3606. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3607. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3608. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3609. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3610. // load y
  3611. const v128_t v1l = wasm_v128_load(y0->qs);
  3612. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3613. // int8x16 -> int16x8
  3614. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3615. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3616. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3617. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3618. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3619. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3620. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3621. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3622. // dot product
  3623. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3624. wasm_i32x4_add(
  3625. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3626. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3627. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3628. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3629. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3630. }
  3631. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3632. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3633. #elif defined(__AVX2__)
  3634. // Initialize accumulator with zeros
  3635. __m256 acc = _mm256_setzero_ps();
  3636. // Main loop
  3637. for (int i = 0; i < nb; i++) {
  3638. /* Compute combined scale for the block */
  3639. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3640. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3641. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3642. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3643. qx = _mm256_or_si256(qx, bxhi);
  3644. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3645. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  3646. /* Multiply q with scale and accumulate */
  3647. acc = _mm256_fmadd_ps(d, q, acc);
  3648. }
  3649. *s = hsum_float_8(acc);
  3650. #elif defined(__AVX__)
  3651. // Initialize accumulator with zeros
  3652. __m256 acc = _mm256_setzero_ps();
  3653. __m128i mask = _mm_set1_epi8((char)0xF0);
  3654. // Main loop
  3655. for (int i = 0; i < nb; i++) {
  3656. /* Compute combined scale for the block */
  3657. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3658. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3659. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3660. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3661. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3662. bxhil = _mm_andnot_si128(bxhil, mask);
  3663. bxhih = _mm_andnot_si128(bxhih, mask);
  3664. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3665. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3666. bxl = _mm_or_si128(bxl, bxhil);
  3667. bxh = _mm_or_si128(bxh, bxhih);
  3668. bx_0 = MM256_SET_M128I(bxh, bxl);
  3669. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3670. const __m256 q = mul_sum_i8_pairs_float(bx_0, by_0);
  3671. /* Multiply q with scale and accumulate */
  3672. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3673. }
  3674. *s = hsum_float_8(acc);
  3675. #elif defined(__riscv_v_intrinsic)
  3676. float sumf = 0.0;
  3677. uint32_t qh;
  3678. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3679. // These temporary registers are for masking and shift operations
  3680. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3681. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3682. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3683. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3684. for (int i = 0; i < nb; i++) {
  3685. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3686. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3687. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3688. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3689. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3690. // ((qh & (1u << (j + 16))) >> (j + 12));
  3691. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3692. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3693. // narrowing
  3694. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3695. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3696. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3697. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3698. // load
  3699. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3700. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3701. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3702. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3703. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3704. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3705. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3706. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3707. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3708. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3709. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3710. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3711. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3712. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3713. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3714. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3715. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3716. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3717. }
  3718. *s = sumf;
  3719. #else
  3720. // scalar
  3721. float sumf = 0.0;
  3722. for (int i = 0; i < nb; i++) {
  3723. uint32_t qh;
  3724. memcpy(&qh, x[i].qh, sizeof(qh));
  3725. int sumi = 0;
  3726. for (int j = 0; j < qk/2; ++j) {
  3727. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3728. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3729. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3730. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3731. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3732. }
  3733. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3734. }
  3735. *s = sumf;
  3736. #endif
  3737. }
  3738. 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) {
  3739. const int qk = QK8_1;
  3740. const int nb = n / qk;
  3741. assert(n % qk == 0);
  3742. assert(qk == QK5_1);
  3743. assert(nrc == 1);
  3744. UNUSED(nrc);
  3745. UNUSED(bx);
  3746. UNUSED(by);
  3747. UNUSED(bs);
  3748. const block_q5_1 * restrict x = vx;
  3749. const block_q8_1 * restrict y = vy;
  3750. #if defined(__ARM_NEON)
  3751. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3752. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3753. float summs0 = 0.0f;
  3754. float summs1 = 0.0f;
  3755. uint32_t qh0;
  3756. uint32_t qh1;
  3757. uint64_t tmp0[4];
  3758. uint64_t tmp1[4];
  3759. assert(nb % 2 == 0); // TODO: handle odd nb
  3760. for (int i = 0; i < nb; i += 2) {
  3761. const block_q5_1 * restrict x0 = &x[i];
  3762. const block_q5_1 * restrict x1 = &x[i + 1];
  3763. const block_q8_1 * restrict y0 = &y[i];
  3764. const block_q8_1 * restrict y1 = &y[i + 1];
  3765. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3766. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3767. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  3768. // extract the 5th bit via lookup table ((b) << 4)
  3769. memcpy(&qh0, x0->qh, sizeof(qh0));
  3770. memcpy(&qh1, x1->qh, sizeof(qh1));
  3771. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3772. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3773. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3774. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3775. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3776. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3777. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3778. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3779. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3780. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3781. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3782. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3783. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3784. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3785. // 4-bit -> 8-bit
  3786. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3787. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3788. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3789. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3790. // add high bit
  3791. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3792. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3793. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3794. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3795. // load y
  3796. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3797. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3798. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3799. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3800. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3801. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3802. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3803. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3804. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3805. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3806. }
  3807. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3808. #elif defined(__wasm_simd128__)
  3809. v128_t sumv = wasm_f32x4_splat(0.0f);
  3810. float summs = 0.0f;
  3811. uint32_t qh;
  3812. uint64_t tmp[4];
  3813. // TODO: check if unrolling this is better
  3814. for (int i = 0; i < nb; ++i) {
  3815. const block_q5_1 * restrict x0 = &x[i];
  3816. const block_q8_1 * restrict y0 = &y[i];
  3817. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3818. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3819. // extract the 5th bit
  3820. memcpy(&qh, x0->qh, sizeof(qh));
  3821. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3822. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3823. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3824. tmp[3] = table_b2b_0[(qh >> 24) ];
  3825. const v128_t qhl = wasm_v128_load(tmp + 0);
  3826. const v128_t qhh = wasm_v128_load(tmp + 2);
  3827. const v128_t v0 = wasm_v128_load(x0->qs);
  3828. // 4-bit -> 8-bit
  3829. const v128_t v0l = wasm_v128_and (v0, m4b);
  3830. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3831. // add high bit
  3832. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3833. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3834. // load y
  3835. const v128_t v1l = wasm_v128_load(y0->qs);
  3836. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3837. // int8x16 -> int16x8
  3838. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3839. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3840. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3841. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3842. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3843. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3844. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3845. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3846. // dot product
  3847. sumv = wasm_f32x4_add(sumv,
  3848. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3849. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3850. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3851. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3852. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3853. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  3854. }
  3855. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3856. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3857. #elif defined(__AVX2__)
  3858. // Initialize accumulator with zeros
  3859. __m256 acc = _mm256_setzero_ps();
  3860. float summs = 0.0f;
  3861. // Main loop
  3862. for (int i = 0; i < nb; i++) {
  3863. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3864. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3865. __m256i qx = bytes_from_nibbles_32(x[i].qs);
  3866. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3867. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3868. qx = _mm256_or_si256(qx, bxhi);
  3869. const __m256 dy = _mm256_set1_ps(y[i].d);
  3870. const __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3871. const __m256 q = mul_sum_us8_pairs_float(qx, qy);
  3872. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3873. }
  3874. *s = hsum_float_8(acc) + summs;
  3875. #elif defined(__AVX__)
  3876. // Initialize accumulator with zeros
  3877. __m256 acc = _mm256_setzero_ps();
  3878. __m128i mask = _mm_set1_epi8(0x10);
  3879. float summs = 0.0f;
  3880. // Main loop
  3881. for (int i = 0; i < nb; i++) {
  3882. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3883. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3884. __m256i bx_0 = bytes_from_nibbles_32(x[i].qs);
  3885. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3886. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3887. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3888. bxhil = _mm_and_si128(bxhil, mask);
  3889. bxhih = _mm_and_si128(bxhih, mask);
  3890. __m128i bxl = _mm256_castsi256_si128(bx_0);
  3891. __m128i bxh = _mm256_extractf128_si256(bx_0, 1);
  3892. bxl = _mm_or_si128(bxl, bxhil);
  3893. bxh = _mm_or_si128(bxh, bxhih);
  3894. bx_0 = MM256_SET_M128I(bxh, bxl);
  3895. const __m256 dy = _mm256_set1_ps(y[i].d);
  3896. const __m256i by_0 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3897. const __m256 q = mul_sum_us8_pairs_float(bx_0, by_0);
  3898. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3899. }
  3900. *s = hsum_float_8(acc) + summs;
  3901. #elif defined(__riscv_v_intrinsic)
  3902. float sumf = 0.0;
  3903. uint32_t qh;
  3904. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3905. // temporary registers for shift operations
  3906. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3907. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3908. for (int i = 0; i < nb; i++) {
  3909. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3910. // load qh
  3911. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3912. // ((qh >> (j + 0)) << 4) & 0x10;
  3913. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3914. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3915. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3916. // ((qh >> (j + 12)) ) & 0x10;
  3917. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3918. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3919. // narrowing
  3920. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3921. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3922. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3923. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3924. // load
  3925. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3926. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3927. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3928. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3929. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3930. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3931. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3932. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3933. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3934. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3935. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3936. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3937. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3938. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3939. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3940. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3941. }
  3942. *s = sumf;
  3943. #else
  3944. // scalar
  3945. float sumf = 0.0;
  3946. for (int i = 0; i < nb; i++) {
  3947. uint32_t qh;
  3948. memcpy(&qh, x[i].qh, sizeof(qh));
  3949. int sumi = 0;
  3950. for (int j = 0; j < qk/2; ++j) {
  3951. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3952. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3953. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3954. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3955. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3956. }
  3957. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3958. }
  3959. *s = sumf;
  3960. #endif
  3961. }
  3962. 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) {
  3963. const int qk = QK8_0;
  3964. const int nb = n / qk;
  3965. assert(n % qk == 0);
  3966. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3967. assert((nrc == 2) || (nrc == 1));
  3968. #else
  3969. assert(nrc == 1);
  3970. #endif
  3971. UNUSED(nrc);
  3972. UNUSED(bx);
  3973. UNUSED(by);
  3974. UNUSED(bs);
  3975. const block_q8_0 * restrict x = vx;
  3976. const block_q8_0 * restrict y = vy;
  3977. #if defined(__ARM_FEATURE_MATMUL_INT8)
  3978. if (nrc == 2) {
  3979. const block_q8_0 * restrict vx0 = vx;
  3980. const block_q8_0 * restrict vx1 = vx + bx;
  3981. const block_q8_0 * restrict vy0 = vy;
  3982. const block_q8_0 * restrict vy1 = vy + by;
  3983. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3984. for (int i = 0; i < nb; i++) {
  3985. const block_q8_0 * restrict b_x0 = &vx0[i];
  3986. const block_q8_0 * restrict b_y0 = &vy0[i];
  3987. const block_q8_0 * restrict b_x1 = &vx1[i];
  3988. const block_q8_0 * restrict b_y1 = &vy1[i];
  3989. const int8x16_t x0_l = vld1q_s8(b_x0->qs);
  3990. const int8x16_t x0_h = vld1q_s8(b_x0->qs + 16);
  3991. const int8x16_t x1_l = vld1q_s8(b_x1->qs);
  3992. const int8x16_t x1_h = vld1q_s8(b_x1->qs + 16);
  3993. // load y
  3994. const int8x16_t y0_l = vld1q_s8(b_y0->qs);
  3995. const int8x16_t y0_h = vld1q_s8(b_y0->qs + 16);
  3996. const int8x16_t y1_l = vld1q_s8(b_y1->qs);
  3997. const int8x16_t y1_h = vld1q_s8(b_y1->qs + 16);
  3998. float32x4_t scale = {GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y0->d),
  3999. GGML_FP16_TO_FP32(b_x0->d)*GGML_FP16_TO_FP32(b_y1->d),
  4000. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y0->d),
  4001. GGML_FP16_TO_FP32(b_x1->d)*GGML_FP16_TO_FP32(b_y1->d)};
  4002. int8x16_t l0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4003. int8x16_t l1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_l), vreinterpretq_s64_s8(x1_l)));
  4004. int8x16_t l2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4005. int8x16_t l3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(x0_h), vreinterpretq_s64_s8(x1_h)));
  4006. int8x16_t r0 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4007. int8x16_t r1 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_l), vreinterpretq_s64_s8(y1_l)));
  4008. int8x16_t r2 = vreinterpretq_s8_s64(vzip1q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4009. int8x16_t r3 = vreinterpretq_s8_s64(vzip2q_s64(vreinterpretq_s64_s8(y0_h), vreinterpretq_s64_s8(y1_h)));
  4010. sumv0 = vmlaq_f32(sumv0,(vcvtq_f32_s32(vmmlaq_s32((vmmlaq_s32((vmmlaq_s32((vmmlaq_s32(vdupq_n_s32(0), l0, r0)),
  4011. l1, r1)), l2, r2)), l3, r3))), scale);
  4012. }
  4013. float32x4_t sumv1 = vextq_f32(sumv0, sumv0, 2);
  4014. float32x4_t sumv2 = vzip1q_f32(sumv0, sumv1);
  4015. vst1_f32(s, vget_low_f32(sumv2));
  4016. vst1_f32(s + bs, vget_high_f32(sumv2));
  4017. return;
  4018. }
  4019. #endif
  4020. #if defined(__ARM_NEON)
  4021. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  4022. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  4023. assert(nb % 2 == 0); // TODO: handle odd nb
  4024. for (int i = 0; i < nb; i += 2) {
  4025. const block_q8_0 * restrict x0 = &x[i + 0];
  4026. const block_q8_0 * restrict x1 = &x[i + 1];
  4027. const block_q8_0 * restrict y0 = &y[i + 0];
  4028. const block_q8_0 * restrict y1 = &y[i + 1];
  4029. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  4030. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  4031. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  4032. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  4033. // load y
  4034. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  4035. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  4036. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  4037. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  4038. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  4039. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  4040. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  4041. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  4042. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  4043. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  4044. }
  4045. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  4046. #elif defined(__AVX2__) || defined(__AVX__)
  4047. // Initialize accumulator with zeros
  4048. __m256 acc = _mm256_setzero_ps();
  4049. // Main loop
  4050. for (int i = 0; i < nb; ++i) {
  4051. // Compute combined scale for the block
  4052. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  4053. __m256i qx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  4054. __m256i qy = _mm256_loadu_si256((const __m256i *)y[i].qs);
  4055. const __m256 q = mul_sum_i8_pairs_float(qx, qy);
  4056. // Multiply q with scale and accumulate
  4057. #if defined(__AVX2__)
  4058. acc = _mm256_fmadd_ps( d, q, acc );
  4059. #else
  4060. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  4061. #endif
  4062. }
  4063. *s = hsum_float_8(acc);
  4064. #elif defined(__riscv_v_intrinsic)
  4065. float sumf = 0.0;
  4066. size_t vl = __riscv_vsetvl_e8m1(qk);
  4067. for (int i = 0; i < nb; i++) {
  4068. // load elements
  4069. vint8m1_t bx_0 = __riscv_vle8_v_i8m1(x[i].qs, vl);
  4070. vint8m1_t by_0 = __riscv_vle8_v_i8m1(y[i].qs, vl);
  4071. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx_0, by_0, vl);
  4072. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  4073. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  4074. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  4075. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4076. }
  4077. *s = sumf;
  4078. #else
  4079. // scalar
  4080. float sumf = 0.0;
  4081. for (int i = 0; i < nb; i++) {
  4082. int sumi = 0;
  4083. for (int j = 0; j < qk; j++) {
  4084. sumi += x[i].qs[j]*y[i].qs[j];
  4085. }
  4086. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  4087. }
  4088. *s = sumf;
  4089. #endif
  4090. }
  4091. #if QK_K == 256
  4092. 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) {
  4093. assert(nrc == 1);
  4094. UNUSED(nrc);
  4095. UNUSED(bx);
  4096. UNUSED(by);
  4097. UNUSED(bs);
  4098. const block_q2_K * restrict x = vx;
  4099. const block_q8_K * restrict y = vy;
  4100. const int nb = n / QK_K;
  4101. #ifdef __ARM_NEON
  4102. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4103. const uint8x16_t m4 = vdupq_n_u8(0xF);
  4104. const int32x4_t vzero = vdupq_n_s32(0);
  4105. ggml_int8x16x2_t q2bytes;
  4106. uint8_t aux[16];
  4107. float sum = 0;
  4108. for (int i = 0; i < nb; ++i) {
  4109. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4110. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4111. const uint8_t * restrict q2 = x[i].qs;
  4112. const int8_t * restrict q8 = y[i].qs;
  4113. const uint8_t * restrict sc = x[i].scales;
  4114. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  4115. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  4116. vst1q_u8(aux, scales);
  4117. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  4118. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  4119. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  4120. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  4121. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  4122. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  4123. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  4124. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  4125. int isum = 0;
  4126. int is = 0;
  4127. // We use this macro instead of a function call because for some reason
  4128. // the code runs 2-3% slower, even if the function is declared inline
  4129. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  4130. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  4131. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  4132. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  4133. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  4134. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  4135. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  4136. MULTIPLY_ACCUM_WITH_SCALE((index));
  4137. for (int j = 0; j < QK_K/128; ++j) {
  4138. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  4139. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4140. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  4141. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  4142. MULTIPLY_ACCUM_WITH_SCALE(0);
  4143. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  4144. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  4145. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  4146. is += 8;
  4147. }
  4148. sum += d * isum;
  4149. }
  4150. *s = sum;
  4151. #elif defined __AVX2__
  4152. const __m256i m3 = _mm256_set1_epi8(3);
  4153. const __m128i m4 = _mm_set1_epi8(0xF);
  4154. __m256 acc = _mm256_setzero_ps();
  4155. for (int i = 0; i < nb; ++i) {
  4156. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4157. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4158. const uint8_t * restrict q2 = x[i].qs;
  4159. const int8_t * restrict q8 = y[i].qs;
  4160. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4161. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  4162. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4163. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  4164. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  4165. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  4166. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  4167. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4168. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4169. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4170. __m256i sumi = _mm256_setzero_si256();
  4171. for (int j = 0; j < QK_K/128; ++j) {
  4172. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4173. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4174. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4175. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4176. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4177. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4178. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4179. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4180. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4181. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4182. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4183. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4184. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4185. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4186. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4187. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4188. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4189. p0 = _mm256_add_epi32(p0, p1);
  4190. p2 = _mm256_add_epi32(p2, p3);
  4191. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4192. }
  4193. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4194. }
  4195. *s = hsum_float_8(acc);
  4196. #elif defined __AVX__
  4197. const __m128i m3 = _mm_set1_epi8(0x3);
  4198. const __m128i m4 = _mm_set1_epi8(0xF);
  4199. const __m128i m2 = _mm_set1_epi8(0x2);
  4200. __m256 acc = _mm256_setzero_ps();
  4201. for (int i = 0; i < nb; ++i) {
  4202. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4203. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4204. const uint8_t * restrict q2 = x[i].qs;
  4205. const int8_t * restrict q8 = y[i].qs;
  4206. // load mins and scales from block_q2_K.scales[QK_K/16]
  4207. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4208. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4209. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4210. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4211. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4212. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4213. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4214. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4215. // sumf += -dmin * summs in 32bits*8
  4216. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4217. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4218. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4219. const __m128i scales[2] = { scales_0, scales_1 };
  4220. __m128i sumi_0 = _mm_setzero_si128();
  4221. __m128i sumi_1 = _mm_setzero_si128();
  4222. for (int j = 0; j < QK_K/128; ++j) {
  4223. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4224. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4225. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4226. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4227. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4228. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4229. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4230. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4231. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4232. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4233. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4234. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4235. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4236. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4237. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4238. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4239. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4240. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4241. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4242. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4243. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4244. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4245. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4246. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4247. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4248. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4249. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4250. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4251. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4252. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4253. __m128i shuffle = _mm_set1_epi16(0x0100);
  4254. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4255. shuffle = _mm_add_epi16(shuffle, m2);
  4256. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4257. shuffle = _mm_add_epi16(shuffle, m2);
  4258. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4259. shuffle = _mm_add_epi16(shuffle, m2);
  4260. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4261. shuffle = _mm_add_epi16(shuffle, m2);
  4262. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4263. shuffle = _mm_add_epi16(shuffle, m2);
  4264. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4265. shuffle = _mm_add_epi16(shuffle, m2);
  4266. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4267. shuffle = _mm_add_epi16(shuffle, m2);
  4268. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4269. p0 = _mm_add_epi32(p0, p1);
  4270. p2 = _mm_add_epi32(p2, p3);
  4271. p4 = _mm_add_epi32(p4, p5);
  4272. p6 = _mm_add_epi32(p6, p7);
  4273. // isum in 32bits*4*2
  4274. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4275. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4276. }
  4277. // sumf += dall * isum - dmin * summs in 32bits
  4278. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4279. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4280. }
  4281. *s = hsum_float_8(acc);
  4282. #elif defined __riscv_v_intrinsic
  4283. float sumf = 0;
  4284. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4285. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4286. for (int i = 0; i < nb; ++i) {
  4287. const uint8_t * q2 = x[i].qs;
  4288. const int8_t * q8 = y[i].qs;
  4289. const uint8_t * sc = x[i].scales;
  4290. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4291. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4292. size_t vl = 16;
  4293. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4294. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4295. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4296. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4297. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4298. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4299. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4300. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4301. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4302. vl = 32;
  4303. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4304. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4305. uint8_t is=0;
  4306. int isum=0;
  4307. for (int j = 0; j < QK_K/128; ++j) {
  4308. // load Q2
  4309. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4310. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4311. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4312. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4313. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4314. // duplicate scale elements for product
  4315. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4316. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4317. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4318. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4319. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4320. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4321. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4322. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4323. // load Q8
  4324. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4325. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4326. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4327. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4328. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4329. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4330. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4331. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4332. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4333. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4334. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4335. q2+=32; q8+=128; is=8;
  4336. }
  4337. sumf += dall * isum;
  4338. }
  4339. *s = sumf;
  4340. #else
  4341. float sumf = 0;
  4342. for (int i = 0; i < nb; ++i) {
  4343. const uint8_t * q2 = x[i].qs;
  4344. const int8_t * q8 = y[i].qs;
  4345. const uint8_t * sc = x[i].scales;
  4346. int summs = 0;
  4347. for (int j = 0; j < 16; ++j) {
  4348. summs += y[i].bsums[j] * (sc[j] >> 4);
  4349. }
  4350. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4351. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4352. int isum = 0;
  4353. int is = 0;
  4354. int d;
  4355. for (int k = 0; k < QK_K/128; ++k) {
  4356. int shift = 0;
  4357. for (int j = 0; j < 4; ++j) {
  4358. d = sc[is++] & 0xF;
  4359. int isuml = 0;
  4360. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4361. isum += d * isuml;
  4362. d = sc[is++] & 0xF;
  4363. isuml = 0;
  4364. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4365. isum += d * isuml;
  4366. shift += 2;
  4367. q8 += 32;
  4368. }
  4369. q2 += 32;
  4370. }
  4371. sumf += dall * isum - dmin * summs;
  4372. }
  4373. *s = sumf;
  4374. #endif
  4375. }
  4376. #else
  4377. 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) {
  4378. assert(nrc == 1);
  4379. UNUSED(nrc);
  4380. UNUSED(bx);
  4381. UNUSED(by);
  4382. UNUSED(bs);
  4383. const block_q2_K * restrict x = vx;
  4384. const block_q8_K * restrict y = vy;
  4385. const int nb = n / QK_K;
  4386. #ifdef __ARM_NEON
  4387. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4388. const int32x4_t vzero = vdupq_n_s32(0);
  4389. ggml_int8x16x4_t q2bytes;
  4390. uint32_t aux32[2];
  4391. const uint8_t * scales = (const uint8_t *)aux32;
  4392. float sum = 0;
  4393. for (int i = 0; i < nb; ++i) {
  4394. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4395. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4396. const uint8_t * restrict q2 = x[i].qs;
  4397. const int8_t * restrict q8 = y[i].qs;
  4398. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4399. aux32[0] = sc[0] & 0x0f0f0f0f;
  4400. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4401. 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]);
  4402. int isum1 = 0, isum2 = 0;
  4403. const uint8x16_t q2bits = vld1q_u8(q2);
  4404. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4405. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4406. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4407. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4408. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4409. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4410. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4411. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4412. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4413. sum += d * (isum1 + isum2);
  4414. }
  4415. *s = sum;
  4416. #elif defined __AVX2__
  4417. const __m256i m3 = _mm256_set1_epi8(3);
  4418. __m256 acc = _mm256_setzero_ps();
  4419. uint32_t ud, um;
  4420. const uint8_t * restrict db = (const uint8_t *)&ud;
  4421. const uint8_t * restrict mb = (const uint8_t *)&um;
  4422. float summs = 0;
  4423. // TODO: optimize this
  4424. for (int i = 0; i < nb; ++i) {
  4425. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4426. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4427. const uint8_t * restrict q2 = x[i].qs;
  4428. const int8_t * restrict q8 = y[i].qs;
  4429. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4430. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4431. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4432. 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];
  4433. summs += dmin * smin;
  4434. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4435. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4436. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4437. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4438. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4439. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4440. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4441. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4442. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4443. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4444. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4445. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4446. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4447. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4448. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4449. }
  4450. *s = hsum_float_8(acc) + summs;
  4451. #elif defined __AVX__
  4452. const __m128i m3 = _mm_set1_epi8(3);
  4453. __m256 acc = _mm256_setzero_ps();
  4454. uint32_t ud, um;
  4455. const uint8_t * restrict db = (const uint8_t *)&ud;
  4456. const uint8_t * restrict mb = (const uint8_t *)&um;
  4457. float summs = 0;
  4458. // TODO: optimize this
  4459. for (int i = 0; i < nb; ++i) {
  4460. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4461. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4462. const uint8_t * restrict q2 = x[i].qs;
  4463. const int8_t * restrict q8 = y[i].qs;
  4464. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4465. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4466. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4467. 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];
  4468. summs += dmin * smin;
  4469. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4470. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4471. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4472. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4473. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4474. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4475. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4476. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4477. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4478. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4479. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4480. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4481. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4482. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4483. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4484. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4485. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4486. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4487. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4488. }
  4489. *s = hsum_float_8(acc) + summs;
  4490. #elif defined __riscv_v_intrinsic
  4491. uint32_t aux32[2];
  4492. const uint8_t * scales = (const uint8_t *)aux32;
  4493. float sumf = 0;
  4494. for (int i = 0; i < nb; ++i) {
  4495. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4496. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4497. const uint8_t * restrict q2 = x[i].qs;
  4498. const int8_t * restrict q8 = y[i].qs;
  4499. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4500. aux32[0] = sc[0] & 0x0f0f0f0f;
  4501. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4502. 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]);
  4503. int isum1 = 0;
  4504. int isum2 = 0;
  4505. size_t vl = 16;
  4506. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4507. // load Q2
  4508. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4509. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4510. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4511. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4512. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4513. // load Q8, and take product with Q2
  4514. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4515. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4516. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4517. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4518. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4519. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4520. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4521. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4522. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4523. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4524. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4525. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4526. sumf += d * (isum1 + isum2);
  4527. }
  4528. *s = sumf;
  4529. #else
  4530. float sumf = 0;
  4531. int isum[QK_K/16];
  4532. for (int i = 0; i < nb; ++i) {
  4533. const uint8_t * q2 = x[i].qs;
  4534. const int8_t * q8 = y[i].qs;
  4535. const uint8_t * sc = x[i].scales;
  4536. int summs = 0;
  4537. for (int j = 0; j < QK_K/16; ++j) {
  4538. summs += y[i].bsums[j] * (sc[j] >> 4);
  4539. }
  4540. const float dall = 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. memset(isum, 0, (QK_K/16)*sizeof(int));
  4543. for (int l = 0; l < 16; ++l) {
  4544. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4545. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4546. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4547. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4548. }
  4549. for (int l = 0; l < QK_K/16; ++l) {
  4550. isum[l] *= (sc[l] & 0xF);
  4551. }
  4552. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4553. }
  4554. *s = sumf;
  4555. #endif
  4556. }
  4557. #endif
  4558. #if QK_K == 256
  4559. 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) {
  4560. assert(n % QK_K == 0);
  4561. assert(nrc == 1);
  4562. UNUSED(nrc);
  4563. UNUSED(bx);
  4564. UNUSED(by);
  4565. UNUSED(bs);
  4566. const uint32_t kmask1 = 0x03030303;
  4567. const uint32_t kmask2 = 0x0f0f0f0f;
  4568. const block_q3_K * restrict x = vx;
  4569. const block_q8_K * restrict y = vy;
  4570. const int nb = n / QK_K;
  4571. #ifdef __ARM_NEON
  4572. uint32_t aux[3];
  4573. uint32_t utmp[4];
  4574. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4575. const int32x4_t vzero = vdupq_n_s32(0);
  4576. const uint8x16_t m0 = vdupq_n_u8(1);
  4577. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4578. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4579. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4580. const int8_t m32 = 32;
  4581. ggml_int8x16x4_t q3bytes;
  4582. float sum = 0;
  4583. for (int i = 0; i < nb; ++i) {
  4584. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4585. const uint8_t * restrict q3 = x[i].qs;
  4586. const uint8_t * restrict qh = x[i].hmask;
  4587. const int8_t * restrict q8 = y[i].qs;
  4588. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4589. ggml_uint8x16x4_t q3h;
  4590. int32_t isum = 0;
  4591. // Set up scales
  4592. memcpy(aux, x[i].scales, 12);
  4593. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4594. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4595. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4596. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4597. int8_t * scale = (int8_t *)utmp;
  4598. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4599. for (int j = 0; j < QK_K/128; ++j) {
  4600. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4601. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4602. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4603. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4604. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4605. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4606. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4607. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4608. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4609. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4610. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4611. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4612. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4613. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4614. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4615. scale += 4;
  4616. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4617. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4618. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4619. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4620. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4621. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4622. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4623. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4624. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4625. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4626. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4627. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4628. scale += 4;
  4629. if (j == 0) {
  4630. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4631. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4632. }
  4633. }
  4634. sum += d * isum;
  4635. }
  4636. *s = sum;
  4637. #elif defined __AVX2__
  4638. const __m256i m3 = _mm256_set1_epi8(3);
  4639. const __m256i mone = _mm256_set1_epi8(1);
  4640. const __m128i m32 = _mm_set1_epi8(32);
  4641. __m256 acc = _mm256_setzero_ps();
  4642. uint32_t aux[3];
  4643. for (int i = 0; i < nb; ++i) {
  4644. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4645. const uint8_t * restrict q3 = x[i].qs;
  4646. const int8_t * restrict q8 = y[i].qs;
  4647. // Set up scales
  4648. memcpy(aux, x[i].scales, 12);
  4649. __m128i scales128 = _mm_set_epi32(
  4650. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4651. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4652. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4653. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4654. scales128 = _mm_sub_epi8(scales128, m32);
  4655. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4656. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4657. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4658. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4659. // high bit
  4660. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4661. // integer accumulator
  4662. __m256i sumi = _mm256_setzero_si256();
  4663. int bit = 0;
  4664. int is = 0;
  4665. for (int j = 0; j < QK_K/128; ++j) {
  4666. // load low 2 bits
  4667. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4668. // prepare low and high bits
  4669. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4670. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4671. ++bit;
  4672. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4673. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4674. ++bit;
  4675. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4676. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4677. ++bit;
  4678. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4679. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4680. ++bit;
  4681. // load Q8 quants
  4682. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4683. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4684. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4685. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4686. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4687. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4688. // and 2 if the high bit was set)
  4689. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4690. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4691. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4692. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4693. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4694. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4695. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4696. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4697. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4698. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4699. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4700. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4701. // multiply with scales
  4702. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4703. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4704. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4705. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4706. // accumulate
  4707. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4708. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4709. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4710. }
  4711. // multiply with block scale and accumulate
  4712. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4713. }
  4714. *s = hsum_float_8(acc);
  4715. #elif defined __AVX__
  4716. const __m128i m3 = _mm_set1_epi8(3);
  4717. const __m128i mone = _mm_set1_epi8(1);
  4718. const __m128i m32 = _mm_set1_epi8(32);
  4719. const __m128i m2 = _mm_set1_epi8(2);
  4720. __m256 acc = _mm256_setzero_ps();
  4721. const uint32_t *aux;
  4722. for (int i = 0; i < nb; ++i) {
  4723. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4724. const uint8_t * restrict q3 = x[i].qs;
  4725. const int8_t * restrict q8 = y[i].qs;
  4726. // Set up scales
  4727. aux = (const uint32_t *)x[i].scales;
  4728. __m128i scales128 = _mm_set_epi32(
  4729. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4730. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4731. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4732. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4733. scales128 = _mm_sub_epi8(scales128, m32);
  4734. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4735. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4736. const __m128i scales[2] = { scales_0, scales_1 };
  4737. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4738. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4739. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4740. // integer accumulator
  4741. __m128i sumi_0 = _mm_setzero_si128();
  4742. __m128i sumi_1 = _mm_setzero_si128();
  4743. for (int j = 0; j < QK_K/128; ++j) {
  4744. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4745. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4746. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4747. // prepare low and high bits
  4748. const int bit = j << 2;
  4749. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4750. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4751. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4752. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4753. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4754. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4755. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4756. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4757. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4758. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4759. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4760. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4761. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4762. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4763. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4764. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4765. // load Q8 quants from block_q8_K.qs[QK_K]
  4766. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4767. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4768. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4769. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4770. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4771. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4772. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4773. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4774. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4775. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4776. // and 2 if the high bit was set)
  4777. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4778. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4779. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4780. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4781. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4782. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4783. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4784. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4785. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4786. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4787. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4788. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4789. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4790. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4791. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4792. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4793. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4794. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4795. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4796. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4797. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4798. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4799. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4800. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4801. // multiply with scales
  4802. __m128i shuffle = _mm_set1_epi16(0x0100);
  4803. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4804. shuffle = _mm_add_epi16(shuffle, m2);
  4805. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4806. shuffle = _mm_add_epi16(shuffle, m2);
  4807. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4808. shuffle = _mm_add_epi16(shuffle, m2);
  4809. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4810. shuffle = _mm_add_epi16(shuffle, m2);
  4811. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4812. shuffle = _mm_add_epi16(shuffle, m2);
  4813. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4814. shuffle = _mm_add_epi16(shuffle, m2);
  4815. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4816. shuffle = _mm_add_epi16(shuffle, m2);
  4817. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4818. // accumulate
  4819. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4820. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4821. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4822. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4823. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4824. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4825. }
  4826. // multiply with block scale and accumulate
  4827. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4828. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4829. }
  4830. *s = hsum_float_8(acc);
  4831. #elif defined __riscv_v_intrinsic
  4832. uint32_t aux[3];
  4833. uint32_t utmp[4];
  4834. float sumf = 0;
  4835. for (int i = 0; i < nb; ++i) {
  4836. const uint8_t * restrict q3 = x[i].qs;
  4837. const uint8_t * restrict qh = x[i].hmask;
  4838. const int8_t * restrict q8 = y[i].qs;
  4839. memcpy(aux, x[i].scales, 12);
  4840. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4841. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4842. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4843. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4844. int8_t * scale = (int8_t *)utmp;
  4845. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4846. size_t vl = 32;
  4847. uint8_t m = 1;
  4848. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4849. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4850. int sum_t = 0;
  4851. for (int j = 0; j < QK_K; j += 128) {
  4852. vl = 32;
  4853. // load Q3
  4854. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4855. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4856. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4857. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4858. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4859. // compute mask for subtraction
  4860. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4861. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4862. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4863. m <<= 1;
  4864. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4865. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4866. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4867. m <<= 1;
  4868. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4869. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4870. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4871. m <<= 1;
  4872. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4873. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4874. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4875. m <<= 1;
  4876. // load Q8 and take product with Q3
  4877. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4878. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4879. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4880. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4881. vl = 16;
  4882. // retrieve lane to multiply with scale
  4883. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4884. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4885. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4886. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4887. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4888. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4889. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4890. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4891. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4892. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4893. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4894. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4895. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4896. q3 += 32; q8 += 128; scale += 8;
  4897. }
  4898. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4899. sumf += d*sum_t;
  4900. }
  4901. *s = sumf;
  4902. #else
  4903. // scalar version
  4904. // This function is written like this so the compiler can manage to vectorize most of it
  4905. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4906. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4907. // The ideal situation would be if we could just write the code once, and the compiler would
  4908. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4909. // write vectorized versions for AVX, ARM_NEON, etc.
  4910. int8_t aux8[QK_K];
  4911. int16_t aux16[8];
  4912. float sums [8];
  4913. int32_t aux32[8];
  4914. memset(sums, 0, 8*sizeof(float));
  4915. uint32_t auxs[4];
  4916. const int8_t * scales = (const int8_t*)auxs;
  4917. float sumf = 0;
  4918. for (int i = 0; i < nb; ++i) {
  4919. const uint8_t * restrict q3 = x[i].qs;
  4920. const uint8_t * restrict hm = x[i].hmask;
  4921. const int8_t * restrict q8 = y[i].qs;
  4922. memset(aux32, 0, 8*sizeof(int32_t));
  4923. int8_t * restrict a = aux8;
  4924. uint8_t m = 1;
  4925. for (int j = 0; j < QK_K; j += 128) {
  4926. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4927. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4928. a += 32; m <<= 1;
  4929. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4930. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4931. a += 32; m <<= 1;
  4932. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4933. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4934. a += 32; m <<= 1;
  4935. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4936. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4937. a += 32; m <<= 1;
  4938. q3 += 32;
  4939. }
  4940. a = aux8;
  4941. memcpy(auxs, x[i].scales, 12);
  4942. uint32_t tmp = auxs[2];
  4943. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4944. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4945. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4946. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4947. for (int j = 0; j < QK_K/16; ++j) {
  4948. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4949. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4950. q8 += 8; a += 8;
  4951. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4952. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4953. q8 += 8; a += 8;
  4954. }
  4955. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4956. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4957. }
  4958. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4959. *s = sumf;
  4960. #endif
  4961. }
  4962. #else
  4963. 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) {
  4964. assert(n % QK_K == 0);
  4965. assert(nrc == 1);
  4966. UNUSED(nrc);
  4967. UNUSED(bx);
  4968. UNUSED(by);
  4969. UNUSED(bs);
  4970. const block_q3_K * restrict x = vx;
  4971. const block_q8_K * restrict y = vy;
  4972. const int nb = n / QK_K;
  4973. #ifdef __ARM_NEON
  4974. const int32x4_t vzero = vdupq_n_s32(0);
  4975. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4976. const uint8x16_t mh = vdupq_n_u8(4);
  4977. ggml_int8x16x4_t q3bytes;
  4978. uint16_t aux16[2];
  4979. int8_t * scales = (int8_t *)aux16;
  4980. float sum = 0;
  4981. for (int i = 0; i < nb; ++i) {
  4982. ggml_uint8x16x4_t q3h;
  4983. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4984. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4985. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4986. const uint16_t a = *(const uint16_t *)x[i].scales;
  4987. aux16[0] = a & 0x0f0f;
  4988. aux16[1] = (a >> 4) & 0x0f0f;
  4989. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4990. 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]);
  4991. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4992. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4993. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4994. q3h.val[1] = vandq_u8(mh, htmp);
  4995. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4996. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4997. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4998. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4999. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  5000. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  5001. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  5002. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  5003. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  5004. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  5005. sum += d * isum;
  5006. }
  5007. *s = sum;
  5008. #elif defined __AVX2__
  5009. const __m256i m3 = _mm256_set1_epi8(3);
  5010. const __m256i m1 = _mm256_set1_epi8(1);
  5011. __m256 acc = _mm256_setzero_ps();
  5012. uint64_t aux64;
  5013. uint16_t aux16[2];
  5014. const int8_t * aux8 = (const int8_t *)aux16;
  5015. for (int i = 0; i < nb; ++i) {
  5016. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5017. const uint8_t * restrict q3 = x[i].qs;
  5018. const int8_t * restrict q8 = y[i].qs;
  5019. const uint16_t a = *(const uint16_t *)x[i].scales;
  5020. aux16[0] = a & 0x0f0f;
  5021. aux16[1] = (a >> 4) & 0x0f0f;
  5022. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  5023. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  5024. memcpy(&aux64, x[i].hmask, 8);
  5025. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  5026. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  5027. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  5028. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  5029. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  5030. // load low 2 bits
  5031. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  5032. // prepare low and high bits
  5033. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  5034. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  5035. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  5036. // load Q8 quants
  5037. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5038. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5039. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  5040. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5041. // and 2 if the high bit was set)
  5042. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  5043. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  5044. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  5045. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  5046. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5047. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5048. // multiply with scales
  5049. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5050. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5051. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  5052. // multiply with block scale and accumulate
  5053. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  5054. }
  5055. *s = hsum_float_8(acc);
  5056. #elif defined __AVX__
  5057. const __m128i m3 = _mm_set1_epi8(3);
  5058. const __m128i m1 = _mm_set1_epi8(1);
  5059. __m256 acc = _mm256_setzero_ps();
  5060. uint64_t aux64;
  5061. uint16_t aux16[2];
  5062. const int8_t * aux8 = (const int8_t *)aux16;
  5063. for (int i = 0; i < nb; ++i) {
  5064. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5065. const uint8_t * restrict q3 = x[i].qs;
  5066. const int8_t * restrict q8 = y[i].qs;
  5067. const uint16_t a = *(const uint16_t *)x[i].scales;
  5068. aux16[0] = a & 0x0f0f;
  5069. aux16[1] = (a >> 4) & 0x0f0f;
  5070. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  5071. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  5072. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  5073. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  5074. memcpy(&aux64, x[i].hmask, 8);
  5075. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  5076. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  5077. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  5078. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  5079. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  5080. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  5081. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  5082. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  5083. // load low 2 bits
  5084. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  5085. // prepare low and high bits
  5086. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  5087. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  5088. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  5089. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  5090. // load Q8 quants
  5091. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5092. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5093. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  5094. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  5095. // and 2 if the high bit was set)
  5096. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  5097. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  5098. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  5099. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  5100. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  5101. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  5102. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  5103. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  5104. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5105. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5106. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5107. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5108. // multiply with scales
  5109. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5110. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  5111. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  5112. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  5113. p16_0 = _mm_add_epi32(p16_0, p16_2);
  5114. p16_1 = _mm_add_epi32(p16_1, p16_3);
  5115. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  5116. // multiply with block scale and accumulate
  5117. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  5118. }
  5119. *s = hsum_float_8(acc);
  5120. #elif defined __riscv_v_intrinsic
  5121. uint16_t aux16[2];
  5122. int8_t * scales = (int8_t *)aux16;
  5123. float sumf = 0;
  5124. for (int i = 0; i < nb; ++i) {
  5125. const uint8_t * restrict q3 = x[i].qs;
  5126. const int8_t * restrict q8 = y[i].qs;
  5127. const uint16_t a = *(const uint16_t *)x[i].scales;
  5128. aux16[0] = a & 0x0f0f;
  5129. aux16[1] = (a >> 4) & 0x0f0f;
  5130. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  5131. 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]);
  5132. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5133. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5134. // load qh
  5135. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  5136. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5137. size_t vl = 16;
  5138. // extend and combine both qh_x1 and qh_x2
  5139. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5140. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5141. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  5142. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  5143. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  5144. // load Q3
  5145. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  5146. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  5147. 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);
  5148. 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);
  5149. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  5150. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  5151. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  5152. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  5153. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  5154. // load Q8 and take product with Q3
  5155. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5156. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5157. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5158. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5159. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5160. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5161. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5162. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5163. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  5164. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  5165. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  5166. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  5167. sumf += d * isum;
  5168. }
  5169. *s = sumf;
  5170. #else
  5171. int8_t aux8[QK_K];
  5172. int16_t aux16[8];
  5173. float sums [8];
  5174. int32_t aux32[8];
  5175. int32_t scales[4];
  5176. memset(sums, 0, 8*sizeof(float));
  5177. float sumf = 0;
  5178. for (int i = 0; i < nb; ++i) {
  5179. const uint8_t * restrict q3 = x[i].qs;
  5180. const uint8_t * restrict hm = x[i].hmask;
  5181. const int8_t * restrict q8 = y[i].qs;
  5182. int8_t * restrict a = aux8;
  5183. for (int l = 0; l < 8; ++l) {
  5184. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5185. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5186. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5187. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5188. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5189. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5190. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5191. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5192. }
  5193. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5194. scales[1] = (x[i].scales[0] >> 4) - 8;
  5195. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5196. scales[3] = (x[i].scales[1] >> 4) - 8;
  5197. memset(aux32, 0, 8*sizeof(int32_t));
  5198. for (int j = 0; j < QK_K/16; ++j) {
  5199. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5200. q8 += 8; a += 8;
  5201. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5202. q8 += 8; a += 8;
  5203. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5204. }
  5205. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5206. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5207. }
  5208. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5209. *s = sumf;
  5210. #endif
  5211. }
  5212. #endif
  5213. #if QK_K == 256
  5214. 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) {
  5215. assert(n % QK_K == 0);
  5216. assert(nrc == 1);
  5217. UNUSED(nrc);
  5218. UNUSED(bx);
  5219. UNUSED(by);
  5220. UNUSED(bs);
  5221. const block_q4_K * restrict x = vx;
  5222. const block_q8_K * restrict y = vy;
  5223. const int nb = n / QK_K;
  5224. static const uint32_t kmask1 = 0x3f3f3f3f;
  5225. static const uint32_t kmask2 = 0x0f0f0f0f;
  5226. static const uint32_t kmask3 = 0x03030303;
  5227. uint32_t utmp[4];
  5228. #ifdef __ARM_NEON
  5229. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5230. const int32x4_t mzero = vdupq_n_s32(0);
  5231. ggml_int8x16x2_t q4bytes;
  5232. ggml_int8x16x2_t q8bytes;
  5233. float sumf = 0;
  5234. for (int i = 0; i < nb; ++i) {
  5235. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5236. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5237. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5238. memcpy(utmp, x[i].scales, 12);
  5239. uint32x2_t mins8 = { 0 };
  5240. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5241. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5242. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5243. utmp[0] &= kmask1;
  5244. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5245. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5246. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5247. sumf -= dmin * vaddvq_s32(prod);
  5248. const uint8_t * scales = (const uint8_t *)utmp;
  5249. const uint8_t * restrict q4 = x[i].qs;
  5250. const int8_t * restrict q8 = y[i].qs;
  5251. int32_t sumi1 = 0;
  5252. int32_t sumi2 = 0;
  5253. for (int j = 0; j < QK_K/64; ++j) {
  5254. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5255. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5256. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5257. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5258. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5259. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5260. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5261. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5262. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5263. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5264. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5265. }
  5266. sumf += d * (sumi1 + sumi2);
  5267. }
  5268. *s = sumf;
  5269. #elif defined __AVX2__
  5270. const __m256i m4 = _mm256_set1_epi8(0xF);
  5271. __m256 acc = _mm256_setzero_ps();
  5272. __m128 acc_m = _mm_setzero_ps();
  5273. for (int i = 0; i < nb; ++i) {
  5274. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5275. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5276. memcpy(utmp, x[i].scales, 12);
  5277. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5278. const uint32_t uaux = utmp[1] & kmask1;
  5279. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5280. utmp[2] = uaux;
  5281. utmp[0] &= kmask1;
  5282. const uint8_t * restrict q4 = x[i].qs;
  5283. const int8_t * restrict q8 = y[i].qs;
  5284. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5285. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5286. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5287. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5288. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5289. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5290. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5291. __m256i sumi = _mm256_setzero_si256();
  5292. for (int j = 0; j < QK_K/64; ++j) {
  5293. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5294. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5295. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5296. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5297. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5298. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5299. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5300. p16l = _mm256_madd_epi16(scale_l, p16l);
  5301. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5302. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5303. p16h = _mm256_madd_epi16(scale_h, p16h);
  5304. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5305. sumi = _mm256_add_epi32(sumi, sumj);
  5306. }
  5307. __m256 vd = _mm256_set1_ps(d);
  5308. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5309. }
  5310. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5311. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5312. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5313. #elif defined __AVX__
  5314. const __m128i m4 = _mm_set1_epi8(0xF);
  5315. const __m128i m2 = _mm_set1_epi8(0x2);
  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. const uint8_t * restrict q4 = x[i].qs;
  5322. const int8_t * restrict q8 = y[i].qs;
  5323. memcpy(utmp, x[i].scales, 12);
  5324. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5325. const uint32_t uaux = utmp[1] & kmask1;
  5326. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5327. utmp[2] = uaux;
  5328. utmp[0] &= kmask1;
  5329. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5330. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5331. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5332. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5333. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5334. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5335. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5336. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5337. __m128i sumi_0 = _mm_setzero_si128();
  5338. __m128i sumi_1 = _mm_setzero_si128();
  5339. __m128i shuffle = _mm_set1_epi16(0x0100);
  5340. for (int j = 0; j < QK_K/64; ++j) {
  5341. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5342. shuffle = _mm_add_epi16(shuffle, m2);
  5343. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5344. shuffle = _mm_add_epi16(shuffle, m2);
  5345. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5346. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5347. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5348. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5349. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5350. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5351. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5352. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5353. p16l = _mm_madd_epi16(scale_l, p16l);
  5354. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5355. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5356. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5357. p16l = _mm_madd_epi16(scale_l, p16l);
  5358. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5359. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5360. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5361. p16h = _mm_madd_epi16(scale_h, p16h);
  5362. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5363. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5364. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5365. p16h = _mm_madd_epi16(scale_h, p16h);
  5366. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5367. }
  5368. __m256 vd = _mm256_set1_ps(d);
  5369. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5370. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5371. }
  5372. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5373. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5374. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5375. #elif defined __riscv_v_intrinsic
  5376. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5377. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5378. float sumf = 0;
  5379. for (int i = 0; i < nb; ++i) {
  5380. size_t vl = 8;
  5381. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5382. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5383. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5384. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5385. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5386. memcpy(utmp, x[i].scales, 12);
  5387. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5388. const uint32_t uaux = utmp[1] & kmask1;
  5389. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5390. utmp[2] = uaux;
  5391. utmp[0] &= kmask1;
  5392. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5393. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5394. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5395. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5396. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5397. const uint8_t * restrict q4 = x[i].qs;
  5398. const int8_t * restrict q8 = y[i].qs;
  5399. vl = 32;
  5400. int32_t sum_1 = 0;
  5401. int32_t sum_2 = 0;
  5402. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5403. for (int j = 0; j < QK_K/64; ++j) {
  5404. // load Q4
  5405. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5406. // load Q8 and multiply it with lower Q4 nibble
  5407. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5408. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5409. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5410. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5411. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5412. // load Q8 and multiply it with upper Q4 nibble
  5413. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5414. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5415. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5416. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5417. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5418. q4 += 32; q8 += 64;
  5419. }
  5420. sumf += d*(sum_1 + sum_2);
  5421. }
  5422. *s = sumf;
  5423. #else
  5424. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5425. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5426. int8_t aux8[QK_K];
  5427. int16_t aux16[8];
  5428. float sums [8];
  5429. int32_t aux32[8];
  5430. memset(sums, 0, 8*sizeof(float));
  5431. float sumf = 0;
  5432. for (int i = 0; i < nb; ++i) {
  5433. const uint8_t * restrict q4 = x[i].qs;
  5434. const int8_t * restrict q8 = y[i].qs;
  5435. memset(aux32, 0, 8*sizeof(int32_t));
  5436. int8_t * restrict a = aux8;
  5437. for (int j = 0; j < QK_K/64; ++j) {
  5438. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5439. a += 32;
  5440. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5441. a += 32; q4 += 32;
  5442. }
  5443. memcpy(utmp, x[i].scales, 12);
  5444. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5445. const uint32_t uaux = utmp[1] & kmask1;
  5446. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5447. utmp[2] = uaux;
  5448. utmp[0] &= kmask1;
  5449. int sumi = 0;
  5450. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5451. a = aux8;
  5452. int is = 0;
  5453. for (int j = 0; j < QK_K/32; ++j) {
  5454. int32_t scale = scales[is++];
  5455. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5456. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5457. q8 += 8; a += 8;
  5458. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5459. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5460. q8 += 8; a += 8;
  5461. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5462. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5463. q8 += 8; a += 8;
  5464. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5465. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5466. q8 += 8; a += 8;
  5467. }
  5468. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5469. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5470. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5471. sumf -= dmin * sumi;
  5472. }
  5473. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5474. *s = sumf;
  5475. #endif
  5476. }
  5477. #else
  5478. 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) {
  5479. assert(n % QK_K == 0);
  5480. assert(nrc == 1);
  5481. UNUSED(nrc);
  5482. UNUSED(bx);
  5483. UNUSED(by);
  5484. UNUSED(bs);
  5485. const block_q4_K * restrict x = vx;
  5486. const block_q8_K * restrict y = vy;
  5487. const int nb = n / QK_K;
  5488. #ifdef __ARM_NEON
  5489. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5490. const int32x4_t mzero = vdupq_n_s32(0);
  5491. float sumf = 0;
  5492. ggml_int8x16x2_t q4bytes;
  5493. ggml_int8x16x4_t q8bytes;
  5494. float sum_mins = 0.f;
  5495. uint16_t aux16[2];
  5496. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5497. for (int i = 0; i < nb; ++i) {
  5498. const uint8_t * restrict q4 = x[i].qs;
  5499. const int8_t * restrict q8 = y[i].qs;
  5500. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5501. aux16[0] = a[0] & 0x0f0f;
  5502. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5503. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5504. sum_mins += y[i].d * GGML_FP16_TO_FP32(x[i].d[1]) * summi;
  5505. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5506. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5507. q8bytes = ggml_vld1q_s8_x4(q8);
  5508. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5509. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5510. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5511. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5512. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5513. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5514. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5515. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5516. sumf += d * (sumi1 + sumi2);
  5517. }
  5518. *s = sumf - sum_mins;
  5519. #elif defined __AVX2__
  5520. const __m256i m4 = _mm256_set1_epi8(0xF);
  5521. __m256 acc = _mm256_setzero_ps();
  5522. float summs = 0;
  5523. uint16_t aux16[2];
  5524. const uint8_t * scales = (const uint8_t *)aux16;
  5525. for (int i = 0; i < nb; ++i) {
  5526. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5527. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5528. const __m256 vd = _mm256_set1_ps(d);
  5529. const uint16_t * a = (const uint16_t *)x[i].scales;
  5530. aux16[0] = a[0] & 0x0f0f;
  5531. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5532. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5533. const uint8_t * restrict q4 = x[i].qs;
  5534. const int8_t * restrict q8 = y[i].qs;
  5535. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5536. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5537. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5538. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5539. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5540. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5541. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5542. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5543. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5544. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5545. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5546. }
  5547. *s = hsum_float_8(acc) - summs;
  5548. #elif defined __AVX__
  5549. const __m128i m4 = _mm_set1_epi8(0xF);
  5550. __m256 acc = _mm256_setzero_ps();
  5551. float summs = 0;
  5552. uint16_t aux16[2];
  5553. const uint8_t * scales = (const uint8_t *)aux16;
  5554. for (int i = 0; i < nb; ++i) {
  5555. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5556. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5557. const __m256 vd = _mm256_set1_ps(d);
  5558. const uint16_t * a = (const uint16_t *)x[i].scales;
  5559. aux16[0] = a[0] & 0x0f0f;
  5560. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5561. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5562. const uint8_t * restrict q4 = x[i].qs;
  5563. const int8_t * restrict q8 = y[i].qs;
  5564. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5565. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5566. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5567. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5568. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5569. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5570. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5571. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5572. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5573. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5574. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5575. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5576. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5577. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5578. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5579. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5580. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5581. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5582. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5583. }
  5584. *s = hsum_float_8(acc) - summs;
  5585. #elif defined __riscv_v_intrinsic
  5586. uint16_t s16[2];
  5587. const uint8_t * restrict scales = (const uint8_t *)s16;
  5588. float sumf = 0;
  5589. for (int i = 0; i < nb; ++i) {
  5590. const uint8_t * restrict q4 = x[i].qs;
  5591. const int8_t * restrict q8 = y[i].qs;
  5592. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5593. s16[0] = b[0] & 0x0f0f;
  5594. s16[1] = (b[0] >> 4) & 0x0f0f;
  5595. 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]));
  5596. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5597. size_t vl = 32;
  5598. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5599. // load Q4
  5600. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5601. // load Q8 and multiply it with lower Q4 nibble
  5602. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5603. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5604. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5605. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5606. // load Q8 and multiply it with upper Q4 nibble
  5607. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5608. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5609. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5610. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5611. }
  5612. *s = sumf;
  5613. #else
  5614. uint8_t aux8[QK_K];
  5615. int16_t aux16[16];
  5616. float sums [8];
  5617. memset(sums, 0, 8*sizeof(float));
  5618. uint16_t s16[2];
  5619. const uint8_t * restrict scales = (const uint8_t *)s16;
  5620. float sumf = 0;
  5621. for (int i = 0; i < nb; ++i) {
  5622. const uint8_t * restrict q4 = x[i].qs;
  5623. const int8_t * restrict q8 = y[i].qs;
  5624. uint8_t * restrict a = aux8;
  5625. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5626. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5627. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5628. s16[0] = b[0] & 0x0f0f;
  5629. s16[1] = (b[0] >> 4) & 0x0f0f;
  5630. 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]));
  5631. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5632. for (int j = 0; j < QK_K/32; ++j) {
  5633. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5634. q8 += 16; a += 16;
  5635. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5636. q8 += 16; a += 16;
  5637. const float dl = d * scales[j];
  5638. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5639. }
  5640. }
  5641. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5642. *s = sumf;
  5643. #endif
  5644. }
  5645. #endif
  5646. #if QK_K == 256
  5647. 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) {
  5648. assert(n % QK_K == 0);
  5649. assert(nrc == 1);
  5650. UNUSED(nrc);
  5651. UNUSED(bx);
  5652. UNUSED(by);
  5653. UNUSED(bs);
  5654. const block_q5_K * restrict x = vx;
  5655. const block_q8_K * restrict y = vy;
  5656. const int nb = n / QK_K;
  5657. static const uint32_t kmask1 = 0x3f3f3f3f;
  5658. static const uint32_t kmask2 = 0x0f0f0f0f;
  5659. static const uint32_t kmask3 = 0x03030303;
  5660. uint32_t utmp[4];
  5661. #ifdef __ARM_NEON
  5662. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5663. const uint8x16_t mone = vdupq_n_u8(1);
  5664. const uint8x16_t mtwo = vdupq_n_u8(2);
  5665. const int32x4_t mzero = vdupq_n_s32(0);
  5666. ggml_int8x16x4_t q5bytes;
  5667. float sumf = 0;
  5668. for (int i = 0; i < nb; ++i) {
  5669. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5670. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5671. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5672. memcpy(utmp, x[i].scales, 12);
  5673. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5674. const uint32_t uaux = utmp[1] & kmask1;
  5675. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5676. utmp[2] = uaux;
  5677. utmp[0] &= kmask1;
  5678. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5679. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5680. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5681. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5682. int32_t sumi_mins = vaddvq_s32(prod);
  5683. const uint8_t * scales = (const uint8_t *)utmp;
  5684. const uint8_t * restrict q5 = x[i].qs;
  5685. const uint8_t * restrict qh = x[i].qh;
  5686. const int8_t * restrict q8 = y[i].qs;
  5687. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5688. ggml_uint8x16x4_t q5h;
  5689. int32_t sumi = 0;
  5690. for (int j = 0; j < QK_K/64; ++j) {
  5691. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5692. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5693. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5694. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5695. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5696. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5697. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5698. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5699. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5700. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5701. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5702. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5703. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5704. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5705. }
  5706. sumf += d * sumi - dmin * sumi_mins;
  5707. }
  5708. *s = sumf;
  5709. #elif defined __AVX2__
  5710. const __m256i m4 = _mm256_set1_epi8(0xF);
  5711. const __m128i mzero = _mm_setzero_si128();
  5712. const __m256i mone = _mm256_set1_epi8(1);
  5713. __m256 acc = _mm256_setzero_ps();
  5714. float summs = 0.f;
  5715. for (int i = 0; i < nb; ++i) {
  5716. const uint8_t * restrict q5 = x[i].qs;
  5717. const int8_t * restrict q8 = y[i].qs;
  5718. #if QK_K == 256
  5719. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5720. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5721. memcpy(utmp, x[i].scales, 12);
  5722. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5723. const uint32_t uaux = utmp[1] & kmask1;
  5724. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5725. utmp[2] = uaux;
  5726. utmp[0] &= kmask1;
  5727. #else
  5728. // TODO
  5729. const float d = 0, dmin = 0;
  5730. #endif
  5731. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5732. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5733. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5734. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5735. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5736. summs += dmin * _mm_extract_epi32(hsum, 0);
  5737. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5738. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5739. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5740. __m256i hmask = mone;
  5741. __m256i sumi = _mm256_setzero_si256();
  5742. int bit = 0;
  5743. for (int j = 0; j < QK_K/64; ++j) {
  5744. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5745. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5746. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5747. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5748. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5749. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5750. hmask = _mm256_slli_epi16(hmask, 1);
  5751. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5752. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5753. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5754. hmask = _mm256_slli_epi16(hmask, 1);
  5755. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5756. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5757. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5758. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5759. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5760. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5761. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5762. }
  5763. __m256 vd = _mm256_set1_ps(d);
  5764. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5765. }
  5766. *s = hsum_float_8(acc) + summs;
  5767. #elif defined __AVX__
  5768. const __m128i m4 = _mm_set1_epi8(0xF);
  5769. const __m128i mzero = _mm_setzero_si128();
  5770. const __m128i mone = _mm_set1_epi8(1);
  5771. const __m128i m2 = _mm_set1_epi8(2);
  5772. __m256 acc = _mm256_setzero_ps();
  5773. float summs = 0.f;
  5774. for (int i = 0; i < nb; ++i) {
  5775. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5776. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5777. const uint8_t * restrict q5 = x[i].qs;
  5778. const int8_t * restrict q8 = y[i].qs;
  5779. memcpy(utmp, x[i].scales, 12);
  5780. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5781. const uint32_t uaux = utmp[1] & kmask1;
  5782. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5783. utmp[2] = uaux;
  5784. utmp[0] &= kmask1;
  5785. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5786. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5787. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5788. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5789. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5790. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5791. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5792. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5793. summs += dmin * _mm_extract_epi32(hsum, 0);
  5794. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5795. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5796. __m128i hmask = mone;
  5797. __m128i sumi_0 = _mm_setzero_si128();
  5798. __m128i sumi_1 = _mm_setzero_si128();
  5799. int bit = 0;
  5800. __m128i shuffle = _mm_set1_epi16(0x0100);
  5801. for (int j = 0; j < QK_K/64; ++j) {
  5802. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5803. shuffle = _mm_add_epi16(shuffle, m2);
  5804. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5805. shuffle = _mm_add_epi16(shuffle, m2);
  5806. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5807. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5808. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5809. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5810. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5811. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5812. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5813. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5814. hmask = _mm_slli_epi16(hmask, 1);
  5815. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5816. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5817. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5818. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5819. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5820. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5821. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5822. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5823. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5824. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5825. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5826. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5827. hmask = _mm_slli_epi16(hmask, 1);
  5828. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5829. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5830. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5831. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5832. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5833. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5834. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5835. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5836. }
  5837. __m256 vd = _mm256_set1_ps(d);
  5838. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5839. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5840. }
  5841. *s = hsum_float_8(acc) + summs;
  5842. #elif defined __riscv_v_intrinsic
  5843. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5844. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5845. float sumf = 0;
  5846. float sums = 0.0;
  5847. size_t vl;
  5848. for (int i = 0; i < nb; ++i) {
  5849. vl = 8;
  5850. const uint8_t * restrict q5 = x[i].qs;
  5851. const uint8_t * restrict hm = x[i].qh;
  5852. const int8_t * restrict q8 = y[i].qs;
  5853. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5854. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5855. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5856. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5857. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5858. memcpy(utmp, x[i].scales, 12);
  5859. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5860. const uint32_t uaux = utmp[1] & kmask1;
  5861. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5862. utmp[2] = uaux;
  5863. utmp[0] &= kmask1;
  5864. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5865. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5866. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5867. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5868. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5869. vl = 32;
  5870. int32_t aux32 = 0;
  5871. int is = 0;
  5872. uint8_t m = 1;
  5873. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5874. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5875. for (int j = 0; j < QK_K/64; ++j) {
  5876. // load Q5 and Q8
  5877. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5878. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5879. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5880. // compute mask for addition
  5881. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5882. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5883. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5884. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5885. m <<= 1;
  5886. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5887. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5888. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5889. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5890. m <<= 1;
  5891. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5892. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5893. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5894. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5895. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5896. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5897. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5898. q5 += 32; q8 += 64;
  5899. }
  5900. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5901. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5902. }
  5903. *s = sumf+sums;
  5904. #else
  5905. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5906. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5907. int8_t aux8[QK_K];
  5908. int16_t aux16[8];
  5909. float sums [8];
  5910. int32_t aux32[8];
  5911. memset(sums, 0, 8*sizeof(float));
  5912. float sumf = 0;
  5913. for (int i = 0; i < nb; ++i) {
  5914. const uint8_t * restrict q4 = x[i].qs;
  5915. const uint8_t * restrict hm = x[i].qh;
  5916. const int8_t * restrict q8 = y[i].qs;
  5917. memset(aux32, 0, 8*sizeof(int32_t));
  5918. int8_t * restrict a = aux8;
  5919. uint8_t m = 1;
  5920. for (int j = 0; j < QK_K/64; ++j) {
  5921. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5922. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5923. a += 32; m <<= 1;
  5924. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5925. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5926. a += 32; m <<= 1;
  5927. q4 += 32;
  5928. }
  5929. memcpy(utmp, x[i].scales, 12);
  5930. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5931. const uint32_t uaux = utmp[1] & kmask1;
  5932. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5933. utmp[2] = uaux;
  5934. utmp[0] &= kmask1;
  5935. int sumi = 0;
  5936. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5937. a = aux8;
  5938. int is = 0;
  5939. for (int j = 0; j < QK_K/32; ++j) {
  5940. int32_t scale = scales[is++];
  5941. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5942. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5943. q8 += 8; a += 8;
  5944. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5945. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5946. q8 += 8; a += 8;
  5947. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5948. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5949. q8 += 8; a += 8;
  5950. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5951. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5952. q8 += 8; a += 8;
  5953. }
  5954. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5955. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5956. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5957. sumf -= dmin * sumi;
  5958. }
  5959. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5960. *s = sumf;
  5961. #endif
  5962. }
  5963. #else
  5964. 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) {
  5965. assert(n % QK_K == 0);
  5966. assert(nrc == 1);
  5967. UNUSED(nrc);
  5968. UNUSED(bx);
  5969. UNUSED(by);
  5970. UNUSED(bs);
  5971. const block_q5_K * restrict x = vx;
  5972. const block_q8_K * restrict y = vy;
  5973. const int nb = n / QK_K;
  5974. #ifdef __ARM_NEON
  5975. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5976. const uint8x16_t mh = vdupq_n_u8(16);
  5977. const int32x4_t mzero = vdupq_n_s32(0);
  5978. ggml_int8x16x4_t q5bytes;
  5979. ggml_uint8x16x4_t q5h;
  5980. float sumf = 0;
  5981. for (int i = 0; i < nb; ++i) {
  5982. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5983. const int8_t * sc = x[i].scales;
  5984. const uint8_t * restrict q5 = x[i].qs;
  5985. const uint8_t * restrict qh = x[i].qh;
  5986. const int8_t * restrict q8 = y[i].qs;
  5987. const uint8x8_t qhbits = vld1_u8(qh);
  5988. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5989. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5990. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5991. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5992. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5993. q5h.val[2] = vbicq_u8(mh, htmp);
  5994. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5995. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5996. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5997. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5998. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5999. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  6000. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  6001. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  6002. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  6003. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  6004. }
  6005. *s = sumf;
  6006. #elif defined __AVX2__
  6007. const __m256i m4 = _mm256_set1_epi8(0xF);
  6008. const __m256i mone = _mm256_set1_epi8(1);
  6009. __m256 acc = _mm256_setzero_ps();
  6010. for (int i = 0; i < nb; ++i) {
  6011. const uint8_t * restrict q5 = x[i].qs;
  6012. const int8_t * restrict q8 = y[i].qs;
  6013. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6014. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  6015. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  6016. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  6017. int64_t aux64;
  6018. memcpy(&aux64, x[i].qh, 8);
  6019. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  6020. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  6021. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  6022. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  6023. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  6024. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  6025. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6026. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6027. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  6028. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  6029. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  6030. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  6031. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  6032. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  6033. }
  6034. *s = hsum_float_8(acc);
  6035. #elif defined __AVX__
  6036. const __m128i m4 = _mm_set1_epi8(0xF);
  6037. const __m128i mone = _mm_set1_epi8(1);
  6038. __m256 acc = _mm256_setzero_ps();
  6039. for (int i = 0; i < nb; ++i) {
  6040. const uint8_t * restrict q5 = x[i].qs;
  6041. const int8_t * restrict q8 = y[i].qs;
  6042. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6043. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  6044. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  6045. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  6046. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  6047. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  6048. int64_t aux64;
  6049. memcpy(&aux64, x[i].qh, 8);
  6050. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  6051. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  6052. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  6053. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  6054. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  6055. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  6056. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  6057. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  6058. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  6059. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  6060. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6061. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6062. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  6063. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  6064. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  6065. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  6066. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  6067. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  6068. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  6069. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  6070. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  6071. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  6072. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  6073. }
  6074. *s = hsum_float_8(acc);
  6075. #elif defined __riscv_v_intrinsic
  6076. float sumf = 0;
  6077. for (int i = 0; i < nb; ++i) {
  6078. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6079. const int8_t * sc = x[i].scales;
  6080. const uint8_t * restrict q5 = x[i].qs;
  6081. const uint8_t * restrict qh = x[i].qh;
  6082. const int8_t * restrict q8 = y[i].qs;
  6083. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6084. // load qh
  6085. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  6086. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  6087. size_t vl = 16;
  6088. // combine both qh_1 and qh_2
  6089. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  6090. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  6091. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  6092. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  6093. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  6094. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  6095. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  6096. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  6097. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  6098. // load q5
  6099. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  6100. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  6101. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  6102. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  6103. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  6104. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  6105. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  6106. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  6107. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  6108. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  6109. // load Q8 and multiply it with Q5
  6110. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6111. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6112. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6113. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6114. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6115. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6116. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6117. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6118. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  6119. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  6120. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  6121. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  6122. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  6123. }
  6124. *s = sumf;
  6125. #else
  6126. int8_t aux8[QK_K];
  6127. int16_t aux16[16];
  6128. float sums [8];
  6129. memset(sums, 0, 8*sizeof(float));
  6130. float sumf = 0;
  6131. for (int i = 0; i < nb; ++i) {
  6132. const uint8_t * restrict q4 = x[i].qs;
  6133. const uint8_t * restrict hm = x[i].qh;
  6134. const int8_t * restrict q8 = y[i].qs;
  6135. int8_t * restrict a = aux8;
  6136. for (int l = 0; l < 32; ++l) {
  6137. a[l+ 0] = q4[l] & 0xF;
  6138. a[l+32] = q4[l] >> 4;
  6139. }
  6140. for (int is = 0; is < 8; ++is) {
  6141. uint8_t m = 1 << is;
  6142. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  6143. }
  6144. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6145. const int8_t * restrict sc = x[i].scales;
  6146. for (int j = 0; j < QK_K/16; ++j) {
  6147. const float dl = d * sc[j];
  6148. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  6149. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  6150. q8 += 16; a += 16;
  6151. }
  6152. }
  6153. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6154. *s = sumf;
  6155. #endif
  6156. }
  6157. #endif
  6158. #if QK_K == 256
  6159. 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) {
  6160. assert(n % QK_K == 0);
  6161. assert(nrc == 1);
  6162. UNUSED(nrc);
  6163. UNUSED(bx);
  6164. UNUSED(by);
  6165. UNUSED(bs);
  6166. const block_q6_K * restrict x = vx;
  6167. const block_q8_K * restrict y = vy;
  6168. const int nb = n / QK_K;
  6169. #ifdef __ARM_NEON
  6170. float sum = 0;
  6171. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6172. const int32x4_t vzero = vdupq_n_s32(0);
  6173. //const int8x16_t m32s = vdupq_n_s8(32);
  6174. const uint8x16_t mone = vdupq_n_u8(3);
  6175. ggml_int8x16x4_t q6bytes;
  6176. ggml_uint8x16x4_t q6h;
  6177. for (int i = 0; i < nb; ++i) {
  6178. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6179. const uint8_t * restrict q6 = x[i].ql;
  6180. const uint8_t * restrict qh = x[i].qh;
  6181. const int8_t * restrict q8 = y[i].qs;
  6182. const int8_t * restrict scale = x[i].scales;
  6183. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  6184. const int8x16_t scales = vld1q_s8(scale);
  6185. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  6186. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  6187. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  6188. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  6189. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  6190. int32_t isum_mins = vaddvq_s32(prod);
  6191. int32_t isum = 0;
  6192. for (int j = 0; j < QK_K/128; ++j) {
  6193. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  6194. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  6195. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6196. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  6197. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  6198. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  6199. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6200. shifted = vshrq_n_u8(qhbits.val[1], 2);
  6201. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6202. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6203. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6204. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  6205. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  6206. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  6207. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6208. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6209. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6210. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6211. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6212. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6213. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6214. scale += 4;
  6215. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6216. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6217. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6218. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6219. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6220. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6221. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6222. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6223. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6224. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6225. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6226. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6227. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6228. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6229. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6230. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6231. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6232. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6233. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6234. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6235. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6236. scale += 4;
  6237. }
  6238. //sum += isum * d_all * y[i].d;
  6239. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6240. }
  6241. *s = sum;
  6242. #elif defined __AVX2__
  6243. const __m256i m4 = _mm256_set1_epi8(0xF);
  6244. const __m256i m2 = _mm256_set1_epi8(3);
  6245. const __m256i m32s = _mm256_set1_epi8(32);
  6246. __m256 acc = _mm256_setzero_ps();
  6247. for (int i = 0; i < nb; ++i) {
  6248. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6249. const uint8_t * restrict q4 = x[i].ql;
  6250. const uint8_t * restrict qh = x[i].qh;
  6251. const int8_t * restrict q8 = y[i].qs;
  6252. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6253. __m256i sumi = _mm256_setzero_si256();
  6254. int is = 0;
  6255. for (int j = 0; j < QK_K/128; ++j) {
  6256. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6257. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6258. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6259. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6260. is += 4;
  6261. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6262. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6263. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6264. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6265. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6266. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6267. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6268. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6269. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6270. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6271. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6272. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6273. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6274. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6275. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6276. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6277. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6278. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6279. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6280. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6281. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6282. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6283. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6284. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6285. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6286. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6287. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6288. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6289. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6290. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6291. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6292. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6293. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6294. }
  6295. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6296. }
  6297. *s = hsum_float_8(acc);
  6298. #elif defined __AVX__
  6299. const __m128i m4 = _mm_set1_epi8(0xF);
  6300. const __m128i m3 = _mm_set1_epi8(3);
  6301. const __m128i m32s = _mm_set1_epi8(32);
  6302. const __m128i m2 = _mm_set1_epi8(2);
  6303. __m256 acc = _mm256_setzero_ps();
  6304. for (int i = 0; i < nb; ++i) {
  6305. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6306. const uint8_t * restrict q4 = x[i].ql;
  6307. const uint8_t * restrict qh = x[i].qh;
  6308. const int8_t * restrict q8 = y[i].qs;
  6309. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6310. __m128i sumi_0 = _mm_setzero_si128();
  6311. __m128i sumi_1 = _mm_setzero_si128();
  6312. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6313. for (int j = 0; j < QK_K/128; ++j) {
  6314. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6315. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6316. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6317. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6318. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6319. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6320. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6321. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6322. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6323. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6324. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6325. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6326. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6327. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6328. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6329. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6330. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6331. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6332. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6333. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6334. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6335. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6336. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6337. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6338. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6339. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6340. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6341. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6342. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6343. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6344. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6345. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6346. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6347. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6348. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6349. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6350. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6351. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6352. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6353. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6354. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6355. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6356. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6357. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6358. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6359. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6360. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6361. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6362. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6363. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6364. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6365. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6366. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6367. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6368. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6369. shuffle = _mm_add_epi8(shuffle, m2);
  6370. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6371. shuffle = _mm_add_epi8(shuffle, m2);
  6372. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6373. shuffle = _mm_add_epi8(shuffle, m2);
  6374. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6375. shuffle = _mm_add_epi8(shuffle, m2);
  6376. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6377. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6378. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6379. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6380. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6381. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6382. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6383. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6384. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6385. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6386. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6387. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6388. }
  6389. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6390. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6391. }
  6392. *s = hsum_float_8(acc);
  6393. #elif defined __riscv_v_intrinsic
  6394. float sumf = 0;
  6395. for (int i = 0; i < nb; ++i) {
  6396. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6397. const uint8_t * restrict q6 = x[i].ql;
  6398. const uint8_t * restrict qh = x[i].qh;
  6399. const int8_t * restrict q8 = y[i].qs;
  6400. const int8_t * restrict scale = x[i].scales;
  6401. size_t vl;
  6402. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6403. int sum_t = 0;
  6404. int is = 0;
  6405. for (int j = 0; j < QK_K/128; ++j) {
  6406. vl = 32;
  6407. // load qh
  6408. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6409. // load Q6
  6410. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6411. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6412. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6413. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6414. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6415. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6416. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6417. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6418. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6419. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6420. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6421. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6422. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6423. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6424. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6425. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6426. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6427. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6428. // load Q8 and take product
  6429. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6430. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6431. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6432. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6433. vl = 16;
  6434. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6435. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6436. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6437. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6438. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6439. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6440. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6441. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6442. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6443. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6444. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6445. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6446. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6447. q6 += 64; qh += 32; q8 += 128; is=8;
  6448. }
  6449. sumf += d * sum_t;
  6450. }
  6451. *s = sumf;
  6452. #else
  6453. int8_t aux8[QK_K];
  6454. int16_t aux16[8];
  6455. float sums [8];
  6456. int32_t aux32[8];
  6457. memset(sums, 0, 8*sizeof(float));
  6458. float sumf = 0;
  6459. for (int i = 0; i < nb; ++i) {
  6460. const uint8_t * restrict q4 = x[i].ql;
  6461. const uint8_t * restrict qh = x[i].qh;
  6462. const int8_t * restrict q8 = y[i].qs;
  6463. memset(aux32, 0, 8*sizeof(int32_t));
  6464. int8_t * restrict a = aux8;
  6465. for (int j = 0; j < QK_K; j += 128) {
  6466. for (int l = 0; l < 32; ++l) {
  6467. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6468. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6469. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6470. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6471. }
  6472. a += 128;
  6473. q4 += 64;
  6474. qh += 32;
  6475. }
  6476. a = aux8;
  6477. int is = 0;
  6478. for (int j = 0; j < QK_K/16; ++j) {
  6479. int scale = x[i].scales[is++];
  6480. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6481. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6482. q8 += 8; a += 8;
  6483. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6484. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6485. q8 += 8; a += 8;
  6486. }
  6487. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6488. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6489. }
  6490. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6491. *s = sumf;
  6492. #endif
  6493. }
  6494. #else
  6495. 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) {
  6496. assert(n % QK_K == 0);
  6497. assert(nrc == 1);
  6498. UNUSED(nrc);
  6499. UNUSED(bx);
  6500. UNUSED(by);
  6501. UNUSED(bs);
  6502. const block_q6_K * restrict x = vx;
  6503. const block_q8_K * restrict y = vy;
  6504. const int nb = n / QK_K;
  6505. #ifdef __ARM_NEON
  6506. float sum = 0;
  6507. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6508. const int8x16_t m32s = vdupq_n_s8(32);
  6509. const int32x4_t vzero = vdupq_n_s32(0);
  6510. const uint8x16_t mone = vdupq_n_u8(3);
  6511. ggml_int8x16x4_t q6bytes;
  6512. ggml_uint8x16x4_t q6h;
  6513. for (int i = 0; i < nb; ++i) {
  6514. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6515. const uint8_t * restrict q6 = x[i].ql;
  6516. const uint8_t * restrict qh = x[i].qh;
  6517. const int8_t * restrict q8 = y[i].qs;
  6518. const int8_t * restrict scale = x[i].scales;
  6519. int32_t isum = 0;
  6520. uint8x16_t qhbits = vld1q_u8(qh);
  6521. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6522. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6523. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6524. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6525. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6526. shifted = vshrq_n_u8(qhbits, 4);
  6527. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6528. shifted = vshrq_n_u8(qhbits, 6);
  6529. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6530. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6531. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6532. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6533. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6534. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6535. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6536. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6537. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6538. sum += isum * d_all * y[i].d;
  6539. }
  6540. *s = sum;
  6541. #elif defined __AVX2__
  6542. const __m256i m4 = _mm256_set1_epi8(0xF);
  6543. const __m256i m2 = _mm256_set1_epi8(3);
  6544. const __m256i m32s = _mm256_set1_epi8(32);
  6545. __m256 acc = _mm256_setzero_ps();
  6546. for (int i = 0; i < nb; ++i) {
  6547. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6548. const uint8_t * restrict q4 = x[i].ql;
  6549. const uint8_t * restrict qh = x[i].qh;
  6550. const int8_t * restrict q8 = y[i].qs;
  6551. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6552. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6553. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6554. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6555. __m256i sumi = _mm256_setzero_si256();
  6556. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6557. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6558. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6559. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6560. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6561. 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);
  6562. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6563. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6564. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6565. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6566. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6567. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6568. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6569. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6570. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6571. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6572. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6573. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6574. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6575. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6576. }
  6577. *s = hsum_float_8(acc);
  6578. #elif defined __AVX__
  6579. const __m128i m4 = _mm_set1_epi8(0xF);
  6580. const __m128i m2 = _mm_set1_epi8(3);
  6581. const __m128i m32s = _mm_set1_epi8(32);
  6582. __m256 acc = _mm256_setzero_ps();
  6583. for (int i = 0; i < nb; ++i) {
  6584. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6585. const uint8_t * restrict q4 = x[i].ql;
  6586. const uint8_t * restrict qh = x[i].qh;
  6587. const int8_t * restrict q8 = y[i].qs;
  6588. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6589. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6590. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6591. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6592. __m128i sumi_0 = _mm_setzero_si128();
  6593. __m128i sumi_1 = _mm_setzero_si128();
  6594. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6595. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6596. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6597. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6598. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6599. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6600. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6601. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6602. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6603. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6604. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6605. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6606. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6607. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6608. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6609. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6610. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6611. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6612. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6613. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6614. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6615. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6616. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6617. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6618. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6619. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6620. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6621. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6622. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6623. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6624. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6625. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6626. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6627. }
  6628. *s = hsum_float_8(acc);
  6629. #elif defined __riscv_v_intrinsic
  6630. float sumf = 0;
  6631. for (int i = 0; i < nb; ++i) {
  6632. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  6633. const uint8_t * restrict q6 = x[i].ql;
  6634. const uint8_t * restrict qh = x[i].qh;
  6635. const int8_t * restrict q8 = y[i].qs;
  6636. const int8_t * restrict scale = x[i].scales;
  6637. int32_t isum = 0;
  6638. size_t vl = 16;
  6639. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6640. // load Q6
  6641. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6642. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6643. // load qh
  6644. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6645. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6646. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6647. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6648. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6649. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6650. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6651. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6652. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6653. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6654. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6655. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6656. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6657. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6658. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6659. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6660. // load Q8 and take product
  6661. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6662. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6663. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6664. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6665. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6666. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6667. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6668. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6669. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6670. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6671. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6672. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6673. sumf += isum * d_all * y[i].d;
  6674. }
  6675. *s = sumf;
  6676. #else
  6677. int8_t aux8[QK_K];
  6678. int16_t aux16[8];
  6679. float sums [8];
  6680. int32_t aux32[8];
  6681. memset(sums, 0, 8*sizeof(float));
  6682. float sumf = 0;
  6683. for (int i = 0; i < nb; ++i) {
  6684. const uint8_t * restrict q4 = x[i].ql;
  6685. const uint8_t * restrict qh = x[i].qh;
  6686. const int8_t * restrict q8 = y[i].qs;
  6687. memset(aux32, 0, 8*sizeof(int32_t));
  6688. int8_t * restrict a = aux8;
  6689. for (int l = 0; l < 16; ++l) {
  6690. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6691. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6692. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6693. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6694. }
  6695. int is = 0;
  6696. for (int j = 0; j < QK_K/16; ++j) {
  6697. int scale = x[i].scales[is++];
  6698. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6699. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6700. q8 += 8; a += 8;
  6701. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6702. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6703. q8 += 8; a += 8;
  6704. }
  6705. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6706. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6707. }
  6708. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6709. *s = sumf;
  6710. #endif
  6711. }
  6712. #endif
  6713. #if defined (__AVX2__) || defined (__ARM_NEON)
  6714. static const int8_t keven_signs_q2xs[1024] = {
  6715. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6716. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6717. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6718. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6719. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6720. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6721. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6722. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6723. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6724. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6725. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6726. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6727. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6728. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6729. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6730. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6731. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6732. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6733. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6734. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6735. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6736. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6737. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6738. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6739. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6740. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6741. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6742. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6743. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6744. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6745. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6746. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6747. };
  6748. #endif
  6749. 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) {
  6750. assert(n % QK_K == 0);
  6751. assert(nrc == 1);
  6752. UNUSED(nrc);
  6753. UNUSED(bx);
  6754. UNUSED(by);
  6755. UNUSED(bs);
  6756. const block_iq2_xxs * restrict x = vx;
  6757. const block_q8_K * restrict y = vy;
  6758. const int nb = n / QK_K;
  6759. #if defined(__ARM_NEON)
  6760. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6761. uint32_t aux32[4];
  6762. const uint8_t * aux8 = (const uint8_t *)aux32;
  6763. ggml_int8x16x4_t q2u;
  6764. ggml_int8x16x4_t q2s;
  6765. ggml_int8x16x4_t q8b;
  6766. float sumf = 0;
  6767. for (int i = 0; i < nb; ++i) {
  6768. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6769. const uint16_t * restrict q2 = x[i].qs;
  6770. const int8_t * restrict q8 = y[i].qs;
  6771. float sumf1 = 0, sumf2 = 0;
  6772. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6773. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6774. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6775. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6776. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6777. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6778. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6779. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6780. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6781. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6782. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6783. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6784. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6785. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6786. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6787. 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]);
  6788. 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]);
  6789. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6790. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6791. }
  6792. sumf += d*(sumf1 + sumf2);
  6793. }
  6794. *s = 0.25f * sumf;
  6795. #elif defined(__AVX2__)
  6796. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6797. uint32_t aux32[4];
  6798. const uint8_t * aux8 = (const uint8_t *)aux32;
  6799. __m256 accumf = _mm256_setzero_ps();
  6800. for (int i = 0; i < nb; ++i) {
  6801. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6802. const uint16_t * restrict q2 = x[i].qs;
  6803. const int8_t * restrict q8 = y[i].qs;
  6804. __m256i sumi1 = _mm256_setzero_si256();
  6805. __m256i sumi2 = _mm256_setzero_si256();
  6806. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6807. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6808. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6809. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6810. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6811. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6812. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6813. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6814. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6815. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6816. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6817. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6818. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6819. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6820. const uint16_t ls1 = aux32[1] >> 28;
  6821. const uint16_t ls2 = aux32[3] >> 28;
  6822. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6823. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6824. sumi1 = _mm256_add_epi32(sumi1, p1);
  6825. sumi2 = _mm256_add_epi32(sumi2, p2);
  6826. }
  6827. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6828. }
  6829. *s = 0.125f * hsum_float_8(accumf);
  6830. #else
  6831. uint32_t aux32[2];
  6832. const uint8_t * aux8 = (const uint8_t *)aux32;
  6833. float sumf = 0.f;
  6834. for (int i = 0; i < nb; ++i) {
  6835. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6836. const uint16_t * restrict q2 = x[i].qs;
  6837. const int8_t * restrict q8 = y[i].qs;
  6838. int32_t bsum = 0;
  6839. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6840. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6841. q2 += 4;
  6842. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6843. int32_t sumi = 0;
  6844. for (int l = 0; l < 4; ++l) {
  6845. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6846. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6847. for (int j = 0; j < 8; ++j) {
  6848. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6849. }
  6850. q8 += 8;
  6851. }
  6852. bsum += sumi * ls;
  6853. }
  6854. sumf += d * bsum;
  6855. }
  6856. *s = 0.125f * sumf;
  6857. #endif
  6858. }
  6859. 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) {
  6860. assert(n % QK_K == 0);
  6861. assert(nrc == 1);
  6862. UNUSED(nrc);
  6863. UNUSED(bx);
  6864. UNUSED(by);
  6865. UNUSED(bs);
  6866. const block_iq2_xs * restrict x = vx;
  6867. const block_q8_K * restrict y = vy;
  6868. const int nb = n / QK_K;
  6869. #if defined(__ARM_NEON)
  6870. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6871. ggml_int8x16x4_t q2u;
  6872. ggml_int8x16x4_t q2s;
  6873. ggml_int8x16x4_t q8b;
  6874. int32x4x4_t scales32;
  6875. float sumf = 0;
  6876. for (int i = 0; i < nb; ++i) {
  6877. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6878. const uint16_t * restrict q2 = x[i].qs;
  6879. const int8_t * restrict q8 = y[i].qs;
  6880. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6881. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6882. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6883. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6884. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6885. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6886. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6887. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6888. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6889. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6890. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6891. int32x4_t sumi = vdupq_n_s32(0);
  6892. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6893. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6894. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6895. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6896. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6897. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6898. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6899. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6900. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6901. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6902. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6903. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6904. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6905. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6906. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6907. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6908. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6909. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6910. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6911. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6912. q2 += 8;
  6913. }
  6914. sumf += d*vaddvq_s32(sumi);
  6915. }
  6916. *s = 0.125f * sumf;
  6917. #elif defined(__AVX2__)
  6918. const __m256i mone = _mm256_set1_epi8(1);
  6919. static const char block_sign_shuffle_mask_1[32] = {
  6920. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  6921. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  6922. };
  6923. static const char block_sign_shuffle_mask_2[32] = {
  6924. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
  6925. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e, 0x0e,
  6926. };
  6927. static const uint8_t bit_selector_mask_bytes[32] = {
  6928. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6929. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  6930. };
  6931. const __m256i bit_selector_mask = _mm256_loadu_si256((const __m256i*)bit_selector_mask_bytes);
  6932. const __m256i block_sign_shuffle_1 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_1);
  6933. const __m256i block_sign_shuffle_2 = _mm256_loadu_si256((const __m256i*)block_sign_shuffle_mask_2);
  6934. #if QK_K == 64
  6935. static const uint8_t k_bit_helper[16] = {
  6936. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6937. };
  6938. const __m128i bit_helper = _mm_loadu_si128((const __m128i*)k_bit_helper);
  6939. const __m128i m511 = _mm_set1_epi16(511);
  6940. typedef union {
  6941. __m128i vec_index;
  6942. uint16_t index[8];
  6943. } index_t;
  6944. index_t idx;
  6945. __m256 accumf = _mm256_setzero_ps();
  6946. for (int i = 0; i < nb; ++i) {
  6947. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6948. const __m128i q2_data = _mm_loadu_si128((const __m128i*)x[i].qs);
  6949. idx.vec_index = _mm_and_si128(q2_data, m511);
  6950. const __m128i partial_sign_bits = _mm_srli_epi16(q2_data, 9);
  6951. const __m128i partial_sign_bits_upper = _mm_srli_epi16(q2_data, 13);
  6952. const __m128i partial_sign_bits_for_counting = _mm_xor_si128(partial_sign_bits, partial_sign_bits_upper);
  6953. const __m128i odd_bits = _mm_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  6954. const __m128i full_sign_bits = _mm_or_si128(partial_sign_bits, odd_bits);
  6955. const __m256i full_signs = MM256_SET_M128I(full_sign_bits, full_sign_bits);
  6956. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)y[i].qs);
  6957. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)(y[i].qs+32));
  6958. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[idx.index[3]], iq2xs_grid[idx.index[2]],
  6959. iq2xs_grid[idx.index[1]], iq2xs_grid[idx.index[0]]);
  6960. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[idx.index[7]], iq2xs_grid[idx.index[6]],
  6961. iq2xs_grid[idx.index[5]], iq2xs_grid[idx.index[4]]);
  6962. __m256i signs;
  6963. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_1);
  6964. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6965. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  6966. signs = _mm256_shuffle_epi8(full_signs, block_sign_shuffle_2);
  6967. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  6968. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  6969. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6970. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6971. 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));
  6972. 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));
  6973. const __m256i sum = _mm256_add_epi32(_mm256_madd_epi16(sc1, dot1), _mm256_madd_epi16(sc2, dot2));
  6974. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sum), accumf);
  6975. }
  6976. *s = 0.125f * hsum_float_8(accumf);
  6977. #else
  6978. static const uint8_t k_bit_helper[32] = {
  6979. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6980. 0x00, 0x80, 0x80, 0x00, 0x80, 0x00, 0x00, 0x80, 0x80, 0x00, 0x00, 0x80, 0x00, 0x80, 0x80, 0x00,
  6981. };
  6982. const __m256i bit_helper = _mm256_loadu_si256((const __m256i*)k_bit_helper);
  6983. const __m256i m511 = _mm256_set1_epi16(511);
  6984. const __m128i m4 = _mm_set1_epi8(0xf);
  6985. const __m128i m1 = _mm_set1_epi8(1);
  6986. uint64_t aux64;
  6987. // somewhat hacky, but gives a significant boost in performance
  6988. __m256i aux_gindex;
  6989. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  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 uint16_t * restrict q2 = x[i].qs;
  6994. const int8_t * restrict q8 = y[i].qs;
  6995. memcpy(&aux64, x[i].scales, 8);
  6996. __m128i stmp = _mm_set1_epi64x(aux64);
  6997. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6998. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6999. __m256i sumi1 = _mm256_setzero_si256();
  7000. __m256i sumi2 = _mm256_setzero_si256();
  7001. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 4) {
  7002. const __m256i q2_data = _mm256_loadu_si256((const __m256i*)q2); q2 += 16;
  7003. aux_gindex = _mm256_and_si256(q2_data, m511);
  7004. const __m256i partial_sign_bits = _mm256_srli_epi16(q2_data, 9);
  7005. const __m256i partial_sign_bits_upper = _mm256_srli_epi16(q2_data, 13);
  7006. const __m256i partial_sign_bits_for_counting = _mm256_xor_si256(partial_sign_bits, partial_sign_bits_upper);
  7007. const __m256i odd_bits = _mm256_shuffle_epi8(bit_helper, partial_sign_bits_for_counting);
  7008. const __m256i full_sign_bits = _mm256_or_si256(partial_sign_bits, odd_bits);
  7009. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7010. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7011. const __m256i q8_3 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7012. const __m256i q8_4 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7013. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[ 3]], iq2xs_grid[gindex[ 2]],
  7014. iq2xs_grid[gindex[ 1]], iq2xs_grid[gindex[ 0]]);
  7015. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[ 7]], iq2xs_grid[gindex[ 6]],
  7016. iq2xs_grid[gindex[ 5]], iq2xs_grid[gindex[ 4]]);
  7017. const __m256i q2_3 = _mm256_set_epi64x(iq2xs_grid[gindex[11]], iq2xs_grid[gindex[10]],
  7018. iq2xs_grid[gindex[ 9]], iq2xs_grid[gindex[ 8]]);
  7019. const __m256i q2_4 = _mm256_set_epi64x(iq2xs_grid[gindex[15]], iq2xs_grid[gindex[14]],
  7020. iq2xs_grid[gindex[13]], iq2xs_grid[gindex[12]]);
  7021. const __m128i full_signs_l = _mm256_castsi256_si128(full_sign_bits);
  7022. const __m128i full_signs_h = _mm256_extractf128_si256(full_sign_bits, 1);
  7023. const __m256i full_signs_1 = MM256_SET_M128I(full_signs_l, full_signs_l);
  7024. const __m256i full_signs_2 = MM256_SET_M128I(full_signs_h, full_signs_h);
  7025. __m256i signs;
  7026. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_1);
  7027. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7028. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, _mm256_or_si256(signs, mone));
  7029. signs = _mm256_shuffle_epi8(full_signs_1, block_sign_shuffle_2);
  7030. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7031. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, _mm256_or_si256(signs, mone));
  7032. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_1);
  7033. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7034. const __m256i q8s_3 = _mm256_sign_epi8(q8_3, _mm256_or_si256(signs, mone));
  7035. signs = _mm256_shuffle_epi8(full_signs_2, block_sign_shuffle_2);
  7036. signs = _mm256_cmpeq_epi8(_mm256_and_si256(signs, bit_selector_mask), bit_selector_mask);
  7037. const __m256i q8s_4 = _mm256_sign_epi8(q8_4, _mm256_or_si256(signs, mone));
  7038. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7039. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7040. const __m256i dot3 = _mm256_maddubs_epi16(q2_3, q8s_3);
  7041. const __m256i dot4 = _mm256_maddubs_epi16(q2_4, q8s_4);
  7042. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  7043. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  7044. const __m256i sc3 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+2)));
  7045. const __m256i sc4 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+3)));
  7046. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  7047. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  7048. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot3, sc3));
  7049. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot4, sc4));
  7050. }
  7051. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7052. }
  7053. *s = 0.125f * hsum_float_8(accumf);
  7054. #endif
  7055. #else
  7056. float sumf = 0.f;
  7057. for (int i = 0; i < nb; ++i) {
  7058. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7059. const uint16_t * restrict q2 = x[i].qs;
  7060. const uint8_t * restrict sc = x[i].scales;
  7061. const int8_t * restrict q8 = y[i].qs;
  7062. int32_t bsum = 0;
  7063. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7064. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  7065. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  7066. int32_t sumi = 0;
  7067. for (int l = 0; l < 2; ++l) {
  7068. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7069. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7070. for (int j = 0; j < 8; ++j) {
  7071. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7072. }
  7073. q8 += 8;
  7074. }
  7075. bsum += sumi * ls1;
  7076. sumi = 0;
  7077. for (int l = 2; l < 4; ++l) {
  7078. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  7079. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  7080. for (int j = 0; j < 8; ++j) {
  7081. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  7082. }
  7083. q8 += 8;
  7084. }
  7085. bsum += sumi * ls2;
  7086. q2 += 4;
  7087. }
  7088. sumf += d * bsum;
  7089. }
  7090. *s = 0.125f * sumf;
  7091. #endif
  7092. }
  7093. 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) {
  7094. assert(n % QK_K == 0);
  7095. assert(nrc == 1);
  7096. UNUSED(nrc);
  7097. UNUSED(bx);
  7098. UNUSED(by);
  7099. UNUSED(bs);
  7100. const block_iq2_s * restrict x = vx;
  7101. const block_q8_K * restrict y = vy;
  7102. const int nb = n / QK_K;
  7103. #if defined(__ARM_NEON)
  7104. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7105. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7106. };
  7107. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7108. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7109. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7110. const uint8x16_t m1 = vdupq_n_u8(1);
  7111. const int32x4_t vzero = vdupq_n_s32(0);
  7112. uint8x16x2_t vs;
  7113. ggml_int8x16x4_t q2s;
  7114. ggml_int8x16x4_t q8b;
  7115. float sumf = 0;
  7116. for (int i = 0; i < nb; ++i) {
  7117. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7118. const uint8_t * restrict qs = x[i].qs;
  7119. const uint8_t * restrict qh = x[i].qh;
  7120. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7121. const int8_t * restrict q8 = y[i].qs;
  7122. int sumi1 = 0, sumi2 = 0;
  7123. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7124. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7125. q2s.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[0] | ((qh[ib32+0] << 8) & 0x300)))),
  7126. vld1_s8((const int8_t *)(iq2s_grid + (qs[1] | ((qh[ib32+0] << 6) & 0x300)))));
  7127. q2s.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[2] | ((qh[ib32+0] << 4) & 0x300)))),
  7128. vld1_s8((const int8_t *)(iq2s_grid + (qs[3] | ((qh[ib32+0] << 2) & 0x300)))));
  7129. q2s.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[4] | ((qh[ib32+1] << 8) & 0x300)))),
  7130. vld1_s8((const int8_t *)(iq2s_grid + (qs[5] | ((qh[ib32+1] << 6) & 0x300)))));
  7131. q2s.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq2s_grid + (qs[6] | ((qh[ib32+1] << 4) & 0x300)))),
  7132. vld1_s8((const int8_t *)(iq2s_grid + (qs[7] | ((qh[ib32+1] << 2) & 0x300)))));
  7133. qs += 8;
  7134. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | (signs[1] << 16)));
  7135. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7136. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7137. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7138. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7139. q2s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[0]);
  7140. q2s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[1]);
  7141. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | (signs[3] << 16)));
  7142. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7143. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7144. vs.val[0] = vceqq_u8(vs.val[0], mask2);
  7145. vs.val[1] = vceqq_u8(vs.val[1], mask2);
  7146. signs += 4;
  7147. q2s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[0], m1)), q2s.val[2]);
  7148. q2s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vorrq_u8(vs.val[1], m1)), q2s.val[3]);
  7149. const int32x4_t p1 = ggml_vdotq_s32(vzero, q2s.val[0], q8b.val[0]);
  7150. const int32x4_t p2 = ggml_vdotq_s32(vzero, q2s.val[1], q8b.val[1]);
  7151. const int32x4_t p3 = ggml_vdotq_s32(vzero, q2s.val[2], q8b.val[2]);
  7152. const int32x4_t p4 = ggml_vdotq_s32(vzero, q2s.val[3], q8b.val[3]);
  7153. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32+0] & 0xf));
  7154. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32+0] >> 4));
  7155. sumi1 += vaddvq_s32(p3) * (1 + 2*(x[i].scales[ib32+1] & 0xf));
  7156. sumi2 += vaddvq_s32(p4) * (1 + 2*(x[i].scales[ib32+1] >> 4));
  7157. }
  7158. sumf += d*(sumi1 + sumi2);
  7159. }
  7160. *s = 0.125f * sumf;
  7161. #elif defined(__AVX2__)
  7162. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7163. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7164. };
  7165. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7166. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7167. };
  7168. const __m128i m4 = _mm_set1_epi8(0xf);
  7169. const __m128i m1 = _mm_set1_epi8(1);
  7170. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7171. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7172. uint64_t aux64;
  7173. __m256 accumf = _mm256_setzero_ps();
  7174. for (int i = 0; i < nb; ++i) {
  7175. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7176. const uint8_t * restrict qs = x[i].qs;
  7177. const uint8_t * restrict qh = x[i].qh;
  7178. const uint16_t * restrict signs = (const uint16_t *)(x[i].qs + QK_K/8);
  7179. const int8_t * restrict q8 = y[i].qs;
  7180. memcpy(&aux64, x[i].scales, 8);
  7181. const __m128i scales8 = _mm_add_epi8(_mm_slli_epi16(_mm_and_si128(_mm_set_epi64x(aux64 >> 4, aux64), m4), 1), m1);
  7182. const __m256i scales16 = _mm256_cvtepi8_epi16(scales8); // 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13 15
  7183. __m256i sumi1 = _mm256_setzero_si256();
  7184. __m256i sumi2 = _mm256_setzero_si256();
  7185. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7186. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7187. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7188. const __m256i q2_1 = _mm256_set_epi64x(iq2s_grid[qs[3] | ((qh[ib32+0] << 2) & 0x300)],
  7189. iq2s_grid[qs[2] | ((qh[ib32+0] << 4) & 0x300)],
  7190. iq2s_grid[qs[1] | ((qh[ib32+0] << 6) & 0x300)],
  7191. iq2s_grid[qs[0] | ((qh[ib32+0] << 8) & 0x300)]);
  7192. const __m256i q2_2 = _mm256_set_epi64x(iq2s_grid[qs[7] | ((qh[ib32+1] << 2) & 0x300)],
  7193. iq2s_grid[qs[6] | ((qh[ib32+1] << 4) & 0x300)],
  7194. iq2s_grid[qs[5] | ((qh[ib32+1] << 6) & 0x300)],
  7195. iq2s_grid[qs[4] | ((qh[ib32+1] << 8) & 0x300)]);
  7196. qs += 8;
  7197. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7198. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7199. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7200. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7201. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7202. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7203. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7204. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7205. signs += 4;
  7206. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1); // blocks 2*ib32+0, 2*ib32+1
  7207. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2); // blocks 2*ib32+2, 2*ib32+3
  7208. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+0)));
  7209. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_shuffle_epi8(scales16, get_scale_shuffle_k4(ib32+1)));
  7210. sumi1 = _mm256_add_epi32(sumi1, p1);
  7211. sumi2 = _mm256_add_epi32(sumi2, p2);
  7212. }
  7213. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7214. }
  7215. *s = 0.125f * hsum_float_8(accumf);
  7216. #else
  7217. float sumf = 0;
  7218. for (int i = 0; i < nb; i++) {
  7219. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7220. const int8_t * q8 = y[i].qs;
  7221. const uint8_t * qs = x[i].qs;
  7222. const uint8_t * qh = x[i].qh;
  7223. const uint8_t * signs = qs + QK_K/8;
  7224. int bsum = 0;
  7225. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7226. int ls1 = 1 + 2*(x[i].scales[ib32] & 0xf);
  7227. int ls2 = 1 + 2*(x[i].scales[ib32] >> 4);
  7228. int sumi1 = 0, sumi2 = 0;
  7229. for (int l = 0; l < 2; ++l) {
  7230. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7231. for (int j = 0; j < 8; ++j) {
  7232. sumi1 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7233. }
  7234. q8 += 8;
  7235. }
  7236. for (int l = 2; l < 4; ++l) {
  7237. const uint8_t * grid = (const uint8_t *)(iq2s_grid + (qs[l] | (qh[ib32] << (8-2*l) & 0x300)));
  7238. for (int j = 0; j < 8; ++j) {
  7239. sumi2 += q8[j] * grid[j] * (signs[l] & kmask_iq2xs[j] ? -1 : 1);
  7240. }
  7241. q8 += 8;
  7242. }
  7243. bsum += ls1 * sumi1 + ls2 * sumi2;
  7244. qs += 4;
  7245. signs += 4;
  7246. }
  7247. sumf += d * bsum;
  7248. }
  7249. *s = 0.125f * sumf;
  7250. #endif
  7251. }
  7252. 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) {
  7253. assert(n % QK_K == 0);
  7254. assert(nrc == 1);
  7255. UNUSED(nrc);
  7256. UNUSED(bx);
  7257. UNUSED(by);
  7258. UNUSED(bs);
  7259. const block_iq3_xxs * restrict x = vx;
  7260. const block_q8_K * restrict y = vy;
  7261. const int nb = n / QK_K;
  7262. #if defined(__ARM_NEON)
  7263. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7264. uint32_t aux32[2];
  7265. ggml_int8x16x4_t q3s;
  7266. ggml_int8x16x4_t q8b;
  7267. float sumf = 0;
  7268. for (int i = 0; i < nb; ++i) {
  7269. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7270. const uint8_t * restrict q3 = x[i].qs;
  7271. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7272. const int8_t * restrict q8 = y[i].qs;
  7273. float sumf1 = 0, sumf2 = 0;
  7274. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7275. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7276. memcpy(aux32, gas, 2*sizeof(uint32_t)); gas += 2*sizeof(uint32_t);
  7277. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3xxs_grid[q3[ 0]], iq3xxs_grid[q3[ 1]], iq3xxs_grid[q3[ 2]], iq3xxs_grid[q3[ 3]]);
  7278. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3xxs_grid[q3[ 4]], iq3xxs_grid[q3[ 5]], iq3xxs_grid[q3[ 6]], iq3xxs_grid[q3[ 7]]);
  7279. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3xxs_grid[q3[ 8]], iq3xxs_grid[q3[ 9]], iq3xxs_grid[q3[10]], iq3xxs_grid[q3[11]]);
  7280. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3xxs_grid[q3[12]], iq3xxs_grid[q3[13]], iq3xxs_grid[q3[14]], iq3xxs_grid[q3[15]]);
  7281. q3 += 16;
  7282. q3s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 7) & 127))));
  7283. q3s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[0] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[0] >> 21) & 127))));
  7284. q3s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  7285. q3s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  7286. q3s.val[0] = vmulq_s8(q3s.val[0], vreinterpretq_s8_u32(aux32x4_0));
  7287. q3s.val[1] = vmulq_s8(q3s.val[1], vreinterpretq_s8_u32(aux32x4_1));
  7288. q3s.val[2] = vmulq_s8(q3s.val[2], vreinterpretq_s8_u32(aux32x4_2));
  7289. q3s.val[3] = vmulq_s8(q3s.val[3], vreinterpretq_s8_u32(aux32x4_3));
  7290. 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]);
  7291. 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]);
  7292. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[0] >> 28));
  7293. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[1] >> 28));
  7294. }
  7295. sumf += d*(sumf1 + sumf2);
  7296. }
  7297. *s = 0.5f * sumf;
  7298. #elif defined(__AVX2__)
  7299. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  7300. uint32_t aux32[2];
  7301. __m256 accumf = _mm256_setzero_ps();
  7302. for (int i = 0; i < nb; ++i) {
  7303. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7304. const uint8_t * restrict q3 = x[i].qs;
  7305. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7306. const int8_t * restrict q8 = y[i].qs;
  7307. __m256i sumi1 = _mm256_setzero_si256();
  7308. __m256i sumi2 = _mm256_setzero_si256();
  7309. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7310. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7311. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7312. const __m256i q2_1 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7313. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7314. q3 += 8;
  7315. const __m256i q2_2 = _mm256_set_epi32(iq3xxs_grid[q3[7]], iq3xxs_grid[q3[6]], iq3xxs_grid[q3[5]], iq3xxs_grid[q3[4]],
  7316. iq3xxs_grid[q3[3]], iq3xxs_grid[q3[2]], iq3xxs_grid[q3[1]], iq3xxs_grid[q3[0]]);
  7317. q3 += 8;
  7318. memcpy(aux32, gas, 8); gas += 8;
  7319. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[0] >> 21) & 127], signs64[(aux32[0] >> 14) & 127],
  7320. signs64[(aux32[0] >> 7) & 127], signs64[(aux32[0] >> 0) & 127]);
  7321. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  7322. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  7323. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  7324. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  7325. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7326. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7327. const uint16_t ls1 = aux32[0] >> 28;
  7328. const uint16_t ls2 = aux32[1] >> 28;
  7329. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7330. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7331. sumi1 = _mm256_add_epi32(sumi1, p1);
  7332. sumi2 = _mm256_add_epi32(sumi2, p2);
  7333. }
  7334. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7335. }
  7336. *s = 0.25f * hsum_float_8(accumf);
  7337. #else
  7338. uint32_t aux32;
  7339. float sumf = 0.f;
  7340. for (int i = 0; i < nb; ++i) {
  7341. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7342. const uint8_t * restrict q3 = x[i].qs;
  7343. const uint8_t * restrict gas = x[i].qs + QK_K/4;
  7344. const int8_t * restrict q8 = y[i].qs;
  7345. int32_t bsum = 0;
  7346. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  7347. memcpy(&aux32, gas, sizeof(uint32_t)); gas += sizeof(uint32_t);
  7348. const uint32_t ls = 2*(aux32 >> 28) + 1;
  7349. int32_t sumi = 0;
  7350. for (int l = 0; l < 4; ++l) {
  7351. const uint8_t * grid1 = (const uint8_t *)(iq3xxs_grid + q3[2*l+0]);
  7352. const uint8_t * grid2 = (const uint8_t *)(iq3xxs_grid + q3[2*l+1]);
  7353. const uint8_t signs = ksigns_iq2xs[(aux32 >> 7*l) & 127];
  7354. for (int j = 0; j < 4; ++j) {
  7355. sumi += grid1[j] * q8[j+0] * (signs & kmask_iq2xs[j+0] ? -1 : 1);
  7356. sumi += grid2[j] * q8[j+4] * (signs & kmask_iq2xs[j+4] ? -1 : 1);
  7357. }
  7358. q8 += 8;
  7359. }
  7360. q3 += 8;
  7361. bsum += sumi * ls;
  7362. }
  7363. sumf += d * bsum;
  7364. }
  7365. *s = 0.25f * sumf;
  7366. #endif
  7367. }
  7368. 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) {
  7369. assert(n % QK_K == 0);
  7370. assert(nrc == 1);
  7371. UNUSED(nrc);
  7372. UNUSED(bx);
  7373. UNUSED(by);
  7374. UNUSED(bs);
  7375. const block_iq3_s * restrict x = vx;
  7376. const block_q8_K * restrict y = vy;
  7377. const int nb = n / QK_K;
  7378. #if defined(__ARM_NEON)
  7379. typedef union {
  7380. uint16x8_t vec_index;
  7381. uint16_t index[8];
  7382. } vec_index_t;
  7383. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7384. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7385. };
  7386. static const uint8_t k_mask2[16] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,};
  7387. static const int16_t k_shift[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  7388. const ggml_uint8x16x2_t mask1 = ggml_vld1q_u8_x2(k_mask1);
  7389. const uint8x16_t mask2 = vld1q_u8(k_mask2);
  7390. const int16x8_t hshift = vld1q_s16(k_shift);
  7391. const uint16x8_t m256 = vdupq_n_u16(256);
  7392. const uint8x16_t m1 = vdupq_n_u8(1);
  7393. uint8x16x2_t vs;
  7394. ggml_int8x16x4_t q3s;
  7395. ggml_int8x16x4_t q8b;
  7396. vec_index_t idx;
  7397. #if QK_K == 256
  7398. uint32_t scales32[2];
  7399. const uint8_t * scales8 = (const uint8_t *)scales32;
  7400. #endif
  7401. float sumf = 0;
  7402. for (int i = 0; i < nb; ++i) {
  7403. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7404. const uint8_t * restrict qs = x[i].qs;
  7405. const uint8_t * restrict qh = x[i].qh;
  7406. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7407. const int8_t * restrict q8 = y[i].qs;
  7408. #if QK_K == 256
  7409. memcpy(scales32, x[i].scales, 4);
  7410. scales32[1] = (((scales32[0] >> 4) & 0x0f0f0f0f) << 1) | 0x01010101;
  7411. scales32[0] = ((scales32[0] & 0x0f0f0f0f) << 1) | 0x01010101;
  7412. #endif
  7413. int sumi1 = 0, sumi2 = 0;
  7414. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7415. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7416. const uint8x16_t idx_l = vld1q_u8(qs); qs += 16;
  7417. idx.vec_index = vorrq_u16(vmovl_u8(vget_low_u8 (idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+0]), hshift), m256));
  7418. const uint32x4_t aux32x4_0 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7419. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7420. const uint32x4_t aux32x4_1 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7421. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7422. idx.vec_index = vorrq_u16(vmovl_u8(vget_high_u8(idx_l)), vandq_u16(vshlq_u16(vdupq_n_u16(qh[ib32+1]), hshift), m256));
  7423. const uint32x4_t aux32x4_2 = ggml_vld1q_u32(iq3s_grid[idx.index[0]], iq3s_grid[idx.index[1]],
  7424. iq3s_grid[idx.index[2]], iq3s_grid[idx.index[3]]);
  7425. const uint32x4_t aux32x4_3 = ggml_vld1q_u32(iq3s_grid[idx.index[4]], iq3s_grid[idx.index[5]],
  7426. iq3s_grid[idx.index[6]], iq3s_grid[idx.index[7]]);
  7427. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[0] | (signs[1] << 16)));
  7428. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7429. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7430. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7431. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7432. q3s.val[0] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_0));
  7433. q3s.val[1] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_1));
  7434. vs.val[0] = vreinterpretq_u8_u32(vdupq_n_u32(signs[2] | (signs[3] << 16)));
  7435. vs.val[1] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[1]), mask2);
  7436. vs.val[0] = vandq_u8(ggml_vqtbl1q_u8(vs.val[0], mask1.val[0]), mask2);
  7437. vs.val[0] = vorrq_u8(vceqq_u8(vs.val[0], mask2), m1);
  7438. vs.val[1] = vorrq_u8(vceqq_u8(vs.val[1], mask2), m1);
  7439. signs += 4;
  7440. q3s.val[2] = vmulq_s8(vreinterpretq_s8_u8(vs.val[0]), vreinterpretq_s8_u32(aux32x4_2));
  7441. q3s.val[3] = vmulq_s8(vreinterpretq_s8_u8(vs.val[1]), vreinterpretq_s8_u32(aux32x4_3));
  7442. 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]);
  7443. 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]);
  7444. #if QK_K == 256
  7445. sumi1 += vaddvq_s32(p1) * scales8[ib32/2+0];
  7446. sumi2 += vaddvq_s32(p2) * scales8[ib32/2+4];
  7447. #else
  7448. sumi1 += vaddvq_s32(p1) * (1 + 2*(x[i].scales[ib32/2] & 0xf));
  7449. sumi2 += vaddvq_s32(p2) * (1 + 2*(x[i].scales[ib32/2] >> 4));
  7450. #endif
  7451. }
  7452. sumf += d*(sumi1 + sumi2);
  7453. }
  7454. *s = sumf;
  7455. #elif defined(__AVX2__)
  7456. static const uint8_t k_mask1[32] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  7457. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03
  7458. };
  7459. static const uint8_t k_mask2[32] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7460. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
  7461. };
  7462. const __m256i mask1 = _mm256_loadu_si256((const __m256i*)k_mask1);
  7463. const __m256i mask2 = _mm256_loadu_si256((const __m256i*)k_mask2);
  7464. const __m256i idx_shift = _mm256_set_epi32(1, 2, 3, 4, 5, 6, 7, 8);
  7465. const __m256i idx_mask = _mm256_set1_epi32(256);
  7466. typedef union {
  7467. __m256i vec[2];
  7468. uint32_t index[16];
  7469. } index_t;
  7470. index_t idx;
  7471. __m256 accumf = _mm256_setzero_ps();
  7472. for (int i = 0; i < nb; ++i) {
  7473. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7474. const uint8_t * restrict qs = x[i].qs;
  7475. const uint8_t * restrict qh = x[i].qh;
  7476. const uint16_t * restrict signs = (const uint16_t *)x[i].signs;
  7477. const int8_t * restrict q8 = y[i].qs;
  7478. __m256i sumi1 = _mm256_setzero_si256();
  7479. __m256i sumi2 = _mm256_setzero_si256();
  7480. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7481. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7482. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7483. const __m256i idx_l = _mm256_cvtepu8_epi16(_mm_loadu_si128((const __m128i *)qs)); qs += 16;
  7484. idx.vec[0] = _mm256_set1_epi32(qh[ib32+0]);
  7485. idx.vec[1] = _mm256_set1_epi32(qh[ib32+1]);
  7486. idx.vec[0] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[0], idx_shift), idx_mask);
  7487. idx.vec[1] = _mm256_and_si256(_mm256_sllv_epi32(idx.vec[1], idx_shift), idx_mask);
  7488. idx.vec[0] = _mm256_or_si256(idx.vec[0], _mm256_cvtepi16_epi32(_mm256_castsi256_si128(idx_l)));
  7489. idx.vec[1] = _mm256_or_si256(idx.vec[1], _mm256_cvtepi16_epi32(_mm256_extractf128_si256(idx_l, 1)));
  7490. // At leat on my CPU (Ryzen 7950X), using _mm256_i32gather_epi32 is slower than _mm256_set_epi32. Strange.
  7491. //const __m256i q2_1 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[0], 4);
  7492. //const __m256i q2_2 = _mm256_i32gather_epi32((const int *)iq3s_grid, idx.vec[1], 4);
  7493. const __m256i q2_1 = _mm256_set_epi32(
  7494. iq3s_grid[idx.index[7]], iq3s_grid[idx.index[6]], iq3s_grid[idx.index[5]], iq3s_grid[idx.index[4]],
  7495. iq3s_grid[idx.index[3]], iq3s_grid[idx.index[2]], iq3s_grid[idx.index[1]], iq3s_grid[idx.index[0]]
  7496. );
  7497. const __m256i q2_2 = _mm256_set_epi32(
  7498. iq3s_grid[idx.index[15]], iq3s_grid[idx.index[14]], iq3s_grid[idx.index[13]], iq3s_grid[idx.index[12]],
  7499. iq3s_grid[idx.index[11]], iq3s_grid[idx.index[10]], iq3s_grid[idx.index[ 9]], iq3s_grid[idx.index[ 8]]
  7500. );
  7501. __m256i aux256 = _mm256_set1_epi32(signs[0] | (signs[1] << 16));
  7502. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7503. const __m256i s2_1 = _mm256_cmpeq_epi8(aux256, mask2);
  7504. const __m256i q8s_1 = _mm256_sub_epi8(_mm256_xor_si256(s2_1, q8_1), s2_1);
  7505. aux256 = _mm256_set1_epi32(signs[2] | (signs[3] << 16));
  7506. aux256 = _mm256_and_si256(_mm256_shuffle_epi8(aux256,mask1), mask2);
  7507. const __m256i s2_2 = _mm256_cmpeq_epi8(aux256, mask2);
  7508. const __m256i q8s_2 = _mm256_sub_epi8(_mm256_xor_si256(s2_2, q8_2), s2_2);
  7509. signs += 4;
  7510. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  7511. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  7512. const uint16_t ls1 = x[i].scales[ib32/2] & 0xf;
  7513. const uint16_t ls2 = x[i].scales[ib32/2] >> 4;
  7514. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  7515. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  7516. sumi1 = _mm256_add_epi32(sumi1, p1);
  7517. sumi2 = _mm256_add_epi32(sumi2, p2);
  7518. }
  7519. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  7520. }
  7521. *s = hsum_float_8(accumf);
  7522. #else
  7523. float sumf = 0.f;
  7524. for (int i = 0; i < nb; ++i) {
  7525. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  7526. const uint8_t * restrict qs = x[i].qs;
  7527. const uint8_t * restrict qh = x[i].qh;
  7528. const uint8_t * restrict signs = x[i].signs;
  7529. const int8_t * restrict q8 = y[i].qs;
  7530. int32_t bsum = 0;
  7531. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  7532. const uint32_t ls1 = 2*(x[i].scales[ib32/2] & 0xf) + 1;
  7533. const uint32_t ls2 = 2*(x[i].scales[ib32/2] >> 4) + 1;
  7534. int32_t sumi = 0;
  7535. for (int l = 0; l < 4; ++l) {
  7536. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+0] << (8-2*l)) & 256)));
  7537. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+0] << (7-2*l)) & 256)));
  7538. for (int j = 0; j < 4; ++j) {
  7539. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7540. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7541. }
  7542. q8 += 8;
  7543. }
  7544. qs += 8;
  7545. signs += 4;
  7546. bsum += sumi * ls1;
  7547. sumi = 0;
  7548. for (int l = 0; l < 4; ++l) {
  7549. const uint8_t * grid1 = (const uint8_t *)(iq3s_grid + (qs[2*l+0] | ((qh[ib32+1] << (8-2*l)) & 256)));
  7550. const uint8_t * grid2 = (const uint8_t *)(iq3s_grid + (qs[2*l+1] | ((qh[ib32+1] << (7-2*l)) & 256)));
  7551. for (int j = 0; j < 4; ++j) {
  7552. sumi += grid1[j] * q8[j+0] * (signs[l] & kmask_iq2xs[j+0] ? -1 : 1);
  7553. sumi += grid2[j] * q8[j+4] * (signs[l] & kmask_iq2xs[j+4] ? -1 : 1);
  7554. }
  7555. q8 += 8;
  7556. }
  7557. qs += 8;
  7558. signs += 4;
  7559. bsum += sumi * ls2;
  7560. }
  7561. sumf += d * bsum;
  7562. }
  7563. *s = sumf;
  7564. #endif
  7565. }
  7566. #ifdef __AVX2__
  7567. static inline __m256i mul_add_epi8(const __m256i x, const __m256i y) {
  7568. const __m256i ax = _mm256_sign_epi8(x, x);
  7569. const __m256i sy = _mm256_sign_epi8(y, x);
  7570. return _mm256_maddubs_epi16(ax, sy);
  7571. }
  7572. #endif
  7573. 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) {
  7574. assert(n % QK_K == 0);
  7575. assert(nrc == 1);
  7576. UNUSED(nrc);
  7577. UNUSED(bx);
  7578. UNUSED(by);
  7579. UNUSED(bs);
  7580. const block_iq1_s * restrict x = vx;
  7581. const block_q8_K * restrict y = vy;
  7582. const int nb = n / QK_K;
  7583. #if defined __ARM_NEON
  7584. ggml_int8x16x4_t q1b;
  7585. ggml_int8x16x4_t q8b;
  7586. float sumf = 0;
  7587. for (int i = 0; i < nb; ++i) {
  7588. const int8_t * q8 = y[i].qs;
  7589. const uint8_t * qs = x[i].qs;
  7590. const uint16_t * qh = x[i].qh;
  7591. int sumi1 = 0, sumi2 = 0, sumi3 = 0;
  7592. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7593. q1b.val[0] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[0] | ((qh[ib+0] << 8) & 0x700)))),
  7594. vld1_s8((const int8_t *)(iq1s_grid + (qs[1] | ((qh[ib+0] << 5) & 0x700)))));
  7595. q1b.val[1] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[2] | ((qh[ib+0] << 2) & 0x700)))),
  7596. vld1_s8((const int8_t *)(iq1s_grid + (qs[3] | ((qh[ib+0] >> 1) & 0x700)))));
  7597. q1b.val[2] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[4] | ((qh[ib+1] << 8) & 0x700)))),
  7598. vld1_s8((const int8_t *)(iq1s_grid + (qs[5] | ((qh[ib+1] << 5) & 0x700)))));
  7599. q1b.val[3] = vcombine_s8(vld1_s8((const int8_t *)(iq1s_grid + (qs[6] | ((qh[ib+1] << 2) & 0x700)))),
  7600. vld1_s8((const int8_t *)(iq1s_grid + (qs[7] | ((qh[ib+1] >> 1) & 0x700)))));
  7601. qs += 8;
  7602. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7603. 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]);
  7604. 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]);
  7605. const int ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7606. const int ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7607. sumi1 += vaddvq_s32(p1) * ls1;
  7608. sumi2 += vaddvq_s32(p2) * ls2;
  7609. sumi3 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * ls1 * (qh[ib+0] & 0x8000 ? -1 : 1)
  7610. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * ls2 * (qh[ib+1] & 0x8000 ? -1 : 1);
  7611. }
  7612. sumf += y[i].d * GGML_FP16_TO_FP32(x[i].d) * (sumi1 + sumi2 + IQ1S_DELTA * sumi3);
  7613. }
  7614. *s = sumf;
  7615. #elif defined __AVX2__
  7616. __m256 accum = _mm256_setzero_ps();
  7617. float accum1 = 0;
  7618. for (int i = 0; i < nb; ++i) {
  7619. const int8_t * q8 = y[i].qs;
  7620. const uint8_t * qs = x[i].qs;
  7621. const uint16_t * qh = x[i].qh;
  7622. __m256i sumi = _mm256_setzero_si256();
  7623. int sumi1 = 0;
  7624. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7625. 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)],
  7626. iq1s_grid[qs[1] | ((qh[ib+0] << 5) & 0x700)], iq1s_grid[qs[0] | ((qh[ib+0] << 8) & 0x700)]);
  7627. 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)],
  7628. iq1s_grid[qs[5] | ((qh[ib+1] << 5) & 0x700)], iq1s_grid[qs[4] | ((qh[ib+1] << 8) & 0x700)]);
  7629. qs += 8;
  7630. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7631. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  7632. const __m256i dot1 = mul_add_epi8(q1b_1, q8b_1);
  7633. const __m256i dot2 = mul_add_epi8(q1b_2, q8b_2);
  7634. const int16_t ls1 = 2*((qh[ib+0] >> 12) & 7) + 1;
  7635. const int16_t ls2 = 2*((qh[ib+1] >> 12) & 7) + 1;
  7636. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(ls1));
  7637. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(ls2));
  7638. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p1, p2));
  7639. sumi1 += (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]) * (qh[ib+0] & 0x8000 ? -1 : 1) * ls1
  7640. + (y[i].bsums[2*ib+2] + y[i].bsums[2*ib+3]) * (qh[ib+1] & 0x8000 ? -1 : 1) * ls2;
  7641. }
  7642. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  7643. accum = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(sumi), accum);
  7644. accum1 += d * sumi1;
  7645. }
  7646. *s = hsum_float_8(accum) + IQ1S_DELTA * accum1;
  7647. #else
  7648. float sumf = 0;
  7649. for (int i = 0; i < nb; i++) {
  7650. const int8_t * q8 = y[i].qs;
  7651. const uint8_t * qs = x[i].qs;
  7652. const uint16_t * qh = x[i].qh;
  7653. int sumi = 0, sumi1 = 0;
  7654. for (int ib = 0; ib < QK_K/32; ++ib) {
  7655. const int ls = 2*((qh[ib] >> 12) & 7) + 1;
  7656. const int delta = qh[ib] & 0x8000 ? -1 : 1;
  7657. int lsum = 0;
  7658. for (int l = 0; l < 4; ++l) {
  7659. const int8_t * grid = (const int8_t *)(iq1s_grid + (qs[l] | (((qh[ib] >> 3*l) & 7) << 8)));
  7660. for (int j = 0; j < 8; ++j) {
  7661. lsum += q8[j] * grid[j];
  7662. }
  7663. q8 += 8;
  7664. }
  7665. sumi += ls * lsum;
  7666. sumi1 += ls * delta * (y[i].bsums[2*ib+0] + y[i].bsums[2*ib+1]);
  7667. qs += 4;
  7668. }
  7669. sumf += GGML_FP16_TO_FP32(x[i].d) * y[i].d * (sumi + IQ1S_DELTA * sumi1);
  7670. }
  7671. *s = sumf;
  7672. #endif
  7673. }
  7674. 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) {
  7675. assert(nrc == 1);
  7676. UNUSED(nrc);
  7677. UNUSED(bx);
  7678. UNUSED(by);
  7679. UNUSED(bs);
  7680. assert(n % QK4_NL == 0);
  7681. static_assert(QK4_NL == QK8_0, "QK4_NL and QK8_0 must be the same");
  7682. const block_iq4_nl * restrict x = vx;
  7683. const block_q8_0 * restrict y = vy;
  7684. const int nb = n / QK4_NL;
  7685. #if defined __ARM_NEON
  7686. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7687. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7688. uint8x16x2_t q4bits;
  7689. int8x16x4_t q4b;
  7690. int8x16x4_t q8b;
  7691. int32x4_t prod_1, prod_2;
  7692. float sumf = 0;
  7693. for (int ib = 0; ib < nb; ib += 2) {
  7694. q4bits.val[0] = vld1q_u8(x[ib+0].qs);
  7695. q4bits.val[1] = vld1q_u8(x[ib+1].qs);
  7696. q8b.val[0] = vld1q_s8(y[ib+0].qs);
  7697. q8b.val[1] = vld1q_s8(y[ib+0].qs + 16);
  7698. q8b.val[2] = vld1q_s8(y[ib+1].qs);
  7699. q8b.val[3] = vld1q_s8(y[ib+1].qs + 16);
  7700. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7701. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7702. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7703. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7704. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7705. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7706. sumf +=
  7707. GGML_FP16_TO_FP32(x[ib+0].d) * GGML_FP16_TO_FP32(y[ib+0].d) * vaddvq_s32(prod_1) +
  7708. GGML_FP16_TO_FP32(x[ib+1].d) * GGML_FP16_TO_FP32(y[ib+1].d) * vaddvq_s32(prod_2);
  7709. }
  7710. *s = sumf;
  7711. #elif defined __AVX2__
  7712. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7713. const __m128i m4b = _mm_set1_epi8(0x0f);
  7714. const __m256i mone = _mm256_set1_epi16(1);
  7715. __m256 accum1 = _mm256_setzero_ps();
  7716. __m256 accum2 = _mm256_setzero_ps();
  7717. for (int ib = 0; ib < nb; ib += 2) {
  7718. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)x[0].qs);
  7719. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)x[1].qs);
  7720. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)y[0].qs);
  7721. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)y[1].qs);
  7722. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7723. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7724. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7725. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7726. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7727. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7728. const __m256i p_1 = _mm256_madd_epi16(p16_1, mone);
  7729. const __m256i p_2 = _mm256_madd_epi16(p16_2, mone);
  7730. accum1 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[0].d)*GGML_FP16_TO_FP32(x[0].d)),
  7731. _mm256_cvtepi32_ps(p_1), accum1);
  7732. accum2 = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(y[1].d)*GGML_FP16_TO_FP32(x[1].d)),
  7733. _mm256_cvtepi32_ps(p_2), accum2);
  7734. y += 2;
  7735. x += 2;
  7736. }
  7737. *s = hsum_float_8(_mm256_add_ps(accum1, accum2));
  7738. #else
  7739. float sumf = 0;
  7740. for (int ib = 0; ib < nb; ++ib) {
  7741. const float d = GGML_FP16_TO_FP32(y[ib].d)*GGML_FP16_TO_FP32(x[ib].d);
  7742. int sumi1 = 0, sumi2 = 0;
  7743. for (int j = 0; j < QK4_NL/2; ++j) {
  7744. sumi1 += y[ib].qs[j+ 0] * kvalues_iq4nl[x[ib].qs[j] & 0xf];
  7745. sumi2 += y[ib].qs[j+QK4_NL/2] * kvalues_iq4nl[x[ib].qs[j] >> 4];
  7746. }
  7747. sumf += d * (sumi1 + sumi2);
  7748. }
  7749. *s = sumf;
  7750. #endif
  7751. }
  7752. 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) {
  7753. assert(nrc == 1);
  7754. UNUSED(nrc);
  7755. UNUSED(bx);
  7756. UNUSED(by);
  7757. UNUSED(bs);
  7758. assert(n % QK_K == 0);
  7759. #if QK_K == 64
  7760. ggml_vec_dot_iq4_nl_q8_0(n, s, bs, vx, bx, vy, by, nrc);
  7761. #else
  7762. const block_iq4_xs * restrict x = vx;
  7763. const block_q8_K * restrict y = vy;
  7764. const int nb = n / QK_K;
  7765. #if defined __ARM_NEON
  7766. const int8x16_t values = vld1q_s8(kvalues_iq4nl);
  7767. const uint8x16_t m4b = vdupq_n_u8(0x0f);
  7768. ggml_uint8x16x2_t q4bits;
  7769. ggml_int8x16x4_t q4b;
  7770. ggml_int8x16x4_t q8b;
  7771. int32x4_t prod_1, prod_2;
  7772. float sumf = 0;
  7773. for (int ibl = 0; ibl < nb; ++ibl) {
  7774. const int8_t * q8 = y[ibl].qs;
  7775. const uint8_t * q4 = x[ibl].qs;
  7776. uint16_t h = x[ibl].scales_h;
  7777. int sumi1 = 0, sumi2 = 0;
  7778. for (int ib = 0; ib < QK_K/64; ++ib) {
  7779. q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  7780. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  7781. q4b.val[0] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[0], m4b));
  7782. q4b.val[1] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[0], 4));
  7783. q4b.val[2] = ggml_vqtbl1q_s8(values, vandq_u8 (q4bits.val[1], m4b));
  7784. q4b.val[3] = ggml_vqtbl1q_s8(values, vshrq_n_u8(q4bits.val[1], 4));
  7785. prod_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[0], q8b.val[0]), q4b.val[1], q8b.val[1]);
  7786. prod_2 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), q4b.val[2], q8b.val[2]), q4b.val[3], q8b.val[3]);
  7787. int ls1 = ((x[ibl].scales_l[ib] & 0xf) | ((h << 4) & 0x30)) - 32;
  7788. int ls2 = ((x[ibl].scales_l[ib] >> 4) | ((h << 2) & 0x30)) - 32;
  7789. h >>= 4;
  7790. sumi1 += vaddvq_s32(prod_1) * ls1;
  7791. sumi2 += vaddvq_s32(prod_2) * ls2;
  7792. }
  7793. sumf += GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d * (sumi1 + sumi2);
  7794. }
  7795. *s = sumf;
  7796. #elif defined __AVX2__
  7797. const __m128i values128 = _mm_loadu_si128((const __m128i*)kvalues_iq4nl);
  7798. const __m128i m4b = _mm_set1_epi8(0x0f);
  7799. __m256 accum = _mm256_setzero_ps();
  7800. for (int ibl = 0; ibl < nb; ++ibl) {
  7801. const uint8_t * qs = x[ibl].qs;
  7802. const int8_t * q8 = y[ibl].qs;
  7803. uint16_t sh = x[ibl].scales_h;
  7804. __m256i sumi1 = _mm256_setzero_si256();
  7805. __m256i sumi2 = _mm256_setzero_si256();
  7806. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7807. const __m128i q4bits_1 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7808. const __m128i q4bits_2 = _mm_loadu_si128((const __m128i*)qs); qs += 16;
  7809. const __m256i q8b_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7810. const __m256i q8b_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  7811. const __m256i q4b_1 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4b)),
  7812. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_1, m4b)));
  7813. const __m256i q4b_2 = MM256_SET_M128I(_mm_shuffle_epi8(values128, _mm_and_si128(_mm_srli_epi16(q4bits_2, 4), m4b)),
  7814. _mm_shuffle_epi8(values128, _mm_and_si128(q4bits_2, m4b)));
  7815. const __m256i p16_1 = mul_add_epi8(q4b_1, q8b_1);
  7816. const __m256i p16_2 = mul_add_epi8(q4b_2, q8b_2);
  7817. const int16_t ls1 = ((x[ibl].scales_l[ib/2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  7818. const int16_t ls2 = ((x[ibl].scales_l[ib/2] >> 4) | ((sh << 2) & 0x30)) - 32;
  7819. sh >>= 4;
  7820. const __m256i p_1 = _mm256_madd_epi16(p16_1, _mm256_set1_epi16(ls1));
  7821. const __m256i p_2 = _mm256_madd_epi16(p16_2, _mm256_set1_epi16(ls2));
  7822. sumi1 = _mm256_add_epi32(p_1, sumi1);
  7823. sumi2 = _mm256_add_epi32(p_2, sumi2);
  7824. }
  7825. accum = _mm256_fmadd_ps(_mm256_set1_ps(GGML_FP16_TO_FP32(x[ibl].d)*y[ibl].d),
  7826. _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accum);
  7827. }
  7828. *s = hsum_float_8(accum);
  7829. #else
  7830. float sumf = 0;
  7831. for (int ibl = 0; ibl < nb; ++ibl) {
  7832. const float d4d8 = GGML_FP16_TO_FP32(x[ibl].d) * y[ibl].d;
  7833. uint16_t h = x[ibl].scales_h;
  7834. const uint8_t * qs = x[ibl].qs;
  7835. const int8_t * q8 = y[ibl].qs;
  7836. for (int ib = 0; ib < QK_K/32; ib += 2) {
  7837. const uint8_t ls1 = (x[ibl].scales_l[ib/2] & 0xf) | ((h << 4) & 0x30);
  7838. const uint8_t ls2 = (x[ibl].scales_l[ib/2] >> 4) | ((h << 2) & 0x30);
  7839. h >>= 4;
  7840. const float d1 = d4d8*(ls1 - 32);
  7841. const float d2 = d4d8*(ls2 - 32);
  7842. int sumi1 = 0, sumi2 = 0;
  7843. for (int j = 0; j < 16; ++j) {
  7844. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7845. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7846. }
  7847. sumf += d1 * (sumi1 + sumi2);
  7848. qs += 16;
  7849. q8 += 32;
  7850. sumi1 = sumi2 = 0;
  7851. for (int j = 0; j < 16; ++j) {
  7852. sumi1 += q8[j+ 0] * kvalues_iq4nl[qs[j] & 0xf];
  7853. sumi2 += q8[j+16] * kvalues_iq4nl[qs[j] >> 4];
  7854. }
  7855. sumf += d2 * (sumi1 + sumi2);
  7856. qs += 16;
  7857. q8 += 32;
  7858. }
  7859. }
  7860. *s = sumf;
  7861. #endif
  7862. #endif
  7863. }
  7864. // ================================ IQ2 quantization =============================================
  7865. typedef struct {
  7866. uint64_t * grid;
  7867. int * map;
  7868. uint16_t * neighbours;
  7869. } iq2_entry_t;
  7870. static iq2_entry_t iq2_data[4] = {
  7871. {NULL, NULL, NULL},
  7872. {NULL, NULL, NULL},
  7873. {NULL, NULL, NULL},
  7874. {NULL, NULL, NULL},
  7875. };
  7876. static inline int iq2_data_index(enum ggml_type type) {
  7877. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ2_S);
  7878. return type == GGML_TYPE_IQ2_XXS ? 0 :
  7879. type == GGML_TYPE_IQ2_XS ? 1 :
  7880. type == GGML_TYPE_IQ1_S ? 2 : 3;
  7881. }
  7882. static inline int iq2_grid_size(enum ggml_type type) {
  7883. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ2_S);
  7884. return type == GGML_TYPE_IQ2_XXS ? 256 :
  7885. type == GGML_TYPE_IQ2_XS ? 512 :
  7886. type == GGML_TYPE_IQ1_S ? NGRID_IQ1S : 1024;
  7887. }
  7888. static int iq2_compare_func(const void * left, const void * right) {
  7889. const int * l = (const int *)left;
  7890. const int * r = (const int *)right;
  7891. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  7892. }
  7893. void iq2xs_init_impl(enum ggml_type type) {
  7894. const int gindex = iq2_data_index(type);
  7895. const int grid_size = iq2_grid_size(type);
  7896. if (iq2_data[gindex].grid) {
  7897. return;
  7898. }
  7899. static const uint16_t kgrid_2bit_256[256] = {
  7900. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  7901. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  7902. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  7903. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  7904. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  7905. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  7906. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  7907. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  7908. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  7909. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  7910. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  7911. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  7912. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  7913. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  7914. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  7915. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  7916. };
  7917. static const uint16_t kgrid_2bit_512[512] = {
  7918. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  7919. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  7920. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  7921. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  7922. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  7923. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  7924. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  7925. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  7926. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  7927. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  7928. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  7929. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  7930. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  7931. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  7932. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  7933. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  7934. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  7935. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  7936. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  7937. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  7938. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  7939. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  7940. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  7941. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  7942. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  7943. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  7944. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  7945. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  7946. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  7947. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  7948. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  7949. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  7950. };
  7951. static const uint16_t kgrid_1bit_2048[NGRID_IQ1S] = {
  7952. 0, 2, 5, 8, 10, 17, 21, 32, 34, 40, 42, 69, 81, 84, 86, 101,
  7953. 128, 130, 136, 138, 149, 160, 162, 168, 170, 260, 261, 273, 276, 278, 281, 282,
  7954. 293, 321, 326, 329, 338, 341, 346, 353, 356, 358, 360, 389, 401, 404, 406, 421,
  7955. 512, 514, 520, 522, 533, 544, 546, 552, 554, 581, 593, 601, 612, 617, 640, 642,
  7956. 648, 650, 657, 661, 665, 672, 674, 680, 682, 1041, 1044, 1046, 1061, 1089, 1097, 1109,
  7957. 1114, 1124, 1125, 1169, 1177, 1189, 1281, 1284, 1285, 1286, 1301, 1304, 1306, 1321, 1344, 1349,
  7958. 1354, 1360, 1361, 1364, 1365, 1366, 1369, 1376, 1378, 1381, 1384, 1386, 1409, 1425, 1429, 1432,
  7959. 1434, 1441, 1444, 1445, 1446, 1449, 1556, 1561, 1601, 1604, 1616, 1618, 1621, 1624, 1632, 1633,
  7960. 1638, 1641, 1669, 1681, 1684, 1689, 2048, 2050, 2056, 2058, 2069, 2080, 2082, 2088, 2090, 2117,
  7961. 2129, 2134, 2149, 2176, 2178, 2184, 2186, 2197, 2208, 2210, 2216, 2218, 2309, 2321, 2324, 2329,
  7962. 2340, 2341, 2369, 2384, 2385, 2389, 2401, 2404, 2409, 2449, 2452, 2454, 2457, 2469, 2560, 2562,
  7963. 2568, 2570, 2581, 2592, 2594, 2600, 2602, 2629, 2641, 2649, 2657, 2661, 2688, 2690, 2693, 2696,
  7964. 2698, 2709, 2720, 2722, 2728, 2730, 4112, 4113, 4116, 4121, 4132, 4133, 4161, 4164, 4176, 4181,
  7965. 4184, 4193, 4196, 4197, 4201, 4241, 4244, 4246, 4257, 4261, 4353, 4356, 4358, 4361, 4368, 4370,
  7966. 4373, 4376, 4385, 4388, 4393, 4421, 4426, 4432, 4433, 4434, 4436, 4437, 4438, 4441, 4448, 4453,
  7967. 4484, 4498, 4501, 4513, 4516, 4625, 4628, 4630, 4645, 4672, 4678, 4681, 4690, 4693, 4696, 4698,
  7968. 4708, 4710, 4741, 4753, 4756, 4758, 4773, 5121, 5126, 5129, 5140, 5141, 5144, 5145, 5153, 5158,
  7969. 5185, 5189, 5190, 5192, 5194, 5201, 5204, 5205, 5206, 5209, 5218, 5221, 5224, 5252, 5257, 5264,
  7970. 5268, 5269, 5272, 5273, 5274, 5281, 5284, 5285, 5289, 5378, 5381, 5386, 5393, 5396, 5397, 5398,
  7971. 5401, 5408, 5410, 5413, 5416, 5418, 5441, 5444, 5445, 5446, 5457, 5458, 5460, 5461, 5462, 5465,
  7972. 5466, 5473, 5476, 5477, 5478, 5481, 5504, 5506, 5508, 5509, 5512, 5514, 5520, 5521, 5524, 5525,
  7973. 5526, 5529, 5530, 5536, 5538, 5541, 5633, 5636, 5637, 5638, 5653, 5654, 5656, 5658, 5665, 5670,
  7974. 5696, 5698, 5700, 5701, 5704, 5706, 5713, 5717, 5718, 5720, 5721, 5729, 5732, 5733, 5736, 5737,
  7975. 5738, 5766, 5770, 5778, 5781, 5796, 5801, 6161, 6166, 6181, 6209, 6212, 6214, 6217, 6224, 6229,
  7976. 6232, 6234, 6240, 6241, 6244, 6246, 6249, 6277, 6289, 6292, 6309, 6416, 6418, 6421, 6426, 6433,
  7977. 6437, 6466, 6468, 6469, 6472, 6481, 6484, 6485, 6486, 6489, 6490, 6496, 6501, 6506, 6537, 6545,
  7978. 6546, 6549, 6552, 6561, 6566, 6569, 6665, 6678, 6692, 6694, 6724, 6726, 6729, 6736, 6738, 6741,
  7979. 6744, 6753, 6758, 6761, 6789, 6801, 6806, 6810, 8192, 8194, 8200, 8202, 8213, 8224, 8226, 8229,
  7980. 8232, 8234, 8261, 8273, 8281, 8289, 8293, 8320, 8322, 8328, 8330, 8341, 8352, 8354, 8357, 8360,
  7981. 8362, 8453, 8465, 8468, 8473, 8485, 8514, 8516, 8521, 8533, 8536, 8538, 8545, 8548, 8549, 8550,
  7982. 8581, 8592, 8598, 8601, 8613, 8705, 8712, 8714, 8721, 8725, 8736, 8738, 8744, 8746, 8773, 8785,
  7983. 8790, 8793, 8805, 8833, 8840, 8842, 8849, 8853, 8864, 8866, 8872, 8874, 9221, 9236, 9238, 9241,
  7984. 9253, 9284, 9285, 9286, 9289, 9298, 9301, 9304, 9306, 9318, 9349, 9361, 9364, 9369, 9377, 9381,
  7985. 9481, 9493, 9505, 9513, 9536, 9541, 9544, 9553, 9556, 9557, 9561, 9570, 9573, 9576, 9609, 9616,
  7986. 9620, 9621, 9624, 9626, 9633, 9636, 9638, 9641, 9733, 9744, 9746, 9753, 9765, 9793, 9801, 9813,
  7987. 9824, 9825, 9833, 9860, 9862, 9872, 9882, 10240, 10242, 10248, 10250, 10261, 10272, 10274, 10280, 10282,
  7988. 10309, 10321, 10324, 10341, 10368, 10370, 10376, 10378, 10400, 10402, 10408, 10410, 10505, 10513, 10516, 10521,
  7989. 10533, 10566, 10569, 10578, 10581, 10593, 10596, 10598, 10601, 10629, 10640, 10646, 10649, 10660, 10661, 10752,
  7990. 10754, 10760, 10762, 10784, 10786, 10792, 10794, 10821, 10833, 10838, 10841, 10853, 10880, 10882, 10888, 10890,
  7991. 10901, 10912, 10914, 10920, 10922, 16389, 16401, 16406, 16421, 16457, 16466, 16469, 16472, 16474, 16481, 16484,
  7992. 16486, 16532, 16537, 16545, 16550, 16640, 16641, 16644, 16646, 16649, 16658, 16661, 16662, 16664, 16666, 16673,
  7993. 16678, 16681, 16709, 16712, 16714, 16721, 16724, 16725, 16726, 16729, 16730, 16741, 16744, 16746, 16769, 16772,
  7994. 16774, 16784, 16786, 16789, 16800, 16801, 16802, 16901, 16913, 16916, 16918, 16933, 16961, 16978, 16981, 16986,
  7995. 16996, 17001, 17033, 17044, 17061, 17409, 17429, 17433, 17449, 17477, 17480, 17482, 17489, 17492, 17493, 17494,
  7996. 17505, 17506, 17509, 17512, 17514, 17537, 17542, 17545, 17552, 17554, 17557, 17568, 17569, 17577, 17665, 17666,
  7997. 17669, 17674, 17681, 17684, 17685, 17686, 17689, 17696, 17701, 17706, 17729, 17732, 17733, 17734, 17737, 17744,
  7998. 17745, 17748, 17749, 17750, 17752, 17753, 17761, 17764, 17765, 17766, 17769, 17794, 17796, 17797, 17800, 17809,
  7999. 17812, 17813, 17814, 17817, 17818, 17829, 17832, 17834, 17921, 17925, 17929, 17940, 17941, 17944, 17946, 17953,
  8000. 17956, 17961, 17984, 17986, 17989, 17992, 18000, 18001, 18002, 18005, 18006, 18009, 18018, 18021, 18024, 18049,
  8001. 18053, 18058, 18068, 18069, 18081, 18084, 18086, 18437, 18449, 18453, 18458, 18469, 18498, 18505, 18512, 18517,
  8002. 18520, 18529, 18532, 18534, 18537, 18565, 18577, 18580, 18582, 18585, 18597, 18689, 18693, 18694, 18698, 18704,
  8003. 18708, 18709, 18712, 18721, 18724, 18726, 18752, 18757, 18762, 18769, 18770, 18772, 18773, 18774, 18777, 18784,
  8004. 18786, 18789, 18790, 18794, 18822, 18825, 18834, 18837, 18838, 18840, 18849, 18852, 18854, 18857, 18966, 19012,
  8005. 19014, 19017, 19029, 19032, 19034, 19044, 19049, 19092, 19109, 20481, 20484, 20485, 20486, 20489, 20498, 20501,
  8006. 20506, 20513, 20516, 20521, 20544, 20549, 20552, 20561, 20564, 20565, 20566, 20569, 20581, 20584, 20614, 20617,
  8007. 20629, 20632, 20640, 20641, 20646, 20649, 20741, 20744, 20745, 20746, 20753, 20756, 20757, 20758, 20760, 20761,
  8008. 20768, 20773, 20774, 20776, 20778, 20801, 20804, 20805, 20806, 20809, 20816, 20817, 20818, 20820, 20821, 20822,
  8009. 20824, 20825, 20826, 20833, 20836, 20837, 20838, 20841, 20866, 20869, 20881, 20884, 20885, 20886, 20889, 20896,
  8010. 20901, 20906, 20993, 20998, 21010, 21013, 21018, 21025, 21028, 21058, 21061, 21066, 21073, 21076, 21077, 21078,
  8011. 21081, 21090, 21093, 21125, 21136, 21138, 21141, 21145, 21146, 21156, 21508, 21509, 21521, 21524, 21525, 21526,
  8012. 21528, 21529, 21537, 21541, 21544, 21546, 21569, 21572, 21573, 21574, 21577, 21578, 21584, 21585, 21588, 21589,
  8013. 21590, 21592, 21593, 21594, 21601, 21602, 21604, 21605, 21606, 21609, 21632, 21640, 21642, 21649, 21652, 21653,
  8014. 21654, 21657, 21665, 21668, 21669, 21674, 21761, 21762, 21764, 21765, 21766, 21769, 21776, 21777, 21778, 21780,
  8015. 21781, 21782, 21785, 21786, 21793, 21796, 21797, 21798, 21801, 21824, 21825, 21826, 21828, 21829, 21830, 21832,
  8016. 21833, 21840, 21841, 21842, 21844, 21845, 21846, 21848, 21849, 21850, 21856, 21857, 21860, 21861, 21862, 21864,
  8017. 21865, 21866, 21889, 21892, 21893, 21897, 21898, 21904, 21905, 21908, 21909, 21910, 21912, 21913, 21921, 21924,
  8018. 21925, 21926, 21929, 22016, 22017, 22018, 22020, 22022, 22024, 22025, 22033, 22036, 22037, 22040, 22041, 22048,
  8019. 22049, 22050, 22052, 22053, 22054, 22056, 22057, 22081, 22085, 22086, 22088, 22089, 22090, 22096, 22097, 22098,
  8020. 22100, 22101, 22102, 22104, 22105, 22106, 22113, 22116, 22117, 22121, 22146, 22149, 22150, 22152, 22153, 22154,
  8021. 22161, 22165, 22170, 22178, 22181, 22182, 22184, 22185, 22532, 22533, 22534, 22537, 22544, 22549, 22552, 22561,
  8022. 22570, 22597, 22600, 22602, 22609, 22612, 22613, 22614, 22616, 22617, 22624, 22626, 22628, 22629, 22658, 22665,
  8023. 22672, 22674, 22677, 22680, 22689, 22697, 22785, 22786, 22789, 22794, 22801, 22804, 22805, 22806, 22809, 22821,
  8024. 22849, 22852, 22853, 22854, 22857, 22864, 22865, 22866, 22868, 22869, 22870, 22872, 22873, 22874, 22881, 22884,
  8025. 22885, 22886, 22889, 22913, 22917, 22921, 22929, 22932, 22933, 22934, 22936, 22937, 22949, 23044, 23048, 23061,
  8026. 23066, 23072, 23077, 23078, 23081, 23109, 23112, 23113, 23121, 23125, 23126, 23128, 23129, 23138, 23141, 23144,
  8027. 23146, 23169, 23178, 23186, 23189, 23190, 23192, 23194, 23201, 24581, 24596, 24598, 24601, 24613, 24644, 24656,
  8028. 24661, 24662, 24664, 24666, 24673, 24676, 24678, 24681, 24705, 24726, 24741, 24833, 24836, 24838, 24841, 24850,
  8029. 24853, 24865, 24866, 24870, 24873, 24901, 24905, 24913, 24917, 24918, 24921, 24933, 24934, 24938, 24964, 24970,
  8030. 24978, 24981, 24993, 24998, 25001, 25105, 25110, 25113, 25152, 25153, 25158, 25173, 25174, 25176, 25184, 25221,
  8031. 25233, 25238, 25253, 25617, 25618, 25621, 25622, 25626, 25633, 25638, 25641, 25664, 25666, 25669, 25672, 25674,
  8032. 25681, 25684, 25685, 25686, 25689, 25690, 25696, 25698, 25701, 25732, 25733, 25737, 25744, 25746, 25748, 25749,
  8033. 25750, 25752, 25754, 25761, 25764, 25769, 25861, 25864, 25866, 25873, 25877, 25878, 25881, 25924, 25925, 25926,
  8034. 25929, 25936, 25937, 25940, 25941, 25942, 25945, 25953, 25956, 25957, 25958, 25961, 25990, 25993, 25994, 26001,
  8035. 26005, 26006, 26009, 26010, 26018, 26021, 26022, 26024, 26114, 26121, 26133, 26144, 26150, 26152, 26153, 26176,
  8036. 26181, 26184, 26186, 26193, 26196, 26197, 26198, 26200, 26202, 26208, 26213, 26216, 26240, 26242, 26245, 26250,
  8037. 26260, 26262, 26264, 26265, 26272, 26276, 26278, 26282, 26646, 26649, 26661, 26689, 26706, 26709, 26714, 26721,
  8038. 26729, 26757, 26769, 26776, 26790, 26881, 26884, 26896, 26901, 26913, 26916, 26918, 26921, 26944, 26945, 26949,
  8039. 26950, 26952, 26961, 26964, 26965, 26966, 26969, 26976, 26981, 26986, 27010, 27012, 27018, 27029, 27041, 27044,
  8040. 27045, 27049, 27153, 27158, 27160, 27201, 27204, 27209, 27216, 27221, 27224, 27226, 27236, 27237, 27241, 27270,
  8041. 27284, 27288, 27290, 27302, 32768, 32770, 32776, 32778, 32800, 32802, 32808, 32810, 32837, 32848, 32849, 32852,
  8042. 32854, 32857, 32869, 32896, 32898, 32904, 32906, 32917, 32928, 32930, 32936, 32938, 33029, 33041, 33044, 33046,
  8043. 33049, 33061, 33089, 33092, 33097, 33104, 33106, 33109, 33110, 33112, 33113, 33124, 33126, 33129, 33157, 33161,
  8044. 33172, 33174, 33177, 33189, 33280, 33282, 33288, 33290, 33301, 33312, 33314, 33320, 33322, 33361, 33364, 33369,
  8045. 33381, 33408, 33410, 33416, 33418, 33429, 33440, 33442, 33448, 33450, 33812, 33817, 33857, 33860, 33873, 33877,
  8046. 33882, 33889, 33892, 33897, 33940, 33945, 34049, 34057, 34066, 34069, 34074, 34086, 34089, 34112, 34113, 34117,
  8047. 34120, 34129, 34132, 34133, 34134, 34137, 34138, 34149, 34150, 34152, 34154, 34177, 34180, 34182, 34185, 34192,
  8048. 34194, 34197, 34200, 34214, 34321, 34326, 34329, 34341, 34369, 34372, 34377, 34378, 34384, 34389, 34393, 34394,
  8049. 34401, 34406, 34410, 34437, 34449, 34458, 34468, 34816, 34818, 34824, 34826, 34837, 34848, 34850, 34856, 34858,
  8050. 34881, 34885, 34897, 34900, 34905, 34917, 34921, 34944, 34946, 34952, 34954, 34965, 34976, 34978, 34984, 34986,
  8051. 35077, 35078, 35089, 35092, 35094, 35109, 35137, 35140, 35142, 35145, 35152, 35154, 35157, 35162, 35169, 35172,
  8052. 35205, 35222, 35225, 35237, 35328, 35330, 35336, 35338, 35349, 35360, 35362, 35368, 35370, 35397, 35409, 35412,
  8053. 35414, 35456, 35458, 35464, 35466, 35477, 35488, 35490, 35496, 35498, 36869, 36881, 36886, 36888, 36889, 36901,
  8054. 36929, 36934, 36937, 36949, 36952, 36954, 36969, 36970, 36997, 37009, 37012, 37014, 37017, 37029, 37121, 37124,
  8055. 37126, 37129, 37136, 37141, 37144, 37146, 37153, 37156, 37158, 37161, 37184, 37189, 37200, 37201, 37204, 37205,
  8056. 37206, 37209, 37218, 37221, 37252, 37254, 37266, 37269, 37272, 37281, 37284, 37286, 37289, 37381, 37393, 37396,
  8057. 37401, 37413, 37444, 37446, 37449, 37456, 37458, 37461, 37464, 37478, 37481, 37509, 37524, 37526, 37545, 37889,
  8058. 37892, 37894, 37904, 37909, 37912, 37926, 37952, 37962, 37969, 37972, 37973, 37974, 37976, 37977, 37984, 37985,
  8059. 37986, 37989, 38020, 38022, 38034, 38036, 38037, 38040, 38049, 38057, 38144, 38149, 38152, 38154, 38160, 38161,
  8060. 38164, 38165, 38166, 38169, 38177, 38181, 38185, 38186, 38209, 38212, 38213, 38214, 38217, 38224, 38225, 38226,
  8061. 38228, 38229, 38230, 38232, 38233, 38234, 38241, 38244, 38245, 38246, 38249, 38273, 38277, 38280, 38289, 38290,
  8062. 38292, 38293, 38294, 38297, 38298, 38304, 38306, 38309, 38312, 38314, 38401, 38404, 38416, 38421, 38425, 38432,
  8063. 38438, 38441, 38469, 38472, 38473, 38481, 38482, 38485, 38486, 38489, 38501, 38504, 38530, 38532, 38537, 38538,
  8064. 38546, 38548, 38549, 38564, 38566, 38569, 38917, 38934, 38937, 38949, 38977, 38982, 38992, 38994, 38997, 38998,
  8065. 39002, 39012, 39013, 39045, 39057, 39062, 39065, 39077, 39172, 39174, 39177, 39184, 39186, 39189, 39192, 39194,
  8066. 39200, 39201, 39204, 39206, 39232, 39234, 39237, 39240, 39242, 39249, 39252, 39253, 39254, 39257, 39266, 39269,
  8067. 39270, 39274, 39297, 39300, 39312, 39314, 39317, 39322, 39329, 39334, 39429, 39445, 39461, 39492, 39494, 39497,
  8068. 39504, 39509, 39512, 39521, 39557, 39569, 39572, 39573, 39574, 40960, 40962, 40968, 40970, 40981, 40992, 40994,
  8069. 41000, 41002, 41029, 41041, 41044, 41046, 41049, 41088, 41090, 41096, 41098, 41109, 41120, 41122, 41128, 41130,
  8070. 41221, 41225, 41233, 41236, 41238, 41241, 41242, 41286, 41289, 41297, 41301, 41304, 41306, 41313, 41316, 41349,
  8071. 41360, 41362, 41366, 41369, 41474, 41480, 41482, 41488, 41497, 41506, 41512, 41514, 41541, 41553, 41558, 41561,
  8072. 41573, 41600, 41602, 41608, 41610, 41621, 41632, 41634, 41640, 41642, 42009, 42021, 42049, 42052, 42064, 42068,
  8073. 42069, 42072, 42074, 42081, 42085, 42086, 42088, 42089, 42117, 42246, 42249, 42256, 42258, 42261, 42264, 42278,
  8074. 42281, 42306, 42309, 42321, 42324, 42325, 42326, 42329, 42341, 42346, 42369, 42372, 42373, 42374, 42377, 42386,
  8075. 42389, 42392, 42501, 42513, 42518, 42522, 42529, 42533, 42564, 42566, 42570, 42578, 42581, 42582, 42584, 42592,
  8076. 42594, 42630, 42640, 42645, 42646, 42649, 42657, 42660, 42662, 43008, 43010, 43016, 43018, 43040, 43042, 43048,
  8077. 43050, 43089, 43092, 43094, 43097, 43136, 43138, 43144, 43146, 43157, 43168, 43170, 43176, 43178, 43269, 43284,
  8078. 43289, 43297, 43301, 43329, 43344, 43349, 43354, 43361, 43366, 43369, 43408, 43414, 43520, 43522, 43528, 43530,
  8079. 43552, 43554, 43560, 43562, 43601, 43604, 43606, 43648, 43650, 43656, 43658, 43669, 43680, 43682, 43688, 43690,
  8080. };
  8081. static const uint16_t kgrid_2bit_1024[1024] = {
  8082. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  8083. 73, 80, 82, 85, 88, 97, 100, 102, 105, 128, 130, 133, 136, 145, 148, 160,
  8084. 165, 170, 257, 260, 262, 265, 272, 274, 277, 280, 289, 292, 320, 322, 325, 328,
  8085. 337, 340, 342, 345, 352, 357, 360, 385, 388, 400, 402, 405, 417, 420, 512, 514,
  8086. 517, 520, 529, 532, 544, 554, 577, 580, 582, 585, 592, 597, 640, 645, 650, 660,
  8087. 674, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1062, 1065, 1088, 1090, 1093,
  8088. 1096, 1098, 1105, 1108, 1110, 1113, 1120, 1122, 1125, 1153, 1156, 1158, 1161, 1168, 1173, 1176,
  8089. 1185, 1188, 1280, 1282, 1285, 1288, 1290, 1297, 1300, 1302, 1305, 1312, 1317, 1320, 1345, 1348,
  8090. 1350, 1353, 1360, 1362, 1365, 1368, 1377, 1380, 1408, 1410, 1413, 1416, 1425, 1428, 1440, 1537,
  8091. 1540, 1542, 1545, 1552, 1557, 1600, 1605, 1608, 1617, 1620, 1632, 1665, 1668, 1680, 2048, 2050,
  8092. 2053, 2056, 2065, 2068, 2070, 2073, 2080, 2085, 2090, 2113, 2116, 2118, 2121, 2128, 2130, 2133,
  8093. 2136, 2145, 2148, 2176, 2181, 2196, 2218, 2305, 2308, 2320, 2322, 2325, 2328, 2337, 2368, 2373,
  8094. 2376, 2385, 2388, 2400, 2433, 2448, 2560, 2577, 2580, 2594, 2600, 2602, 2640, 2713, 4097, 4100,
  8095. 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4134, 4160, 4162, 4165, 4168, 4177, 4180, 4182,
  8096. 4185, 4192, 4194, 4197, 4200, 4225, 4228, 4230, 4240, 4245, 4248, 4257, 4260, 4352, 4354, 4357,
  8097. 4360, 4362, 4369, 4372, 4374, 4377, 4384, 4386, 4389, 4392, 4417, 4420, 4422, 4425, 4432, 4434,
  8098. 4437, 4440, 4449, 4452, 4480, 4482, 4485, 4488, 4497, 4500, 4609, 4612, 4617, 4624, 4629, 4641,
  8099. 4644, 4672, 4677, 4689, 4692, 4737, 4740, 4752, 5120, 5122, 5125, 5128, 5137, 5140, 5142, 5145,
  8100. 5152, 5157, 5160, 5185, 5188, 5190, 5193, 5200, 5202, 5205, 5208, 5217, 5220, 5248, 5250, 5253,
  8101. 5256, 5265, 5268, 5280, 5377, 5380, 5382, 5385, 5392, 5394, 5397, 5400, 5409, 5412, 5440, 5442,
  8102. 5445, 5448, 5457, 5460, 5472, 5505, 5508, 5520, 5632, 5637, 5640, 5649, 5652, 5664, 5697, 5700,
  8103. 5712, 5760, 5802, 6145, 6148, 6150, 6153, 6160, 6165, 6168, 6177, 6208, 6210, 6213, 6216, 6225,
  8104. 6228, 6240, 6273, 6276, 6400, 6402, 6405, 6408, 6417, 6420, 6432, 6465, 6468, 6480, 6505, 6562,
  8105. 6660, 6672, 6720, 6742, 8192, 8194, 8197, 8200, 8209, 8212, 8214, 8217, 8224, 8229, 8234, 8257,
  8106. 8260, 8272, 8274, 8277, 8292, 8320, 8330, 8340, 8362, 8449, 8452, 8464, 8466, 8469, 8481, 8512,
  8107. 8514, 8517, 8529, 8532, 8544, 8577, 8580, 8592, 8704, 8714, 8738, 8744, 8746, 8772, 8784, 8840,
  8108. 8842, 8872, 9217, 9220, 9222, 9225, 9232, 9237, 9240, 9249, 9252, 9280, 9282, 9285, 9288, 9297,
  8109. 9300, 9312, 9345, 9348, 9360, 9472, 9477, 9480, 9489, 9492, 9504, 9537, 9540, 9552, 9574, 9600,
  8110. 9729, 9732, 9744, 9792, 9817, 10240, 10245, 10257, 10260, 10305, 10308, 10320, 10378, 10410, 10497, 10500,
  8111. 10512, 10645, 10762, 10786, 10852, 10888, 10890, 16385, 16388, 16390, 16393, 16400, 16402, 16405, 16408, 16410,
  8112. 16417, 16420, 16422, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16470, 16473, 16480, 16482, 16485, 16513,
  8113. 16516, 16528, 16533, 16536, 16545, 16548, 16640, 16642, 16645, 16648, 16657, 16660, 16662, 16665, 16672, 16674,
  8114. 16677, 16705, 16708, 16710, 16713, 16720, 16722, 16725, 16728, 16737, 16740, 16768, 16770, 16773, 16776, 16785,
  8115. 16788, 16800, 16897, 16900, 16912, 16914, 16917, 16920, 16932, 16960, 16965, 16968, 16977, 16980, 16992, 17025,
  8116. 17028, 17408, 17410, 17413, 17416, 17418, 17425, 17428, 17430, 17433, 17440, 17442, 17445, 17448, 17473, 17476,
  8117. 17478, 17481, 17488, 17490, 17493, 17496, 17505, 17508, 17536, 17538, 17541, 17544, 17553, 17556, 17568, 17665,
  8118. 17668, 17670, 17673, 17680, 17682, 17685, 17688, 17697, 17700, 17728, 17730, 17733, 17736, 17745, 17748, 17760,
  8119. 17770, 17793, 17796, 17808, 17920, 17922, 17925, 17928, 17937, 17940, 17952, 17985, 17988, 18000, 18048, 18085,
  8120. 18433, 18436, 18441, 18448, 18450, 18453, 18456, 18465, 18468, 18496, 18498, 18501, 18504, 18513, 18516, 18528,
  8121. 18564, 18576, 18688, 18690, 18693, 18696, 18705, 18708, 18720, 18753, 18756, 18768, 18816, 18838, 18945, 18948,
  8122. 18960, 19008, 20480, 20482, 20485, 20488, 20497, 20500, 20502, 20505, 20512, 20514, 20517, 20520, 20545, 20548,
  8123. 20550, 20553, 20560, 20562, 20565, 20568, 20577, 20580, 20608, 20610, 20613, 20616, 20625, 20628, 20737, 20740,
  8124. 20742, 20745, 20752, 20754, 20757, 20760, 20769, 20772, 20800, 20802, 20805, 20808, 20817, 20820, 20832, 20865,
  8125. 20868, 20880, 20992, 20997, 21000, 21009, 21012, 21024, 21057, 21060, 21072, 21097, 21120, 21505, 21508, 21510,
  8126. 21513, 21520, 21522, 21525, 21528, 21537, 21540, 21568, 21570, 21573, 21576, 21585, 21588, 21600, 21633, 21636,
  8127. 21648, 21760, 21762, 21765, 21768, 21777, 21780, 21792, 21825, 21828, 21840, 21888, 22017, 22020, 22032, 22054,
  8128. 22080, 22528, 22530, 22533, 22536, 22545, 22548, 22560, 22593, 22596, 22608, 22618, 22656, 22785, 22788, 22800,
  8129. 22848, 23040, 23065, 23173, 23208, 24577, 24580, 24582, 24592, 24594, 24597, 24600, 24609, 24612, 24640, 24645,
  8130. 24648, 24657, 24660, 24672, 24708, 24720, 24832, 24834, 24837, 24840, 24849, 24852, 24864, 24897, 24900, 24912,
  8131. 24960, 24985, 25092, 25104, 25152, 25174, 25249, 25600, 25605, 25608, 25617, 25620, 25632, 25665, 25668, 25680,
  8132. 25728, 25857, 25860, 25872, 25920, 25930, 25960, 26002, 26112, 26260, 26625, 26628, 26640, 26725, 26776, 26880,
  8133. 26922, 27202, 27297, 32768, 32770, 32773, 32776, 32785, 32788, 32793, 32800, 32805, 32833, 32836, 32848, 32850,
  8134. 32853, 32856, 32865, 32896, 32901, 32913, 32916, 33025, 33028, 33033, 33040, 33042, 33045, 33048, 33057, 33060,
  8135. 33088, 33090, 33093, 33096, 33105, 33108, 33153, 33156, 33168, 33193, 33280, 33285, 33290, 33297, 33300, 33345,
  8136. 33348, 33360, 33793, 33796, 33798, 33801, 33808, 33810, 33813, 33816, 33825, 33856, 33858, 33861, 33864, 33873,
  8137. 33876, 33888, 33921, 33924, 33936, 34048, 34050, 34053, 34056, 34065, 34068, 34080, 34113, 34116, 34128, 34176,
  8138. 34186, 34305, 34308, 34320, 34345, 34368, 34816, 34821, 34833, 34836, 34881, 34884, 34896, 34978, 35073, 35076,
  8139. 35136, 35173, 35362, 35416, 35418, 35458, 35490, 36865, 36868, 36873, 36880, 36882, 36885, 36888, 36900, 36928,
  8140. 36930, 36933, 36936, 36945, 36948, 36960, 36993, 36996, 37008, 37120, 37125, 37137, 37140, 37185, 37188, 37200,
  8141. 37210, 37377, 37380, 37392, 37440, 37542, 37888, 37890, 37893, 37896, 37905, 37908, 37920, 37953, 37956, 37968,
  8142. 38016, 38038, 38145, 38148, 38160, 38208, 38296, 38305, 38400, 38470, 38500, 38913, 38916, 38928, 38950, 38976,
  8143. 39081, 39168, 39241, 39250, 39568, 40960, 40965, 40970, 40980, 40994, 41002, 41025, 41028, 41040, 41122, 41130,
  8144. 41280, 41317, 41474, 41482, 41506, 41512, 41514, 41602, 41608, 41610, 41640, 41985, 41988, 42000, 42048, 42121,
  8145. 42148, 42240, 42265, 42577, 43018, 43048, 43170, 43348, 43398, 43528, 43530, 43552, 43554, 43560, 43656, 43690,
  8146. };
  8147. const int kmap_size = 43692;
  8148. //const int nwant = type == GGML_TYPE_IQ1_S ? 3 : 2;
  8149. const int nwant = type == GGML_TYPE_IQ1_S ? 3 : type == GGML_TYPE_IQ2_S ? 1 : 2;
  8150. const uint16_t * kgrid = type == GGML_TYPE_IQ2_XXS ? kgrid_2bit_256 :
  8151. type == GGML_TYPE_IQ2_XS ? kgrid_2bit_512 :
  8152. type == GGML_TYPE_IQ1_S ? kgrid_1bit_2048 : kgrid_2bit_1024;
  8153. uint64_t * kgrid_q2xs;
  8154. int * kmap_q2xs;
  8155. uint16_t * kneighbors_q2xs;
  8156. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8157. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  8158. for (int k = 0; k < grid_size; ++k) {
  8159. int8_t * pos = (int8_t *)(the_grid + k);
  8160. for (int i = 0; i < 8; ++i) {
  8161. int l = (kgrid[k] >> 2*i) & 0x3;
  8162. pos[i] = 2*l + 1;
  8163. }
  8164. }
  8165. kgrid_q2xs = the_grid;
  8166. iq2_data[gindex].grid = the_grid;
  8167. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  8168. iq2_data[gindex].map = kmap_q2xs;
  8169. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  8170. uint64_t aux64;
  8171. uint8_t * aux8 = (uint8_t *)&aux64;
  8172. for (int i = 0; i < grid_size; ++i) {
  8173. aux64 = kgrid_q2xs[i];
  8174. uint16_t index = 0;
  8175. for (int k=0; k<8; ++k) {
  8176. uint16_t q = (aux8[k] - 1)/2;
  8177. index |= (q << 2*k);
  8178. }
  8179. kmap_q2xs[index] = i;
  8180. }
  8181. int8_t pos[8];
  8182. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8183. int num_neighbors = 0, num_not_in_map = 0;
  8184. for (int i = 0; i < kmap_size; ++i) {
  8185. if (kmap_q2xs[i] >= 0) continue;
  8186. ++num_not_in_map;
  8187. for (int k = 0; k < 8; ++k) {
  8188. int l = (i >> 2*k) & 0x3;
  8189. pos[k] = 2*l + 1;
  8190. }
  8191. for (int j = 0; j < grid_size; ++j) {
  8192. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8193. int d2 = 0;
  8194. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8195. dist2[2*j+0] = d2;
  8196. dist2[2*j+1] = j;
  8197. }
  8198. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8199. int n = 0; int d2 = dist2[0];
  8200. int nhave = 1;
  8201. for (int j = 0; j < grid_size; ++j) {
  8202. if (dist2[2*j] > d2) {
  8203. if (nhave == nwant) break;
  8204. d2 = dist2[2*j];
  8205. ++nhave;
  8206. }
  8207. ++n;
  8208. }
  8209. num_neighbors += n;
  8210. }
  8211. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8212. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8213. iq2_data[gindex].neighbours = kneighbors_q2xs;
  8214. int counter = 0;
  8215. for (int i = 0; i < kmap_size; ++i) {
  8216. if (kmap_q2xs[i] >= 0) continue;
  8217. for (int k = 0; k < 8; ++k) {
  8218. int l = (i >> 2*k) & 0x3;
  8219. pos[k] = 2*l + 1;
  8220. }
  8221. for (int j = 0; j < grid_size; ++j) {
  8222. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  8223. int d2 = 0;
  8224. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8225. dist2[2*j+0] = d2;
  8226. dist2[2*j+1] = j;
  8227. }
  8228. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  8229. kmap_q2xs[i] = -(counter + 1);
  8230. int d2 = dist2[0];
  8231. uint16_t * start = &kneighbors_q2xs[counter++];
  8232. int n = 0, nhave = 1;
  8233. for (int j = 0; j < grid_size; ++j) {
  8234. if (dist2[2*j] > d2) {
  8235. if (nhave == nwant) break;
  8236. d2 = dist2[2*j];
  8237. ++nhave;
  8238. }
  8239. kneighbors_q2xs[counter++] = dist2[2*j+1];
  8240. ++n;
  8241. }
  8242. *start = n;
  8243. }
  8244. free(dist2);
  8245. }
  8246. void iq2xs_free_impl(enum ggml_type type) {
  8247. GGML_ASSERT(type == GGML_TYPE_IQ2_XXS || type == GGML_TYPE_IQ2_XS || type == GGML_TYPE_IQ1_S || type == GGML_TYPE_IQ2_S);
  8248. const int gindex = iq2_data_index(type);
  8249. if (iq2_data[gindex].grid) {
  8250. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  8251. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  8252. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  8253. }
  8254. }
  8255. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  8256. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8257. int num_neighbors = neighbours[0];
  8258. GGML_ASSERT(num_neighbors > 0);
  8259. float best_d2 = FLT_MAX;
  8260. int grid_index = -1;
  8261. for (int j = 1; j <= num_neighbors; ++j) {
  8262. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8263. float d2 = 0;
  8264. for (int i = 0; i < 8; ++i) {
  8265. float q = pg[i];
  8266. float diff = scale*q - xval[i];
  8267. d2 += weight[i]*diff*diff;
  8268. }
  8269. if (d2 < best_d2) {
  8270. best_d2 = d2; grid_index = neighbours[j];
  8271. }
  8272. }
  8273. GGML_ASSERT(grid_index >= 0);
  8274. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8275. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  8276. return grid_index;
  8277. }
  8278. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  8279. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XXS);
  8280. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8281. const int * kmap_q2xs = iq2_data[gindex].map;
  8282. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8283. GGML_ASSERT(quant_weights && "missing quantization weights");
  8284. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8285. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8286. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8287. GGML_ASSERT(n%QK_K == 0);
  8288. const int kMaxQ = 3;
  8289. const int nbl = n/QK_K;
  8290. block_iq2_xxs * y = vy;
  8291. float scales[QK_K/32];
  8292. float weight[32];
  8293. float xval[32];
  8294. int8_t L[32];
  8295. int8_t Laux[32];
  8296. float waux[32];
  8297. uint8_t block_signs[4];
  8298. uint32_t q2[2*(QK_K/32)];
  8299. for (int ibl = 0; ibl < nbl; ++ibl) {
  8300. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8301. memset(q2, 0, QK_K/4);
  8302. float max_scale = 0;
  8303. const float * xbl = x + QK_K*ibl;
  8304. float sumx2 = 0;
  8305. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8306. float sigma2 = sumx2/QK_K;
  8307. for (int ib = 0; ib < QK_K/32; ++ib) {
  8308. const float * xb = xbl + 32*ib;
  8309. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8310. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8311. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8312. for (int k = 0; k < 4; ++k) {
  8313. int nflip = 0;
  8314. uint8_t s = 0;
  8315. for (int i = 0; i < 8; ++i) {
  8316. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8317. else {
  8318. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8319. }
  8320. }
  8321. if (nflip%2) {
  8322. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8323. for (int i = 1; i < 8; ++i) {
  8324. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8325. if (ax < min) {
  8326. min = ax; imin = i;
  8327. }
  8328. }
  8329. xval[8*k+imin] = -xval[8*k+imin];
  8330. s ^= (1 << imin);
  8331. }
  8332. block_signs[k] = s & 127;
  8333. }
  8334. float max = xval[0];
  8335. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8336. if (!max) {
  8337. scales[ib] = 0;
  8338. memset(L, 0, 32);
  8339. continue;
  8340. }
  8341. float scale = make_qp_quants(32, kMaxQ+1, xval, (uint8_t*)L, weight);
  8342. float eff_max = scale*kMaxQ;
  8343. float best = 0;
  8344. for (int is = -6; is <= 6; ++is) {
  8345. float id = (2*kMaxQ-1+is*0.1f)/eff_max;
  8346. float this_scale = 1/id;
  8347. for (int k = 0; k < 4; ++k) {
  8348. for (int i = 0; i < 8; ++i) {
  8349. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8350. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8351. }
  8352. uint16_t u = 0;
  8353. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8354. int grid_index = kmap_q2xs[u];
  8355. if (grid_index < 0) {
  8356. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8357. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8358. }
  8359. }
  8360. float sumqx = 0, sumq2 = 0;
  8361. for (int i = 0; i < 32; ++i) {
  8362. float w = weight[i];
  8363. float q = 2*Laux[i] + 1;
  8364. sumqx += w*xval[i]*q;
  8365. sumq2 += w*q*q;
  8366. }
  8367. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8368. scale = sumqx/sumq2; best = scale*sumqx;
  8369. memcpy(L, Laux, 32);
  8370. }
  8371. }
  8372. if (scale > 0) {
  8373. float id = 1/scale;
  8374. for (int k = 0; k < 4; ++k) {
  8375. uint16_t u = 0;
  8376. for (int i = 0; i < 8; ++i) {
  8377. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8378. l = MAX(0, MIN(kMaxQ-1, l));
  8379. u |= (l << 2*i);
  8380. }
  8381. int grid_index = kmap_q2xs[u];
  8382. if (grid_index < 0) {
  8383. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8384. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8385. }
  8386. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  8387. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  8388. }
  8389. float sumqx = 0, sumq2 = 0;
  8390. for (int i = 0; i < 32; ++i) {
  8391. float w = weight[i];
  8392. float q = 2*L[i] + 1;
  8393. sumqx += w*xval[i]*q;
  8394. sumq2 += w*q*q;
  8395. }
  8396. if (sumq2 > 0) scale = sumqx/sumq2;
  8397. }
  8398. if (scale < 0) {
  8399. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8400. // and correspondingly flip quant signs.
  8401. scale = -scale;
  8402. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8403. }
  8404. for (int k = 0; k < 4; ++k) {
  8405. uint16_t u = 0;
  8406. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8407. int grid_index = kmap_q2xs[u];
  8408. if (grid_index < 0) {
  8409. printf("Oops: found point %u not on grid:", u);
  8410. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8411. printf("\n");
  8412. GGML_ASSERT(false);
  8413. }
  8414. q2[2*ib+0] |= (grid_index << 8*k);
  8415. q2[2*ib+1] |= (block_signs[k] << 7*k);
  8416. }
  8417. GGML_ASSERT(scale >= 0);
  8418. scales[ib] = scale;
  8419. max_scale = MAX(max_scale, scale);
  8420. }
  8421. if (!max_scale) {
  8422. memset(y[ibl].qs, 0, QK_K/4);
  8423. continue;
  8424. }
  8425. float d = max_scale/31;
  8426. y[ibl].d = GGML_FP32_TO_FP16(d);
  8427. float id = 1/d;
  8428. for (int ib = 0; ib < QK_K/32; ++ib) {
  8429. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8430. l = MAX(0, MIN(15, l));
  8431. q2[2*ib+1] |= ((uint32_t)l << 28);
  8432. }
  8433. memcpy(y[ibl].qs, q2, QK_K/4);
  8434. }
  8435. }
  8436. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  8437. const int gindex = iq2_data_index(GGML_TYPE_IQ2_XS);
  8438. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  8439. const int * kmap_q2xs = iq2_data[gindex].map;
  8440. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  8441. GGML_ASSERT(quant_weights && "missing quantization weights");
  8442. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  8443. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  8444. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  8445. GGML_ASSERT(n%QK_K == 0);
  8446. const int kMaxQ = 3;
  8447. const int nbl = n/QK_K;
  8448. block_iq2_xs * y = vy;
  8449. float scales[QK_K/16];
  8450. float weight[16];
  8451. float xval[16];
  8452. int8_t L[16];
  8453. int8_t Laux[16];
  8454. float waux[16];
  8455. bool is_on_grid[2];
  8456. bool is_on_grid_aux[2];
  8457. uint8_t block_signs[2];
  8458. uint16_t q2[2*(QK_K/16)];
  8459. for (int ibl = 0; ibl < nbl; ++ibl) {
  8460. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  8461. memset(q2, 0, QK_K/4);
  8462. memset(y[ibl].scales, 0, QK_K/32);
  8463. float max_scale = 0;
  8464. const float * xbl = x + QK_K*ibl;
  8465. float sumx2 = 0;
  8466. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8467. float sigma2 = sumx2/QK_K;
  8468. for (int ib = 0; ib < QK_K/16; ++ib) {
  8469. const float * xb = xbl + 16*ib;
  8470. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  8471. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8472. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  8473. for (int k = 0; k < 2; ++k) {
  8474. int nflip = 0;
  8475. uint8_t s = 0;
  8476. for (int i = 0; i < 8; ++i) {
  8477. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8478. else {
  8479. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8480. }
  8481. }
  8482. if (nflip%2) {
  8483. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8484. for (int i = 1; i < 8; ++i) {
  8485. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8486. if (ax < min) {
  8487. min = ax; imin = i;
  8488. }
  8489. }
  8490. xval[8*k+imin] = -xval[8*k+imin];
  8491. s ^= (1 << imin);
  8492. }
  8493. block_signs[k] = s & 127;
  8494. }
  8495. float max = xval[0];
  8496. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  8497. if (!max) {
  8498. scales[ib] = 0;
  8499. memset(L, 0, 16);
  8500. continue;
  8501. }
  8502. float best = 0;
  8503. float scale = max/(2*kMaxQ-1);
  8504. is_on_grid[0] = is_on_grid[1] = true;
  8505. for (int is = -9; is <= 9; ++is) {
  8506. float id = (2*kMaxQ-1+is*0.1f)/max;
  8507. float this_scale = 1/id;
  8508. for (int k = 0; k < 2; ++k) {
  8509. for (int i = 0; i < 8; ++i) {
  8510. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8511. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8512. }
  8513. uint16_t u = 0;
  8514. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  8515. int grid_index = kmap_q2xs[u];
  8516. is_on_grid_aux[k] = true;
  8517. if (grid_index < 0) {
  8518. is_on_grid_aux[k] = false;
  8519. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8520. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  8521. }
  8522. }
  8523. float sumqx = 0, sumq2 = 0;
  8524. for (int i = 0; i < 16; ++i) {
  8525. float w = weight[i];
  8526. float q = 2*Laux[i] + 1;
  8527. sumqx += w*xval[i]*q;
  8528. sumq2 += w*q*q;
  8529. }
  8530. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8531. scale = sumqx/sumq2; best = scale*sumqx;
  8532. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  8533. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8534. }
  8535. }
  8536. int n_not_ongrid = 0;
  8537. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8538. if (n_not_ongrid > 0 && scale > 0) {
  8539. float id = 1/scale;
  8540. for (int k = 0; k < 2; ++k) {
  8541. if (is_on_grid[k]) continue;
  8542. uint16_t u = 0;
  8543. for (int i = 0; i < 8; ++i) {
  8544. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  8545. l = MAX(0, MIN(kMaxQ-1, l));
  8546. u |= (l << 2*i);
  8547. L[8*k + i] = l;
  8548. }
  8549. int grid_index = kmap_q2xs[u];
  8550. if (grid_index < 0) {
  8551. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  8552. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  8553. }
  8554. }
  8555. float sumqx = 0, sumq2 = 0;
  8556. for (int i = 0; i < 16; ++i) {
  8557. float w = weight[i];
  8558. float q = 2*L[i] + 1;
  8559. sumqx += w*xval[i]*q;
  8560. sumq2 += w*q*q;
  8561. }
  8562. if (sumq2 > 0) scale = sumqx/sumq2;
  8563. }
  8564. if (scale < 0) {
  8565. scale = -scale;
  8566. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8567. }
  8568. for (int k = 0; k < 2; ++k) {
  8569. uint16_t u = 0;
  8570. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  8571. int grid_index = kmap_q2xs[u];
  8572. if (grid_index < 0) {
  8573. printf("Oops: found point %u not on grid:", u);
  8574. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  8575. printf("\n");
  8576. GGML_ASSERT(false);
  8577. }
  8578. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  8579. }
  8580. GGML_ASSERT(scale >= 0);
  8581. scales[ib] = scale;
  8582. max_scale = MAX(max_scale, scale);
  8583. }
  8584. if (!max_scale) {
  8585. memset(y[ibl].qs, 0, QK_K/4);
  8586. continue;
  8587. }
  8588. float d = max_scale/31;
  8589. y[ibl].d = GGML_FP32_TO_FP16(d);
  8590. float id = 1/d;
  8591. for (int ib = 0; ib < QK_K/16; ++ib) {
  8592. int l = nearest_int(0.5f*(id*scales[ib]-1));
  8593. l = MAX(0, MIN(15, l));
  8594. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  8595. else y[ibl].scales[ib/2] |= (l << 4);
  8596. }
  8597. memcpy(y[ibl].qs, q2, QK_K/4);
  8598. }
  8599. }
  8600. size_t quantize_iq2_xxs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  8601. GGML_ASSERT(n_per_row%QK_K == 0);
  8602. int nblock = n_per_row/QK_K;
  8603. char * qrow = (char *)dst;
  8604. for (int row = 0; row < nrow; ++row) {
  8605. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  8606. src += n_per_row;
  8607. qrow += nblock*sizeof(block_iq2_xxs);
  8608. }
  8609. return nrow * nblock * sizeof(block_iq2_xxs);
  8610. }
  8611. size_t quantize_iq2_xs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  8612. GGML_ASSERT(n_per_row%QK_K == 0);
  8613. int nblock = n_per_row/QK_K;
  8614. char * qrow = (char *)dst;
  8615. for (int row = 0; row < nrow; ++row) {
  8616. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  8617. src += n_per_row;
  8618. qrow += nblock*sizeof(block_iq2_xs);
  8619. }
  8620. return nrow * nblock * sizeof(block_iq2_xs);
  8621. }
  8622. //
  8623. // ============================================= 3-bit using D4 lattice
  8624. //
  8625. typedef struct {
  8626. uint32_t * grid;
  8627. int * map;
  8628. uint16_t * neighbours;
  8629. } iq3_entry_t;
  8630. static iq3_entry_t iq3_data[2] = {
  8631. {NULL, NULL, NULL},
  8632. {NULL, NULL, NULL},
  8633. };
  8634. static inline int iq3_data_index(int grid_size) {
  8635. (void)grid_size;
  8636. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8637. return grid_size == 256 ? 0 : 1;
  8638. }
  8639. static int iq3_compare_func(const void * left, const void * right) {
  8640. const int * l = (const int *)left;
  8641. const int * r = (const int *)right;
  8642. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  8643. }
  8644. void iq3xs_init_impl(int grid_size) {
  8645. const int gindex = iq3_data_index(grid_size);
  8646. if (iq3_data[gindex].grid) {
  8647. return;
  8648. }
  8649. static const uint16_t kgrid_256[256] = {
  8650. 0, 2, 4, 9, 11, 15, 16, 18, 25, 34, 59, 61, 65, 67, 72, 74,
  8651. 81, 85, 88, 90, 97, 108, 120, 128, 130, 132, 137, 144, 146, 153, 155, 159,
  8652. 169, 175, 189, 193, 199, 200, 202, 213, 248, 267, 287, 292, 303, 315, 317, 321,
  8653. 327, 346, 362, 413, 436, 456, 460, 462, 483, 497, 513, 515, 520, 522, 529, 531,
  8654. 536, 538, 540, 551, 552, 576, 578, 585, 592, 594, 641, 643, 648, 650, 657, 664,
  8655. 698, 704, 706, 720, 729, 742, 758, 769, 773, 808, 848, 852, 870, 889, 901, 978,
  8656. 992, 1024, 1026, 1033, 1035, 1040, 1042, 1046, 1049, 1058, 1089, 1091, 1093, 1096, 1098, 1105,
  8657. 1112, 1139, 1143, 1144, 1152, 1154, 1161, 1167, 1168, 1170, 1183, 1184, 1197, 1217, 1224, 1228,
  8658. 1272, 1276, 1309, 1323, 1347, 1367, 1377, 1404, 1473, 1475, 1486, 1509, 1537, 1544, 1546, 1553,
  8659. 1555, 1576, 1589, 1594, 1600, 1602, 1616, 1625, 1636, 1638, 1665, 1667, 1672, 1685, 1706, 1722,
  8660. 1737, 1755, 1816, 1831, 1850, 1856, 1862, 1874, 1901, 1932, 1950, 1971, 2011, 2032, 2052, 2063,
  8661. 2077, 2079, 2091, 2095, 2172, 2192, 2207, 2208, 2224, 2230, 2247, 2277, 2308, 2345, 2356, 2389,
  8662. 2403, 2424, 2501, 2504, 2506, 2520, 2570, 2593, 2616, 2624, 2630, 2646, 2669, 2700, 2714, 2746,
  8663. 2754, 2795, 2824, 2835, 2839, 2874, 2882, 2905, 2984, 3028, 3042, 3092, 3108, 3110, 3124, 3153,
  8664. 3185, 3215, 3252, 3288, 3294, 3364, 3397, 3434, 3483, 3523, 3537, 3587, 3589, 3591, 3592, 3610,
  8665. 3626, 3670, 3680, 3722, 3749, 3754, 3776, 3789, 3803, 3824, 3857, 3873, 3904, 3906, 3924, 3992,
  8666. };
  8667. static const uint16_t kgrid_512[512] = {
  8668. 0, 1, 2, 5, 7, 8, 9, 10, 12, 14, 16, 17, 21, 27, 32, 34,
  8669. 37, 39, 41, 43, 48, 50, 57, 60, 63, 64, 65, 66, 68, 72, 73, 77,
  8670. 80, 83, 87, 89, 93, 100, 113, 117, 122, 128, 129, 133, 135, 136, 139, 142,
  8671. 145, 149, 152, 156, 162, 165, 167, 169, 171, 184, 187, 195, 201, 205, 208, 210,
  8672. 217, 219, 222, 228, 232, 234, 247, 249, 253, 256, 267, 271, 273, 276, 282, 288,
  8673. 291, 297, 312, 322, 324, 336, 338, 342, 347, 353, 357, 359, 374, 379, 390, 393,
  8674. 395, 409, 426, 441, 448, 450, 452, 464, 466, 470, 475, 488, 492, 512, 513, 514,
  8675. 516, 520, 521, 523, 525, 527, 528, 530, 537, 540, 542, 556, 558, 561, 570, 576,
  8676. 577, 579, 582, 584, 588, 593, 600, 603, 609, 616, 618, 632, 638, 640, 650, 653,
  8677. 655, 656, 660, 666, 672, 675, 685, 688, 698, 705, 708, 711, 712, 715, 721, 727,
  8678. 728, 732, 737, 754, 760, 771, 773, 778, 780, 793, 795, 802, 806, 808, 812, 833,
  8679. 840, 843, 849, 856, 858, 873, 912, 916, 919, 932, 934, 961, 963, 968, 970, 977,
  8680. 989, 993, 1010, 1016, 1024, 1025, 1027, 1029, 1031, 1032, 1034, 1036, 1038, 1041, 1043, 1047,
  8681. 1048, 1050, 1057, 1059, 1061, 1064, 1066, 1079, 1080, 1083, 1085, 1088, 1090, 1096, 1099, 1103,
  8682. 1106, 1109, 1113, 1116, 1122, 1129, 1153, 1156, 1159, 1169, 1171, 1176, 1183, 1185, 1195, 1199,
  8683. 1209, 1212, 1216, 1218, 1221, 1225, 1234, 1236, 1241, 1243, 1250, 1256, 1270, 1281, 1287, 1296,
  8684. 1299, 1306, 1309, 1313, 1338, 1341, 1348, 1353, 1362, 1375, 1376, 1387, 1400, 1408, 1410, 1415,
  8685. 1425, 1453, 1457, 1477, 1481, 1494, 1496, 1507, 1512, 1538, 1545, 1547, 1549, 1551, 1554, 1561,
  8686. 1563, 1565, 1570, 1572, 1575, 1577, 1587, 1593, 1601, 1603, 1605, 1612, 1617, 1619, 1632, 1648,
  8687. 1658, 1662, 1664, 1674, 1680, 1690, 1692, 1704, 1729, 1736, 1740, 1745, 1747, 1751, 1752, 1761,
  8688. 1763, 1767, 1773, 1787, 1795, 1801, 1806, 1810, 1817, 1834, 1840, 1844, 1857, 1864, 1866, 1877,
  8689. 1882, 1892, 1902, 1915, 1934, 1953, 1985, 1987, 2000, 2002, 2013, 2048, 2052, 2058, 2064, 2068,
  8690. 2071, 2074, 2081, 2088, 2104, 2114, 2119, 2121, 2123, 2130, 2136, 2141, 2147, 2153, 2157, 2177,
  8691. 2179, 2184, 2189, 2193, 2203, 2208, 2223, 2226, 2232, 2244, 2249, 2251, 2256, 2258, 2265, 2269,
  8692. 2304, 2306, 2324, 2335, 2336, 2361, 2373, 2375, 2385, 2418, 2443, 2460, 2480, 2504, 2509, 2520,
  8693. 2531, 2537, 2562, 2568, 2572, 2578, 2592, 2596, 2599, 2602, 2614, 2620, 2625, 2627, 2629, 2634,
  8694. 2641, 2650, 2682, 2688, 2697, 2707, 2712, 2718, 2731, 2754, 2759, 2760, 2775, 2788, 2793, 2805,
  8695. 2811, 2817, 2820, 2832, 2842, 2854, 2890, 2902, 2921, 2923, 2978, 3010, 3012, 3026, 3081, 3083,
  8696. 3085, 3097, 3099, 3120, 3136, 3152, 3159, 3188, 3210, 3228, 3234, 3245, 3250, 3256, 3264, 3276,
  8697. 3281, 3296, 3349, 3363, 3378, 3392, 3395, 3420, 3440, 3461, 3488, 3529, 3531, 3584, 3588, 3591,
  8698. 3600, 3602, 3614, 3616, 3628, 3634, 3650, 3657, 3668, 3683, 3685, 3713, 3716, 3720, 3726, 3729,
  8699. 3736, 3753, 3778, 3802, 3805, 3819, 3841, 3845, 3851, 3856, 3880, 3922, 3938, 3970, 3993, 4032,
  8700. };
  8701. const int kmap_size = 4096;
  8702. const int nwant = grid_size == 256 ? 2 : 3;
  8703. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  8704. uint32_t * kgrid_q3xs;
  8705. int * kmap_q3xs;
  8706. uint16_t * kneighbors_q3xs;
  8707. //printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  8708. uint32_t * the_grid = (uint32_t *)malloc(grid_size*sizeof(uint32_t));
  8709. for (int k = 0; k < grid_size; ++k) {
  8710. int8_t * pos = (int8_t *)(the_grid + k);
  8711. for (int i = 0; i < 4; ++i) {
  8712. int l = (kgrid[k] >> 3*i) & 0x7;
  8713. pos[i] = 2*l + 1;
  8714. }
  8715. }
  8716. kgrid_q3xs = the_grid;
  8717. iq3_data[gindex].grid = the_grid;
  8718. kmap_q3xs = (int *)malloc(kmap_size*sizeof(int));
  8719. iq3_data[gindex].map = kmap_q3xs;
  8720. for (int i = 0; i < kmap_size; ++i) kmap_q3xs[i] = -1;
  8721. uint32_t aux32;
  8722. uint8_t * aux8 = (uint8_t *)&aux32;
  8723. for (int i = 0; i < grid_size; ++i) {
  8724. aux32 = kgrid_q3xs[i];
  8725. uint16_t index = 0;
  8726. for (int k=0; k<4; ++k) {
  8727. uint16_t q = (aux8[k] - 1)/2;
  8728. index |= (q << 3*k);
  8729. }
  8730. kmap_q3xs[index] = i;
  8731. }
  8732. int8_t pos[4];
  8733. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  8734. int num_neighbors = 0, num_not_in_map = 0;
  8735. for (int i = 0; i < kmap_size; ++i) {
  8736. if (kmap_q3xs[i] >= 0) continue;
  8737. ++num_not_in_map;
  8738. for (int k = 0; k < 4; ++k) {
  8739. int l = (i >> 3*k) & 0x7;
  8740. pos[k] = 2*l + 1;
  8741. }
  8742. for (int j = 0; j < grid_size; ++j) {
  8743. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8744. int d2 = 0;
  8745. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8746. dist2[2*j+0] = d2;
  8747. dist2[2*j+1] = j;
  8748. }
  8749. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8750. int n = 0; int d2 = dist2[0];
  8751. int nhave = 1;
  8752. for (int j = 0; j < grid_size; ++j) {
  8753. if (dist2[2*j] > d2) {
  8754. if (nhave == nwant) break;
  8755. d2 = dist2[2*j];
  8756. ++nhave;
  8757. }
  8758. ++n;
  8759. }
  8760. num_neighbors += n;
  8761. }
  8762. //printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  8763. kneighbors_q3xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  8764. iq3_data[gindex].neighbours = kneighbors_q3xs;
  8765. int counter = 0;
  8766. for (int i = 0; i < kmap_size; ++i) {
  8767. if (kmap_q3xs[i] >= 0) continue;
  8768. for (int k = 0; k < 4; ++k) {
  8769. int l = (i >> 3*k) & 0x7;
  8770. pos[k] = 2*l + 1;
  8771. }
  8772. for (int j = 0; j < grid_size; ++j) {
  8773. const int8_t * pg = (const int8_t *)(kgrid_q3xs + j);
  8774. int d2 = 0;
  8775. for (int k = 0; k < 4; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  8776. dist2[2*j+0] = d2;
  8777. dist2[2*j+1] = j;
  8778. }
  8779. qsort(dist2, grid_size, 2*sizeof(int), iq3_compare_func);
  8780. kmap_q3xs[i] = -(counter + 1);
  8781. int d2 = dist2[0];
  8782. uint16_t * start = &kneighbors_q3xs[counter++];
  8783. int n = 0, nhave = 1;
  8784. for (int j = 0; j < grid_size; ++j) {
  8785. if (dist2[2*j] > d2) {
  8786. if (nhave == nwant) break;
  8787. d2 = dist2[2*j];
  8788. ++nhave;
  8789. }
  8790. kneighbors_q3xs[counter++] = dist2[2*j+1];
  8791. ++n;
  8792. }
  8793. *start = n;
  8794. }
  8795. free(dist2);
  8796. }
  8797. void iq3xs_free_impl(int grid_size) {
  8798. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  8799. const int gindex = iq3_data_index(grid_size);
  8800. if (iq3_data[gindex].grid) {
  8801. free(iq3_data[gindex].grid); iq3_data[gindex].grid = NULL;
  8802. free(iq3_data[gindex].map); iq3_data[gindex].map = NULL;
  8803. free(iq3_data[gindex].neighbours); iq3_data[gindex].neighbours = NULL;
  8804. }
  8805. }
  8806. static int iq3_find_best_neighbour(const uint16_t * restrict neighbours, const uint32_t * restrict grid,
  8807. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  8808. int num_neighbors = neighbours[0];
  8809. GGML_ASSERT(num_neighbors > 0);
  8810. float best_d2 = FLT_MAX;
  8811. int grid_index = -1;
  8812. for (int j = 1; j <= num_neighbors; ++j) {
  8813. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  8814. float d2 = 0;
  8815. for (int i = 0; i < 4; ++i) {
  8816. float q = pg[i];
  8817. float diff = scale*q - xval[i];
  8818. d2 += weight[i]*diff*diff;
  8819. }
  8820. if (d2 < best_d2) {
  8821. best_d2 = d2; grid_index = neighbours[j];
  8822. }
  8823. }
  8824. GGML_ASSERT(grid_index >= 0);
  8825. const int8_t * pg = (const int8_t *)(grid + grid_index);
  8826. for (int i = 0; i < 4; ++i) L[i] = (pg[i] - 1)/2;
  8827. return grid_index;
  8828. }
  8829. static void quantize_row_iq3_xxs_impl(int grid_size, const float * restrict x, void * restrict vy, int n,
  8830. const float * restrict quant_weights) {
  8831. const int gindex = iq3_data_index(grid_size);
  8832. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  8833. const int * kmap_q3xs = iq3_data[gindex].map;
  8834. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  8835. //GGML_ASSERT(quant_weights && "missing quantization weights");
  8836. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  8837. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  8838. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  8839. GGML_ASSERT(n%QK_K == 0);
  8840. const int kMaxQ = 8;
  8841. const int nbl = n/QK_K;
  8842. ggml_fp16_t * dh;
  8843. uint8_t * qs;
  8844. int block_size;
  8845. if (grid_size == 256) {
  8846. block_iq3_xxs * y = vy;
  8847. dh = &y->d;
  8848. qs = y->qs;
  8849. block_size = sizeof(block_iq3_xxs);
  8850. } else {
  8851. block_iq3_s * y = vy;
  8852. dh = &y->d;
  8853. qs = y->qs;
  8854. block_size = sizeof(block_iq3_s);
  8855. }
  8856. int quant_size = block_size - sizeof(ggml_fp16_t);
  8857. float scales[QK_K/32];
  8858. float weight[32];
  8859. float xval[32];
  8860. int8_t L[32];
  8861. int8_t Laux[32];
  8862. float waux[32];
  8863. bool is_on_grid[8];
  8864. bool is_on_grid_aux[8];
  8865. uint8_t block_signs[8];
  8866. uint8_t q3[3*(QK_K/8)+QK_K/32];
  8867. uint32_t * scales_and_signs = (uint32_t *)(q3 + QK_K/4);
  8868. uint8_t * qh = q3 + 3*(QK_K/8);
  8869. for (int ibl = 0; ibl < nbl; ++ibl) {
  8870. dh[0] = GGML_FP32_TO_FP16(0.f);
  8871. memset(q3, 0, 3*QK_K/8+QK_K/32);
  8872. float max_scale = 0;
  8873. const float * xbl = x + QK_K*ibl;
  8874. float sumx2 = 0;
  8875. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  8876. float sigma2 = 2*sumx2/QK_K;
  8877. for (int ib = 0; ib < QK_K/32; ++ib) {
  8878. const float * xb = xbl + 32*ib;
  8879. if (quant_weights) {
  8880. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  8881. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  8882. } else {
  8883. for (int i = 0; i < 32; ++i) weight[i] = xb[i]*xb[i];
  8884. }
  8885. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  8886. for (int k = 0; k < 4; ++k) {
  8887. int nflip = 0;
  8888. uint8_t s = 0;
  8889. for (int i = 0; i < 8; ++i) {
  8890. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  8891. else {
  8892. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  8893. }
  8894. }
  8895. if (nflip%2) {
  8896. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  8897. for (int i = 1; i < 8; ++i) {
  8898. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  8899. if (ax < min) {
  8900. min = ax; imin = i;
  8901. }
  8902. }
  8903. xval[8*k+imin] = -xval[8*k+imin];
  8904. s ^= (1 << imin);
  8905. }
  8906. block_signs[k] = s & 127;
  8907. }
  8908. float max = xval[0];
  8909. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  8910. if (!max) {
  8911. scales[ib] = 0;
  8912. memset(L, 0, 32);
  8913. continue;
  8914. }
  8915. float best = 0;
  8916. float scale = max/(2*kMaxQ-1);
  8917. for (int is = -15; is <= 15; ++is) {
  8918. float id = (2*kMaxQ-1+is*0.2f)/max;
  8919. float this_scale = 1/id;
  8920. for (int k = 0; k < 8; ++k) {
  8921. for (int i = 0; i < 4; ++i) {
  8922. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8923. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  8924. }
  8925. uint16_t u = 0;
  8926. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  8927. int grid_index = kmap_q3xs[u];
  8928. is_on_grid_aux[k] = true;
  8929. if (grid_index < 0) {
  8930. is_on_grid_aux[k] = false;
  8931. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8932. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  8933. }
  8934. }
  8935. float sumqx = 0, sumq2 = 0;
  8936. for (int i = 0; i < 32; ++i) {
  8937. float w = weight[i];
  8938. float q = 2*Laux[i] + 1;
  8939. sumqx += w*xval[i]*q;
  8940. sumq2 += w*q*q;
  8941. }
  8942. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  8943. scale = sumqx/sumq2; best = scale*sumqx;
  8944. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  8945. for (int k = 0; k < 8; ++k) is_on_grid[k] = is_on_grid_aux[k];
  8946. }
  8947. }
  8948. int n_not_ongrid = 0;
  8949. for (int k = 0; k < 8; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  8950. if (n_not_ongrid > 0 && scale > 0) {
  8951. float id = 1/scale;
  8952. for (int k = 0; k < 8; ++k) {
  8953. if (is_on_grid[k]) continue;
  8954. uint16_t u = 0;
  8955. for (int i = 0; i < 4; ++i) {
  8956. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  8957. l = MAX(0, MIN(kMaxQ-1, l));
  8958. u |= (l << 3*i);
  8959. }
  8960. int grid_index = kmap_q3xs[u];
  8961. if (grid_index < 0) {
  8962. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  8963. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  8964. }
  8965. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  8966. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  8967. }
  8968. float sumqx = 0, sumq2 = 0;
  8969. for (int i = 0; i < 32; ++i) {
  8970. float w = weight[i];
  8971. float q = 2*L[i] + 1;
  8972. sumqx += w*xval[i]*q;
  8973. sumq2 += w*q*q;
  8974. }
  8975. if (sumq2 > 0) scale = sumqx/sumq2;
  8976. }
  8977. if (scale < 0) {
  8978. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  8979. // and correspondingly flip quant signs.
  8980. scale = -scale;
  8981. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  8982. }
  8983. for (int k = 0; k < 8; ++k) {
  8984. uint16_t u = 0;
  8985. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  8986. int grid_index = kmap_q3xs[u];
  8987. if (grid_index < 0) {
  8988. printf("Oops: found point %u not on grid:", u);
  8989. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  8990. printf("\n");
  8991. GGML_ASSERT(false);
  8992. }
  8993. if (grid_size == 256) {
  8994. q3[8*ib+k] = grid_index;
  8995. } else {
  8996. q3[8*ib+k] = grid_index & 255;
  8997. qh[ib] |= ((grid_index >> 8) << k);
  8998. }
  8999. }
  9000. scales_and_signs[ib] = block_signs[0] | (block_signs[1] << 7) | (block_signs[2] << 14) | (block_signs[3] << 21);
  9001. GGML_ASSERT(scale >= 0);
  9002. scales[ib] = scale;
  9003. max_scale = MAX(max_scale, scale);
  9004. }
  9005. if (!max_scale) {
  9006. memset(qs, 0, quant_size);
  9007. dh += block_size/sizeof(ggml_fp16_t);
  9008. qs += block_size;
  9009. continue;
  9010. }
  9011. float d = max_scale/31;
  9012. dh[0] = GGML_FP32_TO_FP16(d * 1.0125f); // small improvement via this fudge factor
  9013. float id = 1/d;
  9014. for (int ib = 0; ib < QK_K/32; ++ib) {
  9015. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9016. l = MAX(0, MIN(15, l));
  9017. scales_and_signs[ib] |= ((uint32_t)l << 28);
  9018. }
  9019. memcpy(qs, q3, quant_size);
  9020. dh += block_size/sizeof(ggml_fp16_t);
  9021. qs += block_size;
  9022. }
  9023. }
  9024. size_t quantize_iq3_xxs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9025. GGML_ASSERT(n_per_row%QK_K == 0);
  9026. int nblock = n_per_row/QK_K;
  9027. char * qrow = (char *)dst;
  9028. for (int row = 0; row < nrow; ++row) {
  9029. quantize_row_iq3_xxs_impl(256, src, qrow, n_per_row, quant_weights);
  9030. src += n_per_row;
  9031. qrow += nblock*sizeof(block_iq3_xxs);
  9032. }
  9033. return nrow * nblock * sizeof(block_iq3_xxs);
  9034. }
  9035. void quantize_row_iq3_xxs(const float * restrict x, void * restrict vy, int k) {
  9036. assert(k % QK_K == 0);
  9037. block_iq3_xxs * restrict y = vy;
  9038. quantize_row_iq3_xxs_reference(x, y, k);
  9039. }
  9040. void quantize_row_iq3_xxs_reference(const float * restrict x, block_iq3_xxs * restrict y, int k) {
  9041. assert(k % QK_K == 0);
  9042. quantize_row_iq3_xxs_impl(256, x, y, k, NULL);
  9043. }
  9044. static void quantize_row_iq3_s_impl(int block_size, const float * restrict x, void * restrict vy, int n,
  9045. const float * restrict quant_weights,
  9046. float * scales,
  9047. float * weight,
  9048. float * xval,
  9049. int8_t * L,
  9050. int8_t * Laux,
  9051. float * waux,
  9052. bool * is_on_grid,
  9053. bool * is_on_grid_aux,
  9054. uint8_t * block_signs) {
  9055. const int gindex = iq3_data_index(512);
  9056. const uint32_t * kgrid_q3xs = iq3_data[gindex].grid;
  9057. const int * kmap_q3xs = iq3_data[gindex].map;
  9058. const uint16_t * kneighbors_q3xs = iq3_data[gindex].neighbours;
  9059. //GGML_ASSERT(quant_weights && "missing quantization weights");
  9060. GGML_ASSERT(kgrid_q3xs && "forgot to call ggml_quantize_init()?");
  9061. GGML_ASSERT(kmap_q3xs && "forgot to call ggml_quantize_init()?");
  9062. GGML_ASSERT(kneighbors_q3xs && "forgot to call ggml_quantize_init()?");
  9063. GGML_ASSERT(n%QK_K == 0);
  9064. const int kMaxQ = 8;
  9065. const int nbl = n/QK_K;
  9066. block_iq3_s * y = vy;
  9067. const int bs4 = block_size/4;
  9068. const int bs8 = block_size/8;
  9069. for (int ibl = 0; ibl < nbl; ++ibl) {
  9070. memset(&y[ibl], 0, sizeof(block_iq3_s));
  9071. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9072. uint8_t * qs = y[ibl].qs;
  9073. uint8_t * qh = y[ibl].qh;
  9074. uint8_t * signs = y[ibl].signs;
  9075. float max_scale = 0;
  9076. const float * xbl = x + QK_K*ibl;
  9077. float sumx2 = 0;
  9078. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9079. float sigma2 = 2*sumx2/QK_K;
  9080. for (int ib = 0; ib < QK_K/block_size; ++ib) {
  9081. const float * xb = xbl + block_size*ib;
  9082. if (quant_weights) {
  9083. const float * qw = quant_weights + QK_K*ibl + block_size*ib;
  9084. for (int i = 0; i < block_size; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9085. } else {
  9086. for (int i = 0; i < block_size; ++i) weight[i] = xb[i]*xb[i];
  9087. }
  9088. for (int i = 0; i < block_size; ++i) waux[i] = sqrtf(weight[i]);
  9089. for (int k = 0; k < bs8; ++k) {
  9090. uint8_t s = 0;
  9091. for (int i = 0; i < 8; ++i) {
  9092. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9093. else {
  9094. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9095. }
  9096. }
  9097. block_signs[k] = s;
  9098. }
  9099. float max = xval[0];
  9100. for (int i = 1; i < block_size; ++i) max = MAX(max, xval[i]);
  9101. if (!max) {
  9102. scales[ib] = 0;
  9103. continue;
  9104. }
  9105. float best = 0;
  9106. float scale = max/(2*kMaxQ-1);
  9107. for (int k = 0; k < bs4; ++k) is_on_grid[k] = false;
  9108. for (int is = -9; is <= 9; ++is) {
  9109. float id = (2*kMaxQ-1+is*0.2f)/max;
  9110. float this_scale = 1/id;
  9111. for (int k = 0; k < bs4; ++k) {
  9112. for (int i = 0; i < 4; ++i) {
  9113. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9114. Laux[4*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9115. }
  9116. uint16_t u = 0;
  9117. for (int i = 0; i < 4; ++i) u |= (Laux[4*k+i] << 3*i);
  9118. int grid_index = kmap_q3xs[u];
  9119. is_on_grid_aux[k] = true;
  9120. if (grid_index < 0) {
  9121. is_on_grid_aux[k] = false;
  9122. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9123. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, this_scale, Laux + 4*k);
  9124. }
  9125. }
  9126. float sumqx = 0, sumq2 = 0;
  9127. for (int i = 0; i < block_size; ++i) {
  9128. float w = weight[i];
  9129. float q = 2*Laux[i] + 1;
  9130. sumqx += w*xval[i]*q;
  9131. sumq2 += w*q*q;
  9132. }
  9133. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9134. scale = sumqx/sumq2; best = scale*sumqx;
  9135. for (int i = 0; i < block_size; ++i) L[i] = Laux[i];
  9136. for (int k = 0; k < bs4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9137. }
  9138. }
  9139. int n_not_ongrid = 0;
  9140. for (int k = 0; k < bs4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9141. if (n_not_ongrid > 0 && scale > 0) {
  9142. float id = 1/scale;
  9143. for (int k = 0; k < bs4; ++k) {
  9144. //if (is_on_grid[k]) continue;
  9145. uint16_t u = 0;
  9146. for (int i = 0; i < 4; ++i) {
  9147. int l = nearest_int(0.5f*(id*xval[4*k+i]-1));
  9148. l = MAX(0, MIN(kMaxQ-1, l));
  9149. u |= (l << 3*i);
  9150. }
  9151. int grid_index = kmap_q3xs[u];
  9152. if (grid_index < 0) {
  9153. const uint16_t * neighbours = kneighbors_q3xs - kmap_q3xs[u] - 1;
  9154. grid_index = iq3_find_best_neighbour(neighbours, kgrid_q3xs, xval + 4*k, waux + 4*k, scale, L + 4*k);
  9155. }
  9156. const int8_t * pg = (const int8_t *)(kgrid_q3xs + grid_index);
  9157. for (int i = 0; i < 4; ++i) L[4*k+i] = (pg[i] - 1)/2;
  9158. }
  9159. float sumqx = 0, sumq2 = 0;
  9160. for (int i = 0; i < block_size; ++i) {
  9161. float w = weight[i];
  9162. float q = 2*L[i] + 1;
  9163. sumqx += w*xval[i]*q;
  9164. sumq2 += w*q*q;
  9165. }
  9166. if (sumq2 > 0) scale = sumqx/sumq2;
  9167. }
  9168. if (scale < 0) {
  9169. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  9170. // and correspondingly flip quant signs.
  9171. scale = -scale;
  9172. for (int k = 0; k < bs8; ++k) block_signs[k] = ~block_signs[k];
  9173. }
  9174. for (int k = 0; k < bs4; ++k) {
  9175. uint16_t u = 0;
  9176. for (int i = 0; i < 4; ++i) u |= (L[4*k+i] << 3*i);
  9177. int grid_index = kmap_q3xs[u];
  9178. if (grid_index < 0) {
  9179. printf("Oops: found point %u not on grid:", u);
  9180. for (int i = 0; i < 4; ++i) printf(" %d", L[4*k+i]);
  9181. printf("\n");
  9182. GGML_ASSERT(false);
  9183. }
  9184. qs[k] = grid_index & 255;
  9185. qh[(ib*bs4+k)/8] |= ((grid_index >> 8) << ((ib*bs4+k)%8));
  9186. }
  9187. qs += bs4;
  9188. for (int k = 0; k < bs8; ++k) signs[k] = block_signs[k];
  9189. signs += bs8;
  9190. GGML_ASSERT(scale >= 0);
  9191. scales[ib] = scale;
  9192. max_scale = MAX(max_scale, scale);
  9193. }
  9194. if (!max_scale) {
  9195. continue;
  9196. }
  9197. float d = max_scale/31;
  9198. y[ibl].d = GGML_FP32_TO_FP16(d * 1.033f);
  9199. float id = 1/d;
  9200. for (int ib = 0; ib < QK_K/block_size; ib += 2) {
  9201. int l1 = nearest_int(0.5f*(id*scales[ib+0]-1));
  9202. l1 = MAX(0, MIN(15, l1));
  9203. int l2 = nearest_int(0.5f*(id*scales[ib+1]-1));
  9204. l2 = MAX(0, MIN(15, l2));
  9205. y[ibl].scales[ib/2] = l1 | (l2 << 4);
  9206. }
  9207. }
  9208. }
  9209. #define IQ3S_BLOCK_SIZE 32
  9210. size_t quantize_iq3_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9211. GGML_ASSERT(n_per_row%QK_K == 0);
  9212. int nblock = n_per_row/QK_K;
  9213. float scales[QK_K/IQ3S_BLOCK_SIZE];
  9214. float weight[IQ3S_BLOCK_SIZE];
  9215. float xval[IQ3S_BLOCK_SIZE];
  9216. int8_t L[IQ3S_BLOCK_SIZE];
  9217. int8_t Laux[IQ3S_BLOCK_SIZE];
  9218. float waux[IQ3S_BLOCK_SIZE];
  9219. bool is_on_grid[IQ3S_BLOCK_SIZE/4];
  9220. bool is_on_grid_aux[IQ3S_BLOCK_SIZE/4];
  9221. uint8_t block_signs[IQ3S_BLOCK_SIZE/8];
  9222. char * qrow = (char *)dst;
  9223. for (int row = 0; row < nrow; ++row) {
  9224. quantize_row_iq3_s_impl(IQ3S_BLOCK_SIZE, src, qrow, n_per_row, quant_weights,
  9225. scales, weight, xval, L, Laux, waux, is_on_grid, is_on_grid_aux, block_signs);
  9226. src += n_per_row;
  9227. qrow += nblock*sizeof(block_iq3_s);
  9228. }
  9229. return nrow * nblock * sizeof(block_iq3_s);
  9230. }
  9231. void quantize_row_iq3_s(const float * restrict x, void * restrict vy, int k) {
  9232. assert(k % QK_K == 0);
  9233. block_iq3_s * restrict y = vy;
  9234. quantize_row_iq3_s_reference(x, y, k);
  9235. }
  9236. void quantize_row_iq3_s_reference(const float * restrict x, block_iq3_s * restrict y, int k) {
  9237. assert(k % QK_K == 0);
  9238. quantize_iq3_s(x, y, 1, k, NULL);
  9239. }
  9240. // =================================== 1.5 bpw ===================================================
  9241. static int iq1_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9242. const float * restrict xval, const float * restrict weight, float * scale, int8_t * restrict L, int ngrid) {
  9243. int num_neighbors = neighbours[0];
  9244. GGML_ASSERT(num_neighbors > 0);
  9245. float best_score = 0;
  9246. int grid_index = -1;
  9247. for (int j = 1; j <= num_neighbors; ++j) {
  9248. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9249. float sumqx = 0, sumq2 = 0;
  9250. for (int i = 0; i < 8; ++i) {
  9251. float q = (pg[i] - 3)/2;
  9252. float w = weight[i];
  9253. sumqx += w*q*xval[i];
  9254. sumq2 += w*q*q;
  9255. }
  9256. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9257. *scale = sumqx/sumq2; best_score = *scale * sumqx;
  9258. grid_index = neighbours[j];
  9259. }
  9260. }
  9261. if (grid_index < 0) {
  9262. for (int i = 0; i < ngrid; ++i) {
  9263. const int8_t * grid_i = (const int8_t *)(grid + i);
  9264. float sumqx = 0, sumq2 = 0;
  9265. for (int j = 0; j < 8; ++j) {
  9266. float w = weight[j];
  9267. float q = (grid_i[j] - 3)/2;
  9268. sumqx += w*q*xval[j];
  9269. sumq2 += w*q*q;
  9270. }
  9271. if (sumqx > 0 && sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9272. *scale = sumqx/sumq2; best_score = *scale*sumqx;
  9273. grid_index = i;
  9274. }
  9275. }
  9276. }
  9277. if (grid_index < 0) {
  9278. printf("Oops, did not find grid point\n");
  9279. printf("Have %d neighbours\n", num_neighbors);
  9280. for (int j = 1; j <= num_neighbors; ++j) {
  9281. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9282. float sumqx = 0, sumq2 = 0;
  9283. for (int i = 0; i < 8; ++i) {
  9284. float q = (pg[i] - 3)/2;
  9285. float w = weight[i];
  9286. sumqx += w*q*xval[i];
  9287. sumq2 += w*q*q;
  9288. }
  9289. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9290. }
  9291. }
  9292. GGML_ASSERT(grid_index >= 0);
  9293. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9294. *scale *= 1.05f; // This is a fudge factor. Don't ask me why it improves the result.
  9295. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9296. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9297. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9298. return grid_index;
  9299. }
  9300. static int iq1_find_best_neighbour2(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  9301. const float * restrict xval, const float * restrict weight, float scale, const float * restrict xg, int8_t * restrict L, int ngrid) {
  9302. int num_neighbors = neighbours[0];
  9303. GGML_ASSERT(num_neighbors > 0);
  9304. float best_score = FLT_MAX;
  9305. int grid_index = -1;
  9306. for (int j = 1; j <= num_neighbors; ++j) {
  9307. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9308. float d2 = 0;
  9309. for (int i = 0; i < 8; ++i) {
  9310. float q = xg[(pg[i] - 1)/2];
  9311. float w = weight[i];
  9312. float diff = scale*q - xval[i];
  9313. d2 += w*diff*diff;
  9314. }
  9315. if (d2 < best_score) {
  9316. best_score = d2;
  9317. grid_index = neighbours[j];
  9318. }
  9319. }
  9320. if (grid_index < 0) {
  9321. for (int i = 0; i < ngrid; ++i) {
  9322. const int8_t * grid_i = (const int8_t *)(grid + i);
  9323. float d2 = 0;
  9324. for (int j = 0; j < 8; ++j) {
  9325. float w = weight[j];
  9326. float q = xg[(grid_i[j] - 1)/2];
  9327. float diff = scale*q - xval[i];
  9328. d2 += w*diff*diff;
  9329. }
  9330. if (d2 < best_score) {
  9331. best_score = d2;
  9332. grid_index = i;
  9333. }
  9334. }
  9335. }
  9336. if (grid_index < 0) {
  9337. printf("Oops, did not find grid point\n");
  9338. printf("Have %d neighbours\n", num_neighbors);
  9339. for (int j = 1; j <= num_neighbors; ++j) {
  9340. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  9341. float sumqx = 0, sumq2 = 0;
  9342. for (int i = 0; i < 8; ++i) {
  9343. float q = xg[(pg[i] - 1)/2];
  9344. float w = weight[i];
  9345. sumqx += w*q*xval[i];
  9346. sumq2 += w*q*q;
  9347. }
  9348. printf(" neighbour %d: sumqx = %g sumq2 = %g\n", j, (double)sumqx, (double)sumq2);
  9349. }
  9350. }
  9351. GGML_ASSERT(grid_index >= 0);
  9352. const int8_t * pg = (const int8_t *)(grid + grid_index);
  9353. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  9354. return grid_index;
  9355. }
  9356. static int iq1_sort_helper(const void * left, const void * right) {
  9357. const float * l = left;
  9358. const float * r = right;
  9359. return *l < *r ? -1 : *l > *r ? 1 : 0;
  9360. }
  9361. #define IQ1S_BLOCK_SIZE 32
  9362. static void quantize_row_iq1_s_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  9363. const int gindex = iq2_data_index(GGML_TYPE_IQ1_S);
  9364. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9365. const int * kmap_q2xs = iq2_data[gindex].map;
  9366. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9367. GGML_ASSERT(quant_weights && "missing quantization weights");
  9368. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9369. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9370. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9371. GGML_ASSERT(n%QK_K == 0);
  9372. const int nbl = n/QK_K;
  9373. block_iq1_s * y = vy;
  9374. const float x_p[3] = {-1 + IQ1S_DELTA, IQ1S_DELTA, 1 + IQ1S_DELTA};
  9375. const float x_m[3] = {-1 - IQ1S_DELTA, -IQ1S_DELTA, 1 - IQ1S_DELTA};
  9376. float scales[QK_K/IQ1S_BLOCK_SIZE];
  9377. float weight[IQ1S_BLOCK_SIZE];
  9378. int8_t L[IQ1S_BLOCK_SIZE];
  9379. float sumx[IQ1S_BLOCK_SIZE+1];
  9380. float sumw[IQ1S_BLOCK_SIZE+1];
  9381. float pairs[2*IQ1S_BLOCK_SIZE];
  9382. int * idx = (int *)(pairs + 1);
  9383. uint16_t index[IQ1S_BLOCK_SIZE/8];
  9384. int8_t shifts[QK_K/IQ1S_BLOCK_SIZE];
  9385. for (int ibl = 0; ibl < nbl; ++ibl) {
  9386. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9387. memset(y[ibl].qs, 0, QK_K/8);
  9388. memset(y[ibl].qh, 0, QK_K/16);
  9389. float max_scale = 0;
  9390. const float * xbl = x + QK_K*ibl;
  9391. float sumx2 = 0;
  9392. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9393. float sigma2 = 2*sumx2/QK_K;
  9394. for (int ib = 0; ib < QK_K/IQ1S_BLOCK_SIZE; ++ib) {
  9395. const float * xb = xbl + IQ1S_BLOCK_SIZE*ib;
  9396. const float * qw = quant_weights + QK_K*ibl + IQ1S_BLOCK_SIZE*ib;
  9397. for (int i = 0; i < IQ1S_BLOCK_SIZE; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9398. float max = fabsf(xb[0]);
  9399. for (int i = 1; i < IQ1S_BLOCK_SIZE; ++i) max = MAX(max, fabsf(xb[i]));
  9400. if (!max) {
  9401. scales[ib] = 0;
  9402. memset(L, 1, IQ1S_BLOCK_SIZE);
  9403. continue;
  9404. }
  9405. // Here we solve exactly the sum of squared difference (SSD) weighted minimization problem.
  9406. // With just 3 allowed quant values (-1, 0, 1), we can search exhaustively for the two
  9407. // boundaries that split the weights xb[i] into 3 groups. To do so, we sort the weights
  9408. // in ascending order, compute Si = sum[weight[j] xb[j], j = 0...i] and
  9409. // Wi = sum[weight[j], j = 0...i], and use these to quckly get get the optimum scale
  9410. // for each possible and score for each split.
  9411. for (int j = 0; j < IQ1S_BLOCK_SIZE; ++j) {
  9412. pairs[2*j] = xb[j];
  9413. idx[2*j] = j;
  9414. }
  9415. qsort(pairs, IQ1S_BLOCK_SIZE, 2*sizeof(float), iq1_sort_helper);
  9416. {
  9417. sumx[0] = sumw[0] = 0;
  9418. for (int j = 0; j < IQ1S_BLOCK_SIZE; ++j) {
  9419. int i = idx[2*j];
  9420. sumx[j+1] = sumx[j] + weight[i]*xb[i];
  9421. sumw[j+1] = sumw[j] + weight[i];
  9422. }
  9423. }
  9424. float best_score = 0, scale = max;
  9425. int besti1 = -1, besti2 = -1, best_shift = 0;
  9426. for (int i1 = 0; i1 <= IQ1S_BLOCK_SIZE; ++i1) {
  9427. for (int i2 = i1; i2 <= IQ1S_BLOCK_SIZE; ++i2) {
  9428. float sumqx = (sumx[i1] - sumx[0])*x_p[0] + (sumx[i2] - sumx[i1])*x_p[1] + (sumx[IQ1S_BLOCK_SIZE] - sumx[i2])*x_p[2];
  9429. float sumq2 = (sumw[i1] - sumw[0])*x_p[0]*x_p[0] + (sumw[i2] - sumw[i1])*x_p[1]*x_p[1] + (sumw[IQ1S_BLOCK_SIZE] - sumw[i2])*x_p[2]*x_p[2];
  9430. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9431. scale = sumqx/sumq2; best_score = scale*sumqx;
  9432. besti1 = i1; besti2 = i2; best_shift = 1;
  9433. }
  9434. sumqx = (sumx[i1] - sumx[0])*x_m[0] + (sumx[i2] - sumx[i1])*x_m[1] + (sumx[IQ1S_BLOCK_SIZE] - sumx[i2])*x_m[2];
  9435. sumq2 = (sumw[i1] - sumw[0])*x_m[0]*x_m[0] + (sumw[i2] - sumw[i1])*x_m[1]*x_m[1] + (sumw[IQ1S_BLOCK_SIZE] - sumw[i2])*x_m[2]*x_m[2];
  9436. if (sumq2 > 0 && sumqx*sumqx > best_score*sumq2) {
  9437. scale = sumqx/sumq2; best_score = scale*sumqx;
  9438. besti1 = i1; besti2 = i2; best_shift = -1;
  9439. }
  9440. }
  9441. }
  9442. GGML_ASSERT(besti1 >= 0 && besti2 >= 0 && best_shift != 0);
  9443. for (int j = 0; j < besti1; ++j) L[idx[2*j]] = 0;
  9444. for (int j = besti1; j < besti2; ++j) L[idx[2*j]] = 1;
  9445. for (int j = besti2; j < IQ1S_BLOCK_SIZE; ++j) L[idx[2*j]] = 2;
  9446. if (scale < 0) {
  9447. for (int j = 0; j < IQ1S_BLOCK_SIZE; ++j) L[j] = 2 - L[j];
  9448. scale = -scale; best_shift = -best_shift;
  9449. }
  9450. bool all_on_grid = true;
  9451. const float * xx = best_shift == 1 ? x_p : x_m;
  9452. for (int k = 0; k < IQ1S_BLOCK_SIZE/8; ++k) {
  9453. uint16_t u = 0;
  9454. for (int j = 0; j < 8; ++j) u |= (L[8*k+j] << 2*j);
  9455. int grid_index = kmap_q2xs[u];
  9456. if (grid_index < 0) {
  9457. all_on_grid = false;
  9458. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9459. grid_index = iq1_find_best_neighbour2(neighbours, kgrid_q2xs, xb + 8*k, weight + 8*k, scale, xx, L + 8*k, NGRID_IQ1S);
  9460. GGML_ASSERT(grid_index >= 0);
  9461. }
  9462. index[k] = grid_index;
  9463. }
  9464. if (!all_on_grid) {
  9465. float sumqx = 0, sumq2 = 0;
  9466. for (int k = 0; k < IQ1S_BLOCK_SIZE/8; ++k) {
  9467. const int8_t * pg = (const int8_t *)(kgrid_q2xs + index[k]);
  9468. for (int j = 0; j < 8; ++j) {
  9469. float w = weight[8*k + j];
  9470. float q = xx[(pg[j] - 1)/2];
  9471. sumqx += w*q*xb[8*k+j];
  9472. sumq2 += w*q*q;
  9473. }
  9474. }
  9475. if (sumqx > 0 && sumq2 > 0) scale = sumqx/sumq2;
  9476. }
  9477. uint16_t h = 0;
  9478. for (int k = 0; k < IQ1S_BLOCK_SIZE/8; ++k) {
  9479. y[ibl].qs[(IQ1S_BLOCK_SIZE/8)*ib + k] = index[k] & 255;
  9480. h |= (index[k] >> 8) << 3*k;
  9481. }
  9482. y[ibl].qh[ib] = h;
  9483. GGML_ASSERT(scale >= 0);
  9484. scales[ib] = scale;
  9485. shifts[ib] = best_shift;
  9486. max_scale = MAX(max_scale, scale);
  9487. }
  9488. if (!max_scale) {
  9489. memset(y[ibl].qs, 0, QK_K/8);
  9490. continue;
  9491. }
  9492. float d = max_scale/15;
  9493. y[ibl].d = GGML_FP32_TO_FP16(d*1.125f); // 1.085f is another fudge factor. Don't ask me why it is needed.
  9494. float id = 1/d;
  9495. for (int ib = 0; ib < QK_K/IQ1S_BLOCK_SIZE; ++ib) {
  9496. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9497. l = MAX(0, MIN(7, l));
  9498. if (shifts[ib] == -1) l |= 8;
  9499. y[ibl].qh[ib] |= (l << 12);
  9500. }
  9501. }
  9502. }
  9503. size_t quantize_iq1_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9504. GGML_ASSERT(n_per_row%QK_K == 0);
  9505. int nblock = n_per_row/QK_K;
  9506. char * qrow = (char *)dst;
  9507. for (int row = 0; row < nrow; ++row) {
  9508. quantize_row_iq1_s_impl(src, qrow, n_per_row, quant_weights);
  9509. src += n_per_row;
  9510. qrow += nblock*sizeof(block_iq1_s);
  9511. }
  9512. return nrow * nblock * sizeof(block_iq1_s);
  9513. }
  9514. // ============================ 4-bit non-linear quants
  9515. static inline int best_index_int8(int n, const int8_t * val, float x) {
  9516. if (x <= val[0]) return 0;
  9517. if (x >= val[n-1]) return n-1;
  9518. int ml = 0, mu = n-1;
  9519. while (mu-ml > 1) {
  9520. int mav = (ml+mu)/2;
  9521. if (x < val[mav]) mu = mav; else ml = mav;
  9522. }
  9523. return x - val[mu-1] < val[mu] - x ? mu-1 : mu;
  9524. }
  9525. static void quantize_row_iq4_nl_impl(const int super_block_size, const int block_size, const float * restrict x,
  9526. ggml_fp16_t * dh, uint8_t * q4, uint16_t * scales_h, uint8_t * scales_l,
  9527. float * scales, float * weight, uint8_t * L,
  9528. const int8_t * values,
  9529. const float * quant_weights) {
  9530. const int ntry = 7;
  9531. float sigma2 = 0;
  9532. for (int j = 0; j < super_block_size; ++j) sigma2 += x[j]*x[j];
  9533. sigma2 *= 2.f/super_block_size;
  9534. memset(q4, 0, super_block_size/2);
  9535. dh[0] = GGML_FP32_TO_FP16(0.f);
  9536. float max_scale = 0, amax_scale = 0;
  9537. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9538. const float * xb = x + ib*block_size;
  9539. if (quant_weights) {
  9540. const float * qw = quant_weights + ib*block_size;
  9541. for (int j = 0; j < block_size; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  9542. } else {
  9543. for (int j = 0; j < block_size; ++j) weight[j] = xb[j]*xb[j];
  9544. }
  9545. float amax = 0, max = 0;
  9546. for (int j = 0; j < block_size; ++j) {
  9547. float ax = fabsf(xb[j]);
  9548. if (ax > amax) {
  9549. amax = ax; max = xb[j];
  9550. }
  9551. }
  9552. if (!amax) {
  9553. scales[ib] = 0;
  9554. continue;
  9555. }
  9556. float d = -max/values[0];
  9557. float id = 1/d;
  9558. float sumqx = 0, sumq2 = 0;
  9559. for (int j = 0; j < block_size; ++j) {
  9560. float al = id*xb[j];
  9561. int l = best_index_int8(16, values, al);
  9562. float q = values[l];
  9563. float w = weight[j];
  9564. sumqx += w*q*xb[j];
  9565. sumq2 += w*q*q;
  9566. }
  9567. d = sumqx/sumq2;
  9568. float best = d*sumqx;
  9569. for (int itry = -ntry; itry <= ntry; ++itry) {
  9570. id = (itry + values[0])/max;
  9571. sumqx = sumq2 = 0;
  9572. for (int j = 0; j < block_size; ++j) {
  9573. float al = id*xb[j];
  9574. int l = best_index_int8(16, values, al);
  9575. float q = values[l];
  9576. float w = weight[j];
  9577. sumqx += w*q*xb[j];
  9578. sumq2 += w*q*q;
  9579. }
  9580. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9581. d = sumqx/sumq2; best = d * sumqx;
  9582. }
  9583. }
  9584. scales[ib] = d;
  9585. float abs_d = fabsf(d);
  9586. if (abs_d > amax_scale) {
  9587. amax_scale = abs_d; max_scale = d;
  9588. }
  9589. }
  9590. if (super_block_size/block_size > 1) {
  9591. int nb = super_block_size/block_size;
  9592. memset(scales_h, 0, ((nb+7)/8)*sizeof(uint16_t));
  9593. float d = -max_scale/32;
  9594. dh[0] = GGML_FP32_TO_FP16(d);
  9595. float id = d ? 1/d : 0.f;
  9596. for (int ib = 0; ib < super_block_size/block_size; ++ib) {
  9597. int l = nearest_int(id*scales[ib]);
  9598. l = MAX(-32, MIN(31, l));
  9599. float dl = d * l;
  9600. float idl = dl ? 1/dl : 0.f;
  9601. uint8_t * Lb = L + ib*block_size;
  9602. const float * xb = x + ib*block_size;
  9603. for (int j = 0; j < block_size; ++j) {
  9604. Lb[j] = best_index_int8(16, values, idl*xb[j]);
  9605. }
  9606. l += 32;
  9607. uint8_t l_l = l & 0xf;
  9608. uint8_t l_h = l >> 4;
  9609. if (ib%2 == 0) scales_l[ib/2] = l_l;
  9610. else scales_l[ib/2] |= (l_l << 4);
  9611. scales_h[ib/8] |= (l_h << 2*(ib%8));
  9612. }
  9613. } else {
  9614. dh[0] = GGML_FP32_TO_FP16(scales[0]);
  9615. float id = scales[0] ? 1/scales[0] : 0;
  9616. for (int j = 0; j < super_block_size; ++j) {
  9617. L[j] = best_index_int8(16, values, id*x[j]);
  9618. }
  9619. }
  9620. for (int i = 0; i < super_block_size/32; ++i) {
  9621. for (int j = 0; j < 16; ++j) {
  9622. q4[16*i + j] = L[32*i + j] | (L[32*i + 16 + j] << 4);
  9623. }
  9624. }
  9625. }
  9626. size_t quantize_iq4_nl(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9627. GGML_ASSERT(n_per_row%QK4_NL == 0);
  9628. int nblock = n_per_row/QK4_NL;
  9629. char * qrow = (char *)dst;
  9630. uint8_t L[QK4_NL];
  9631. float weight[QK4_NL];
  9632. uint16_t unused_h;
  9633. uint8_t * unused_l = NULL;
  9634. float scale;
  9635. for (int row = 0; row < nrow; ++row) {
  9636. block_iq4_nl * iq4 = (block_iq4_nl *)qrow;
  9637. for (int ibl = 0; ibl < nblock; ++ibl) {
  9638. const float * qw = quant_weights ? quant_weights + QK4_NL*ibl : NULL;
  9639. quantize_row_iq4_nl_impl(QK4_NL, 32, src + QK4_NL*ibl, &iq4[ibl].d, iq4[ibl].qs, &unused_h, unused_l,
  9640. &scale, weight, L, kvalues_iq4nl, qw);
  9641. }
  9642. src += n_per_row;
  9643. qrow += nblock*sizeof(block_iq4_nl);
  9644. }
  9645. return nrow * nblock * sizeof(block_iq4_nl);
  9646. }
  9647. void quantize_row_iq4_nl(const float * restrict x, void * restrict vy, int k) {
  9648. assert(k % QK4_NL == 0);
  9649. block_iq4_nl * restrict y = vy;
  9650. quantize_row_iq4_nl_reference(x, y, k);
  9651. }
  9652. void quantize_row_iq4_nl_reference(const float * restrict x, block_iq4_nl * restrict y, int k) {
  9653. assert(k % QK4_NL == 0);
  9654. quantize_iq4_nl(x, y, 1, k, NULL);
  9655. }
  9656. size_t quantize_iq4_xs(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9657. #if QK_K == 64
  9658. return quantize_iq4_nl(src, dst, nrow, n_per_row, quant_weights);
  9659. #else
  9660. GGML_ASSERT(n_per_row%QK_K == 0);
  9661. int nblock = n_per_row/QK_K;
  9662. char * qrow = (char *)dst;
  9663. uint8_t L[QK_K];
  9664. float weight[32];
  9665. float scales[QK_K/32];
  9666. for (int row = 0; row < nrow; ++row) {
  9667. block_iq4_xs * iq4 = (block_iq4_xs *)qrow;
  9668. for (int ibl = 0; ibl < nblock; ++ibl) {
  9669. const float * qw = quant_weights ? quant_weights + QK_K*ibl : NULL;
  9670. 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,
  9671. scales, weight, L, kvalues_iq4nl, qw);
  9672. }
  9673. src += n_per_row;
  9674. qrow += nblock*sizeof(block_iq4_xs);
  9675. }
  9676. return nrow * nblock * sizeof(block_iq4_xs);
  9677. #endif
  9678. }
  9679. void quantize_row_iq4_xs(const float * restrict x, void * restrict vy, int k) {
  9680. assert(k % QK_K == 0);
  9681. block_iq4_xs * restrict y = vy;
  9682. quantize_row_iq4_xs_reference(x, y, k);
  9683. }
  9684. void quantize_row_iq4_xs_reference(const float * restrict x, block_iq4_xs * restrict y, int k) {
  9685. assert(k % QK_K == 0);
  9686. quantize_iq4_xs(x, y, 1, k, NULL);
  9687. }
  9688. // =============================== 2.5625 bpw
  9689. static void quantize_row_iq2_s_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  9690. const int gindex = iq2_data_index(GGML_TYPE_IQ2_S);
  9691. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  9692. const int * kmap_q2xs = iq2_data[gindex].map;
  9693. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  9694. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  9695. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  9696. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  9697. GGML_ASSERT(n%QK_K == 0);
  9698. const int kMaxQ = 3;
  9699. const int nbl = n/QK_K;
  9700. block_iq2_s * y = vy;
  9701. float scales[QK_K/16];
  9702. float weight[16];
  9703. float xval[16];
  9704. int8_t L[16];
  9705. int8_t Laux[16];
  9706. float waux[16];
  9707. bool is_on_grid[2];
  9708. bool is_on_grid_aux[2];
  9709. uint8_t block_signs[2];
  9710. for (int ibl = 0; ibl < nbl; ++ibl) {
  9711. memset(&y[ibl], 0, sizeof(block_iq2_s));
  9712. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  9713. float max_scale = 0;
  9714. const float * xbl = x + QK_K*ibl;
  9715. float sumx2 = 0;
  9716. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  9717. float sigma2 = 2*sumx2/QK_K;
  9718. for (int ib = 0; ib < QK_K/16; ++ib) {
  9719. const float * xb = xbl + 16*ib;
  9720. if (quant_weights) {
  9721. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  9722. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  9723. } else {
  9724. for (int i = 0; i < 16; ++i) weight[i] = 0.25f*sigma2 + xb[i]*xb[i];
  9725. }
  9726. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  9727. for (int k = 0; k < 2; ++k) {
  9728. uint8_t s = 0;
  9729. for (int i = 0; i < 8; ++i) {
  9730. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  9731. else {
  9732. xval[8*k + i] = -xb[8*k + i]; s |= (1 << i);
  9733. }
  9734. }
  9735. block_signs[k] = s;
  9736. }
  9737. float max = xval[0];
  9738. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  9739. if (!max) {
  9740. scales[ib] = 0;
  9741. continue;
  9742. }
  9743. float best = 0;
  9744. float scale = max/(2*kMaxQ-1);
  9745. is_on_grid[0] = is_on_grid[1] = true;
  9746. for (int is = -9; is <= 9; ++is) {
  9747. float id = (2*kMaxQ-1+is*0.1f)/max;
  9748. float this_scale = 1/id;
  9749. for (int k = 0; k < 2; ++k) {
  9750. for (int i = 0; i < 8; ++i) {
  9751. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  9752. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  9753. }
  9754. uint16_t u = 0;
  9755. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  9756. int grid_index = kmap_q2xs[u];
  9757. is_on_grid_aux[k] = true;
  9758. if (grid_index < 0) {
  9759. is_on_grid_aux[k] = false;
  9760. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9761. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  9762. }
  9763. }
  9764. float sumqx = 0, sumq2 = 0;
  9765. for (int i = 0; i < 16; ++i) {
  9766. float w = weight[i];
  9767. float q = 2*Laux[i] + 1;
  9768. sumqx += w*xval[i]*q;
  9769. sumq2 += w*q*q;
  9770. }
  9771. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  9772. scale = sumqx/sumq2; best = scale*sumqx;
  9773. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  9774. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  9775. }
  9776. }
  9777. int n_not_ongrid = 0;
  9778. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  9779. if (n_not_ongrid > 0 && scale > 0) {
  9780. float id = 1/scale;
  9781. for (int k = 0; k < 2; ++k) {
  9782. if (is_on_grid[k]) continue;
  9783. uint16_t u = 0;
  9784. for (int i = 0; i < 8; ++i) {
  9785. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  9786. l = MAX(0, MIN(kMaxQ-1, l));
  9787. u |= (l << 2*i);
  9788. L[8*k + i] = l;
  9789. }
  9790. int grid_index = kmap_q2xs[u];
  9791. if (grid_index < 0) {
  9792. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  9793. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  9794. }
  9795. }
  9796. float sumqx = 0, sumq2 = 0;
  9797. for (int i = 0; i < 16; ++i) {
  9798. float w = weight[i];
  9799. float q = 2*L[i] + 1;
  9800. sumqx += w*xval[i]*q;
  9801. sumq2 += w*q*q;
  9802. }
  9803. if (sumq2 > 0) scale = sumqx/sumq2;
  9804. }
  9805. if (scale < 0) {
  9806. scale = -scale;
  9807. for (int k = 0; k < 2; ++k) block_signs[k] = ~block_signs[k];
  9808. }
  9809. for (int k = 0; k < 2; ++k) {
  9810. uint16_t u = 0;
  9811. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  9812. int grid_index = kmap_q2xs[u];
  9813. if (grid_index < 0) {
  9814. printf("Oops: found point %u not on grid:", u);
  9815. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  9816. printf("\n");
  9817. GGML_ASSERT(false);
  9818. }
  9819. const int i8 = 2*ib + k;
  9820. y[ibl].qs[i8] = grid_index & 255;
  9821. y[ibl].qh[i8/4] |= ((grid_index >> 8) << 2*(i8%4));
  9822. y[ibl].qs[QK_K/8 + i8] = block_signs[k];
  9823. }
  9824. GGML_ASSERT(scale >= 0);
  9825. scales[ib] = scale;
  9826. max_scale = MAX(max_scale, scale);
  9827. }
  9828. if (!max_scale) {
  9829. continue;
  9830. }
  9831. float d = max_scale/31;
  9832. y[ibl].d = GGML_FP32_TO_FP16(d * 0.9875f);
  9833. float id = 1/d;
  9834. for (int ib = 0; ib < QK_K/16; ++ib) {
  9835. int l = nearest_int(0.5f*(id*scales[ib]-1));
  9836. l = MAX(0, MIN(15, l));
  9837. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  9838. else y[ibl].scales[ib/2] |= (l << 4);
  9839. }
  9840. }
  9841. }
  9842. size_t quantize_iq2_s(const float * restrict src, void * restrict dst, int nrow, int n_per_row, const float * quant_weights) {
  9843. GGML_ASSERT(n_per_row%QK_K == 0);
  9844. int nblock = n_per_row/QK_K;
  9845. char * qrow = (char *)dst;
  9846. for (int row = 0; row < nrow; ++row) {
  9847. quantize_row_iq2_s_impl(src, qrow, n_per_row, quant_weights);
  9848. src += n_per_row;
  9849. qrow += nblock*sizeof(block_iq2_s);
  9850. }
  9851. return nrow * nblock * sizeof(block_iq2_s);
  9852. }
  9853. void quantize_row_iq2_s_reference(const float * restrict x, block_iq2_s * restrict y, int k) {
  9854. assert(k % QK_K == 0);
  9855. quantize_iq2_s(x, y, 1, k, NULL);
  9856. }
  9857. void quantize_row_iq2_s(const float * restrict x, void * restrict vy, int k) {
  9858. assert(k % QK_K == 0);
  9859. block_iq2_s * restrict y = vy;
  9860. quantize_row_iq2_s_reference(x, y, k);
  9861. }