ggml-quants.c 360 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191
  1. #include "ggml-quants.h"
  2. #include "ggml-impl.h"
  3. #include <math.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <float.h>
  7. #include <stdlib.h> // for qsort
  8. #include <stdio.h> // for GGML_ASSERT
  9. #ifdef __ARM_NEON
  10. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  11. //
  12. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  13. //
  14. #include <arm_neon.h>
  15. #else
  16. #ifdef __wasm_simd128__
  17. #include <wasm_simd128.h>
  18. #else
  19. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  20. #include <altivec.h>
  21. #undef bool
  22. #define bool _Bool
  23. #else
  24. #if defined(_MSC_VER) || defined(__MINGW32__)
  25. #include <intrin.h>
  26. #else
  27. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  28. #if !defined(__riscv)
  29. #include <immintrin.h>
  30. #endif
  31. #endif
  32. #endif
  33. #endif
  34. #endif
  35. #endif
  36. #ifdef __riscv_v_intrinsic
  37. #include <riscv_vector.h>
  38. #endif
  39. #undef MIN
  40. #undef MAX
  41. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  42. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  43. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  44. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  45. // multiply int8_t, add results pairwise twice
  46. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  47. // Get absolute values of x vectors
  48. const __m128i ax = _mm_sign_epi8(x, x);
  49. // Sign the values of the y vectors
  50. const __m128i sy = _mm_sign_epi8(y, x);
  51. // Perform multiplication and create 16-bit values
  52. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  53. const __m128i ones = _mm_set1_epi16(1);
  54. return _mm_madd_epi16(ones, dot);
  55. }
  56. #if __AVX__ || __AVX2__ || __AVX512F__
  57. // horizontally add 8 floats
  58. static inline float hsum_float_8(const __m256 x) {
  59. __m128 res = _mm256_extractf128_ps(x, 1);
  60. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  61. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  62. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  63. return _mm_cvtss_f32(res);
  64. }
  65. // horizontally add 8 int32_t
  66. static inline int hsum_i32_8(const __m256i a) {
  67. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  68. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  69. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  70. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  71. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  72. }
  73. // horizontally add 4 int32_t
  74. static inline int hsum_i32_4(const __m128i a) {
  75. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  76. const __m128i sum64 = _mm_add_epi32(hi64, a);
  77. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  78. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  79. }
  80. #if defined(__AVX2__) || defined(__AVX512F__)
  81. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  82. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  83. uint32_t x32;
  84. memcpy(&x32, x, sizeof(uint32_t));
  85. const __m256i shuf_mask = _mm256_set_epi64x(
  86. 0x0303030303030303, 0x0202020202020202,
  87. 0x0101010101010101, 0x0000000000000000);
  88. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  89. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  90. bytes = _mm256_or_si256(bytes, bit_mask);
  91. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  92. }
  93. // Unpack 32 4-bit fields into 32 bytes
  94. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  95. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  96. {
  97. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  98. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  99. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  100. return _mm256_and_si256(lowMask, bytes);
  101. }
  102. // add int16_t pairwise and return as float vector
  103. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  104. const __m256i ones = _mm256_set1_epi16(1);
  105. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  106. return _mm256_cvtepi32_ps(summed_pairs);
  107. }
  108. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  109. #if __AVXVNNI__
  110. const __m256i zero = _mm256_setzero_si256();
  111. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  112. return _mm256_cvtepi32_ps(summed_pairs);
  113. #else
  114. // Perform multiplication and create 16-bit values
  115. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  116. return sum_i16_pairs_float(dot);
  117. #endif
  118. }
  119. // multiply int8_t, add results pairwise twice and return as float vector
  120. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  121. #if __AVXVNNIINT8__
  122. const __m256i zero = _mm256_setzero_si256();
  123. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  124. return _mm256_cvtepi32_ps(summed_pairs);
  125. #else
  126. // Get absolute values of x vectors
  127. const __m256i ax = _mm256_sign_epi8(x, x);
  128. // Sign the values of the y vectors
  129. const __m256i sy = _mm256_sign_epi8(y, x);
  130. return mul_sum_us8_pairs_float(ax, sy);
  131. #endif
  132. }
  133. static inline __m128i packNibbles( __m256i bytes )
  134. {
  135. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  136. #if __AVX512F__
  137. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  138. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  139. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  140. #else
  141. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  142. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  143. __m256i low = _mm256_and_si256( lowByte, bytes );
  144. high = _mm256_srli_epi16( high, 4 );
  145. bytes = _mm256_or_si256( low, high );
  146. // Compress uint16_t lanes into bytes
  147. __m128i r0 = _mm256_castsi256_si128( bytes );
  148. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  149. return _mm_packus_epi16( r0, r1 );
  150. #endif
  151. }
  152. #elif defined(__AVX__)
  153. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  154. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  155. uint32_t x32;
  156. memcpy(&x32, x, sizeof(uint32_t));
  157. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  158. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  159. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  160. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  161. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  162. bytesl = _mm_or_si128(bytesl, bit_mask);
  163. bytesh = _mm_or_si128(bytesh, bit_mask);
  164. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  165. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  166. return MM256_SET_M128I(bytesh, bytesl);
  167. }
  168. // Unpack 32 4-bit fields into 32 bytes
  169. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  170. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  171. {
  172. // Load 16 bytes from memory
  173. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  174. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  175. const __m128i lowMask = _mm_set1_epi8(0xF);
  176. tmpl = _mm_and_si128(lowMask, tmpl);
  177. tmph = _mm_and_si128(lowMask, tmph);
  178. return MM256_SET_M128I(tmph, tmpl);
  179. }
  180. // add int16_t pairwise and return as float vector
  181. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  182. const __m128i ones = _mm_set1_epi16(1);
  183. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  184. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  185. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  186. return _mm256_cvtepi32_ps(summed_pairs);
  187. }
  188. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  189. const __m128i axl = _mm256_castsi256_si128(ax);
  190. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  191. const __m128i syl = _mm256_castsi256_si128(sy);
  192. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  193. // Perform multiplication and create 16-bit values
  194. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  195. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  196. return sum_i16_pairs_float(doth, dotl);
  197. }
  198. // multiply int8_t, add results pairwise twice and return as float vector
  199. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  200. const __m128i xl = _mm256_castsi256_si128(x);
  201. const __m128i xh = _mm256_extractf128_si256(x, 1);
  202. const __m128i yl = _mm256_castsi256_si128(y);
  203. const __m128i yh = _mm256_extractf128_si256(y, 1);
  204. // Get absolute values of x vectors
  205. const __m128i axl = _mm_sign_epi8(xl, xl);
  206. const __m128i axh = _mm_sign_epi8(xh, xh);
  207. // Sign the values of the y vectors
  208. const __m128i syl = _mm_sign_epi8(yl, xl);
  209. const __m128i syh = _mm_sign_epi8(yh, xh);
  210. // Perform multiplication and create 16-bit values
  211. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  212. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  213. return sum_i16_pairs_float(doth, dotl);
  214. }
  215. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  216. {
  217. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  218. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  219. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  220. __m128i low = _mm_and_si128( lowByte, bytes1 );
  221. high = _mm_srli_epi16( high, 4 );
  222. bytes1 = _mm_or_si128( low, high );
  223. high = _mm_andnot_si128( lowByte, bytes2 );
  224. low = _mm_and_si128( lowByte, bytes2 );
  225. high = _mm_srli_epi16( high, 4 );
  226. bytes2 = _mm_or_si128( low, high );
  227. return _mm_packus_epi16( bytes1, bytes2);
  228. }
  229. #endif
  230. #elif defined(__SSSE3__)
  231. // horizontally add 4x4 floats
  232. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  233. __m128 res_0 =_mm_hadd_ps(a, b);
  234. __m128 res_1 =_mm_hadd_ps(c, d);
  235. __m128 res =_mm_hadd_ps(res_0, res_1);
  236. res =_mm_hadd_ps(res, res);
  237. res =_mm_hadd_ps(res, res);
  238. return _mm_cvtss_f32(res);
  239. }
  240. #endif // __AVX__ || __AVX2__ || __AVX512F__
  241. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  242. #if defined(__ARM_NEON)
  243. #if !defined(__aarch64__)
  244. // 64-bit compatibility
  245. // vaddvq_s16
  246. // vpaddq_s16
  247. // vpaddq_s32
  248. // vaddvq_s32
  249. // vaddvq_f32
  250. // vmaxvq_f32
  251. // vcvtnq_s32_f32
  252. // vzip1_u8
  253. // vzip2_u8
  254. inline static int32_t vaddvq_s16(int16x8_t v) {
  255. return
  256. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  257. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  258. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  259. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  260. }
  261. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  262. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  263. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  264. return vcombine_s16(a0, b0);
  265. }
  266. inline static int32x4_t vpaddq_s32(int32x4_t a, int32x4_t b) {
  267. int32x2_t a0 = vpadd_s32(vget_low_s32(a), vget_high_s32(a));
  268. int32x2_t b0 = vpadd_s32(vget_low_s32(b), vget_high_s32(b));
  269. return vcombine_s32(a0, b0);
  270. }
  271. inline static int32_t vaddvq_s32(int32x4_t v) {
  272. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  273. }
  274. inline static float vaddvq_f32(float32x4_t v) {
  275. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  276. }
  277. inline static float vmaxvq_f32(float32x4_t v) {
  278. return
  279. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  280. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  281. }
  282. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  283. int32x4_t res;
  284. res[0] = roundf(vgetq_lane_f32(v, 0));
  285. res[1] = roundf(vgetq_lane_f32(v, 1));
  286. res[2] = roundf(vgetq_lane_f32(v, 2));
  287. res[3] = roundf(vgetq_lane_f32(v, 3));
  288. return res;
  289. }
  290. inline static uint8x8_t vzip1_u8(uint8x8_t a, uint8x8_t b) {
  291. uint8x8_t res;
  292. res[0] = a[0]; res[1] = b[0];
  293. res[2] = a[1]; res[3] = b[1];
  294. res[4] = a[2]; res[5] = b[2];
  295. res[6] = a[3]; res[7] = b[3];
  296. return res;
  297. }
  298. inline static uint8x8_t vzip2_u8(uint8x8_t a, uint8x8_t b) {
  299. uint8x8_t res;
  300. res[0] = a[4]; res[1] = b[4];
  301. res[2] = a[5]; res[3] = b[5];
  302. res[4] = a[6]; res[5] = b[6];
  303. res[6] = a[7]; res[7] = b[7];
  304. return res;
  305. }
  306. // vld1q_s16_x2
  307. // vld1q_u8_x2
  308. // vld1q_u8_x4
  309. // vld1q_s8_x2
  310. // vld1q_s8_x4
  311. // TODO: double-check these work correctly
  312. typedef struct ggml_int16x8x2_t {
  313. int16x8_t val[2];
  314. } ggml_int16x8x2_t;
  315. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  316. ggml_int16x8x2_t res;
  317. res.val[0] = vld1q_s16(ptr + 0);
  318. res.val[1] = vld1q_s16(ptr + 8);
  319. return res;
  320. }
  321. typedef struct ggml_uint8x16x2_t {
  322. uint8x16_t val[2];
  323. } ggml_uint8x16x2_t;
  324. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  325. ggml_uint8x16x2_t res;
  326. res.val[0] = vld1q_u8(ptr + 0);
  327. res.val[1] = vld1q_u8(ptr + 16);
  328. return res;
  329. }
  330. typedef struct ggml_uint8x16x4_t {
  331. uint8x16_t val[4];
  332. } ggml_uint8x16x4_t;
  333. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  334. ggml_uint8x16x4_t res;
  335. res.val[0] = vld1q_u8(ptr + 0);
  336. res.val[1] = vld1q_u8(ptr + 16);
  337. res.val[2] = vld1q_u8(ptr + 32);
  338. res.val[3] = vld1q_u8(ptr + 48);
  339. return res;
  340. }
  341. typedef struct ggml_int8x16x2_t {
  342. int8x16_t val[2];
  343. } ggml_int8x16x2_t;
  344. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  345. ggml_int8x16x2_t res;
  346. res.val[0] = vld1q_s8(ptr + 0);
  347. res.val[1] = vld1q_s8(ptr + 16);
  348. return res;
  349. }
  350. typedef struct ggml_int8x16x4_t {
  351. int8x16_t val[4];
  352. } ggml_int8x16x4_t;
  353. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  354. ggml_int8x16x4_t res;
  355. res.val[0] = vld1q_s8(ptr + 0);
  356. res.val[1] = vld1q_s8(ptr + 16);
  357. res.val[2] = vld1q_s8(ptr + 32);
  358. res.val[3] = vld1q_s8(ptr + 48);
  359. return res;
  360. }
  361. #else
  362. #define ggml_int16x8x2_t int16x8x2_t
  363. #define ggml_uint8x16x2_t uint8x16x2_t
  364. #define ggml_uint8x16x4_t uint8x16x4_t
  365. #define ggml_int8x16x2_t int8x16x2_t
  366. #define ggml_int8x16x4_t int8x16x4_t
  367. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  368. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  369. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  370. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  371. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  372. #endif
  373. #if !defined(__ARM_FEATURE_DOTPROD)
  374. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  375. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  376. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  377. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  378. }
  379. #else
  380. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  381. #endif
  382. #endif
  383. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  384. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  385. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  386. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  387. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  388. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  389. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  390. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  391. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  392. // precomputed tables for expanding 8bits to 8 bytes:
  393. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  394. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  395. #endif
  396. // reference implementation for deterministic creation of model files
  397. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  398. static const int qk = QK4_0;
  399. assert(k % qk == 0);
  400. const int nb = k / qk;
  401. for (int i = 0; i < nb; i++) {
  402. float amax = 0.0f; // absolute max
  403. float max = 0.0f;
  404. for (int j = 0; j < qk; j++) {
  405. const float v = x[i*qk + j];
  406. if (amax < fabsf(v)) {
  407. amax = fabsf(v);
  408. max = v;
  409. }
  410. }
  411. const float d = max / -8;
  412. const float id = d ? 1.0f/d : 0.0f;
  413. y[i].d = GGML_FP32_TO_FP16(d);
  414. for (int j = 0; j < qk/2; ++j) {
  415. const float x0 = x[i*qk + 0 + j]*id;
  416. const float x1 = x[i*qk + qk/2 + j]*id;
  417. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  418. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  419. y[i].qs[j] = xi0;
  420. y[i].qs[j] |= xi1 << 4;
  421. }
  422. }
  423. }
  424. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  425. quantize_row_q4_0_reference(x, y, k);
  426. }
  427. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  428. const int qk = QK4_1;
  429. assert(k % qk == 0);
  430. const int nb = k / qk;
  431. for (int i = 0; i < nb; i++) {
  432. float min = FLT_MAX;
  433. float max = -FLT_MAX;
  434. for (int j = 0; j < qk; j++) {
  435. const float v = x[i*qk + j];
  436. if (v < min) min = v;
  437. if (v > max) max = v;
  438. }
  439. const float d = (max - min) / ((1 << 4) - 1);
  440. const float id = d ? 1.0f/d : 0.0f;
  441. y[i].d = GGML_FP32_TO_FP16(d);
  442. y[i].m = GGML_FP32_TO_FP16(min);
  443. for (int j = 0; j < qk/2; ++j) {
  444. const float x0 = (x[i*qk + 0 + j] - min)*id;
  445. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  446. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  447. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  448. y[i].qs[j] = xi0;
  449. y[i].qs[j] |= xi1 << 4;
  450. }
  451. }
  452. }
  453. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  454. quantize_row_q4_1_reference(x, y, k);
  455. }
  456. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  457. static const int qk = QK5_0;
  458. assert(k % qk == 0);
  459. const int nb = k / qk;
  460. for (int i = 0; i < nb; i++) {
  461. float amax = 0.0f; // absolute max
  462. float max = 0.0f;
  463. for (int j = 0; j < qk; j++) {
  464. const float v = x[i*qk + j];
  465. if (amax < fabsf(v)) {
  466. amax = fabsf(v);
  467. max = v;
  468. }
  469. }
  470. const float d = max / -16;
  471. const float id = d ? 1.0f/d : 0.0f;
  472. y[i].d = GGML_FP32_TO_FP16(d);
  473. uint32_t qh = 0;
  474. for (int j = 0; j < qk/2; ++j) {
  475. const float x0 = x[i*qk + 0 + j]*id;
  476. const float x1 = x[i*qk + qk/2 + j]*id;
  477. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  478. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  479. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  480. // get the 5-th bit and store it in qh at the right position
  481. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  482. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  483. }
  484. memcpy(&y[i].qh, &qh, sizeof(qh));
  485. }
  486. }
  487. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  488. quantize_row_q5_0_reference(x, y, k);
  489. }
  490. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  491. const int qk = QK5_1;
  492. assert(k % qk == 0);
  493. const int nb = k / qk;
  494. for (int i = 0; i < nb; i++) {
  495. float min = FLT_MAX;
  496. float max = -FLT_MAX;
  497. for (int j = 0; j < qk; j++) {
  498. const float v = x[i*qk + j];
  499. if (v < min) min = v;
  500. if (v > max) max = v;
  501. }
  502. const float d = (max - min) / ((1 << 5) - 1);
  503. const float id = d ? 1.0f/d : 0.0f;
  504. y[i].d = GGML_FP32_TO_FP16(d);
  505. y[i].m = GGML_FP32_TO_FP16(min);
  506. uint32_t qh = 0;
  507. for (int j = 0; j < qk/2; ++j) {
  508. const float x0 = (x[i*qk + 0 + j] - min)*id;
  509. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  510. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  511. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  512. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  513. // get the 5-th bit and store it in qh at the right position
  514. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  515. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  516. }
  517. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  518. }
  519. }
  520. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  521. quantize_row_q5_1_reference(x, y, k);
  522. }
  523. // reference implementation for deterministic creation of model files
  524. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  525. assert(k % QK8_0 == 0);
  526. const int nb = k / QK8_0;
  527. for (int i = 0; i < nb; i++) {
  528. float amax = 0.0f; // absolute max
  529. for (int j = 0; j < QK8_0; j++) {
  530. const float v = x[i*QK8_0 + j];
  531. amax = MAX(amax, fabsf(v));
  532. }
  533. const float d = amax / ((1 << 7) - 1);
  534. const float id = d ? 1.0f/d : 0.0f;
  535. y[i].d = GGML_FP32_TO_FP16(d);
  536. for (int j = 0; j < QK8_0; ++j) {
  537. const float x0 = x[i*QK8_0 + j]*id;
  538. y[i].qs[j] = roundf(x0);
  539. }
  540. }
  541. }
  542. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  543. assert(QK8_0 == 32);
  544. assert(k % QK8_0 == 0);
  545. const int nb = k / QK8_0;
  546. block_q8_0 * restrict y = vy;
  547. #if defined(__ARM_NEON)
  548. for (int i = 0; i < nb; i++) {
  549. float32x4_t srcv [8];
  550. float32x4_t asrcv[8];
  551. float32x4_t amaxv[8];
  552. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  553. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  554. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  555. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  556. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  557. const float amax = vmaxvq_f32(amaxv[0]);
  558. const float d = amax / ((1 << 7) - 1);
  559. const float id = d ? 1.0f/d : 0.0f;
  560. y[i].d = GGML_FP32_TO_FP16(d);
  561. for (int j = 0; j < 8; j++) {
  562. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  563. const int32x4_t vi = vcvtnq_s32_f32(v);
  564. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  565. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  566. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  567. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  568. }
  569. }
  570. #elif defined(__wasm_simd128__)
  571. for (int i = 0; i < nb; i++) {
  572. v128_t srcv [8];
  573. v128_t asrcv[8];
  574. v128_t amaxv[8];
  575. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  576. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  577. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  578. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  579. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  580. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  581. wasm_f32x4_extract_lane(amaxv[0], 1)),
  582. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  583. wasm_f32x4_extract_lane(amaxv[0], 3)));
  584. const float d = amax / ((1 << 7) - 1);
  585. const float id = d ? 1.0f/d : 0.0f;
  586. y[i].d = GGML_FP32_TO_FP16(d);
  587. for (int j = 0; j < 8; j++) {
  588. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  589. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  590. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  591. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  592. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  593. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  594. }
  595. }
  596. #elif defined(__AVX2__) || defined(__AVX__)
  597. for (int i = 0; i < nb; i++) {
  598. // Load elements into 4 AVX vectors
  599. __m256 v0 = _mm256_loadu_ps( x );
  600. __m256 v1 = _mm256_loadu_ps( x + 8 );
  601. __m256 v2 = _mm256_loadu_ps( x + 16 );
  602. __m256 v3 = _mm256_loadu_ps( x + 24 );
  603. x += 32;
  604. // Compute max(abs(e)) for the block
  605. const __m256 signBit = _mm256_set1_ps( -0.0f );
  606. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  607. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  608. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  609. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  610. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  611. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  612. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  613. const float maxScalar = _mm_cvtss_f32( max4 );
  614. // Quantize these floats
  615. const float d = maxScalar / 127.f;
  616. y[i].d = GGML_FP32_TO_FP16(d);
  617. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  618. const __m256 mul = _mm256_set1_ps( id );
  619. // Apply the multiplier
  620. v0 = _mm256_mul_ps( v0, mul );
  621. v1 = _mm256_mul_ps( v1, mul );
  622. v2 = _mm256_mul_ps( v2, mul );
  623. v3 = _mm256_mul_ps( v3, mul );
  624. // Round to nearest integer
  625. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  626. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  627. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  628. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  629. // Convert floats to integers
  630. __m256i i0 = _mm256_cvtps_epi32( v0 );
  631. __m256i i1 = _mm256_cvtps_epi32( v1 );
  632. __m256i i2 = _mm256_cvtps_epi32( v2 );
  633. __m256i i3 = _mm256_cvtps_epi32( v3 );
  634. #if defined(__AVX2__)
  635. // Convert int32 to int16
  636. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  637. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  638. // Convert int16 to int8
  639. i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  640. // We got our precious signed bytes, but the order is now wrong
  641. // These AVX2 pack instructions process 16-byte pieces independently
  642. // The following instruction is fixing the order
  643. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  644. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  645. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  646. #else
  647. // Since we don't have in AVX some necessary functions,
  648. // we split the registers in half and call AVX2 analogs from SSE
  649. __m128i ni0 = _mm256_castsi256_si128( i0 );
  650. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  651. __m128i ni2 = _mm256_castsi256_si128( i1 );
  652. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  653. __m128i ni4 = _mm256_castsi256_si128( i2 );
  654. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  655. __m128i ni6 = _mm256_castsi256_si128( i3 );
  656. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  657. // Convert int32 to int16
  658. ni0 = _mm_packs_epi32( ni0, ni1 );
  659. ni2 = _mm_packs_epi32( ni2, ni3 );
  660. ni4 = _mm_packs_epi32( ni4, ni5 );
  661. ni6 = _mm_packs_epi32( ni6, ni7 );
  662. // Convert int16 to int8
  663. ni0 = _mm_packs_epi16( ni0, ni2 );
  664. ni4 = _mm_packs_epi16( ni4, ni6 );
  665. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  666. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  667. #endif
  668. }
  669. #elif defined(__riscv_v_intrinsic)
  670. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  671. for (int i = 0; i < nb; i++) {
  672. // load elements
  673. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  674. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  675. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  676. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  677. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  678. const float d = amax / ((1 << 7) - 1);
  679. const float id = d ? 1.0f/d : 0.0f;
  680. y[i].d = GGML_FP32_TO_FP16(d);
  681. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  682. // convert to integer
  683. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  684. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  685. // store result
  686. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  687. }
  688. #else
  689. GGML_UNUSED(nb);
  690. // scalar
  691. quantize_row_q8_0_reference(x, y, k);
  692. #endif
  693. }
  694. // reference implementation for deterministic creation of model files
  695. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  696. assert(QK8_1 == 32);
  697. assert(k % QK8_1 == 0);
  698. const int nb = k / QK8_1;
  699. for (int i = 0; i < nb; i++) {
  700. float amax = 0.0f; // absolute max
  701. for (int j = 0; j < QK8_1; j++) {
  702. const float v = x[i*QK8_1 + j];
  703. amax = MAX(amax, fabsf(v));
  704. }
  705. const float d = amax / ((1 << 7) - 1);
  706. const float id = d ? 1.0f/d : 0.0f;
  707. y[i].d = d;
  708. int sum = 0;
  709. for (int j = 0; j < QK8_1/2; ++j) {
  710. const float v0 = x[i*QK8_1 + j]*id;
  711. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  712. y[i].qs[ j] = roundf(v0);
  713. y[i].qs[QK8_1/2 + j] = roundf(v1);
  714. sum += y[i].qs[ j];
  715. sum += y[i].qs[QK8_1/2 + j];
  716. }
  717. y[i].s = sum*d;
  718. }
  719. }
  720. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  721. assert(k % QK8_1 == 0);
  722. const int nb = k / QK8_1;
  723. block_q8_1 * restrict y = vy;
  724. #if defined(__ARM_NEON)
  725. for (int i = 0; i < nb; i++) {
  726. float32x4_t srcv [8];
  727. float32x4_t asrcv[8];
  728. float32x4_t amaxv[8];
  729. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  730. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  731. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  732. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  733. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  734. const float amax = vmaxvq_f32(amaxv[0]);
  735. const float d = amax / ((1 << 7) - 1);
  736. const float id = d ? 1.0f/d : 0.0f;
  737. y[i].d = d;
  738. int32x4_t accv = vdupq_n_s32(0);
  739. for (int j = 0; j < 8; j++) {
  740. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  741. const int32x4_t vi = vcvtnq_s32_f32(v);
  742. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  743. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  744. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  745. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  746. accv = vaddq_s32(accv, vi);
  747. }
  748. y[i].s = d * vaddvq_s32(accv);
  749. }
  750. #elif defined(__wasm_simd128__)
  751. for (int i = 0; i < nb; i++) {
  752. v128_t srcv [8];
  753. v128_t asrcv[8];
  754. v128_t amaxv[8];
  755. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  756. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  757. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  758. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  759. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  760. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  761. wasm_f32x4_extract_lane(amaxv[0], 1)),
  762. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  763. wasm_f32x4_extract_lane(amaxv[0], 3)));
  764. const float d = amax / ((1 << 7) - 1);
  765. const float id = d ? 1.0f/d : 0.0f;
  766. y[i].d = d;
  767. v128_t accv = wasm_i32x4_splat(0);
  768. for (int j = 0; j < 8; j++) {
  769. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  770. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  771. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  772. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  773. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  774. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  775. accv = wasm_i32x4_add(accv, vi);
  776. }
  777. y[i].s = d * (wasm_i32x4_extract_lane(accv, 0) +
  778. wasm_i32x4_extract_lane(accv, 1) +
  779. wasm_i32x4_extract_lane(accv, 2) +
  780. wasm_i32x4_extract_lane(accv, 3));
  781. }
  782. #elif defined(__AVX2__) || defined(__AVX__)
  783. for (int i = 0; i < nb; i++) {
  784. // Load elements into 4 AVX vectors
  785. __m256 v0 = _mm256_loadu_ps( x );
  786. __m256 v1 = _mm256_loadu_ps( x + 8 );
  787. __m256 v2 = _mm256_loadu_ps( x + 16 );
  788. __m256 v3 = _mm256_loadu_ps( x + 24 );
  789. x += 32;
  790. // Compute max(abs(e)) for the block
  791. const __m256 signBit = _mm256_set1_ps( -0.0f );
  792. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  793. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  794. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  795. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  796. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  797. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  798. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  799. const float maxScalar = _mm_cvtss_f32( max4 );
  800. // Quantize these floats
  801. const float d = maxScalar / 127.f;
  802. y[i].d = d;
  803. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  804. const __m256 mul = _mm256_set1_ps( id );
  805. // Apply the multiplier
  806. v0 = _mm256_mul_ps( v0, mul );
  807. v1 = _mm256_mul_ps( v1, mul );
  808. v2 = _mm256_mul_ps( v2, mul );
  809. v3 = _mm256_mul_ps( v3, mul );
  810. // Round to nearest integer
  811. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  812. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  813. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  814. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  815. // Convert floats to integers
  816. __m256i i0 = _mm256_cvtps_epi32( v0 );
  817. __m256i i1 = _mm256_cvtps_epi32( v1 );
  818. __m256i i2 = _mm256_cvtps_epi32( v2 );
  819. __m256i i3 = _mm256_cvtps_epi32( v3 );
  820. #if defined(__AVX2__)
  821. // Compute the sum of the quants and set y[i].s
  822. y[i].s = d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)));
  823. // Convert int32 to int16
  824. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  825. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  826. // Convert int16 to int8
  827. i0 = _mm256_packs_epi16( i0, i2 ); // 0, 1, 2, 3, 8, 9, 10, 11, 16, 17, 18, 19, 24, 25, 26, 27, 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31
  828. // We got our precious signed bytes, but the order is now wrong
  829. // These AVX2 pack instructions process 16-byte pieces independently
  830. // The following instruction is fixing the order
  831. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  832. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  833. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  834. #else
  835. // Since we don't have in AVX some necessary functions,
  836. // we split the registers in half and call AVX2 analogs from SSE
  837. __m128i ni0 = _mm256_castsi256_si128( i0 );
  838. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  839. __m128i ni2 = _mm256_castsi256_si128( i1 );
  840. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  841. __m128i ni4 = _mm256_castsi256_si128( i2 );
  842. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  843. __m128i ni6 = _mm256_castsi256_si128( i3 );
  844. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  845. // Compute the sum of the quants and set y[i].s
  846. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  847. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  848. y[i].s = d * hsum_i32_4(_mm_add_epi32(s0, s1));
  849. // Convert int32 to int16
  850. ni0 = _mm_packs_epi32( ni0, ni1 );
  851. ni2 = _mm_packs_epi32( ni2, ni3 );
  852. ni4 = _mm_packs_epi32( ni4, ni5 );
  853. ni6 = _mm_packs_epi32( ni6, ni7 );
  854. // Convert int16 to int8
  855. ni0 = _mm_packs_epi16( ni0, ni2 );
  856. ni4 = _mm_packs_epi16( ni4, ni6 );
  857. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  858. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  859. #endif
  860. }
  861. #elif defined(__riscv_v_intrinsic)
  862. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  863. for (int i = 0; i < nb; i++) {
  864. // load elements
  865. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  866. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  867. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  868. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  869. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  870. const float d = amax / ((1 << 7) - 1);
  871. const float id = d ? 1.0f/d : 0.0f;
  872. y[i].d = d;
  873. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  874. // convert to integer
  875. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  876. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  877. // store result
  878. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  879. // compute sum for y[i].s
  880. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  881. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  882. // set y[i].s
  883. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  884. y[i].s = sum*d;
  885. }
  886. #else
  887. GGML_UNUSED(nb);
  888. // scalar
  889. quantize_row_q8_1_reference(x, y, k);
  890. #endif
  891. }
  892. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  893. static const int qk = QK4_0;
  894. assert(k % qk == 0);
  895. const int nb = k / qk;
  896. for (int i = 0; i < nb; i++) {
  897. const float d = GGML_FP16_TO_FP32(x[i].d);
  898. for (int j = 0; j < qk/2; ++j) {
  899. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  900. const int x1 = (x[i].qs[j] >> 4) - 8;
  901. y[i*qk + j + 0 ] = x0*d;
  902. y[i*qk + j + qk/2] = x1*d;
  903. }
  904. }
  905. }
  906. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  907. static const int qk = QK4_1;
  908. assert(k % qk == 0);
  909. const int nb = k / qk;
  910. for (int i = 0; i < nb; i++) {
  911. const float d = GGML_FP16_TO_FP32(x[i].d);
  912. const float m = GGML_FP16_TO_FP32(x[i].m);
  913. for (int j = 0; j < qk/2; ++j) {
  914. const int x0 = (x[i].qs[j] & 0x0F);
  915. const int x1 = (x[i].qs[j] >> 4);
  916. y[i*qk + j + 0 ] = x0*d + m;
  917. y[i*qk + j + qk/2] = x1*d + m;
  918. }
  919. }
  920. }
  921. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  922. static const int qk = QK5_0;
  923. assert(k % qk == 0);
  924. const int nb = k / qk;
  925. for (int i = 0; i < nb; i++) {
  926. const float d = GGML_FP16_TO_FP32(x[i].d);
  927. uint32_t qh;
  928. memcpy(&qh, x[i].qh, sizeof(qh));
  929. for (int j = 0; j < qk/2; ++j) {
  930. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  931. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  932. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  933. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  934. y[i*qk + j + 0 ] = x0*d;
  935. y[i*qk + j + qk/2] = x1*d;
  936. }
  937. }
  938. }
  939. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  940. static const int qk = QK5_1;
  941. assert(k % qk == 0);
  942. const int nb = k / qk;
  943. for (int i = 0; i < nb; i++) {
  944. const float d = GGML_FP16_TO_FP32(x[i].d);
  945. const float m = GGML_FP16_TO_FP32(x[i].m);
  946. uint32_t qh;
  947. memcpy(&qh, x[i].qh, sizeof(qh));
  948. for (int j = 0; j < qk/2; ++j) {
  949. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  950. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  951. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  952. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  953. y[i*qk + j + 0 ] = x0*d + m;
  954. y[i*qk + j + qk/2] = x1*d + m;
  955. }
  956. }
  957. }
  958. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  959. static const int qk = QK8_0;
  960. assert(k % qk == 0);
  961. const int nb = k / qk;
  962. for (int i = 0; i < nb; i++) {
  963. const float d = GGML_FP16_TO_FP32(x[i].d);
  964. for (int j = 0; j < qk; ++j) {
  965. y[i*qk + j] = x[i].qs[j]*d;
  966. }
  967. }
  968. }
  969. //
  970. // 2-6 bit quantization in super-blocks
  971. //
  972. //
  973. // ===================== Helper functions
  974. //
  975. static inline int nearest_int(float fval) {
  976. assert(fval <= 4194303.f);
  977. float val = fval + 12582912.f;
  978. int i; memcpy(&i, &val, sizeof(int));
  979. return (i & 0x007fffff) - 0x00400000;
  980. }
  981. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type,
  982. const float * restrict qw) {
  983. float max = 0;
  984. float amax = 0;
  985. for (int i = 0; i < n; ++i) {
  986. float ax = fabsf(x[i]);
  987. if (ax > amax) { amax = ax; max = x[i]; }
  988. }
  989. if (amax < 1e-30f) { // all zero
  990. for (int i = 0; i < n; ++i) {
  991. L[i] = 0;
  992. }
  993. return 0.f;
  994. }
  995. float iscale = -nmax / max;
  996. if (rmse_type == 0) {
  997. for (int i = 0; i < n; ++i) {
  998. int l = nearest_int(iscale * x[i]);
  999. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1000. }
  1001. return 1/iscale;
  1002. }
  1003. bool return_early = false;
  1004. if (rmse_type < 0) {
  1005. rmse_type = -rmse_type;
  1006. return_early = true;
  1007. }
  1008. float sumlx = 0;
  1009. float suml2 = 0;
  1010. #ifdef HAVE_BUGGY_APPLE_LINKER
  1011. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1012. for (volatile int i = 0; i < n; ++i) {
  1013. #else
  1014. for (int i = 0; i < n; ++i) {
  1015. #endif
  1016. int l = nearest_int(iscale * x[i]);
  1017. l = MAX(-nmax, MIN(nmax-1, l));
  1018. L[i] = l + nmax;
  1019. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1020. sumlx += w*x[i]*l;
  1021. suml2 += w*l*l;
  1022. }
  1023. float scale = sumlx/suml2;
  1024. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  1025. float best = scale * sumlx;
  1026. for (int is = -9; is <= 9; ++is) {
  1027. if (is == 0) {
  1028. continue;
  1029. }
  1030. iscale = -(nmax + 0.1f*is) / max;
  1031. sumlx = suml2 = 0;
  1032. for (int i = 0; i < n; ++i) {
  1033. int l = nearest_int(iscale * x[i]);
  1034. l = MAX(-nmax, MIN(nmax-1, l));
  1035. float w = qw ? qw[i] : rmse_type == 1 ? x[i] * x[i] : rmse_type == 2 ? 1 : rmse_type == 3 ? fabsf(x[i]) : sqrtf(fabsf(x[i]));
  1036. sumlx += w*x[i]*l;
  1037. suml2 += w*l*l;
  1038. }
  1039. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1040. for (int i = 0; i < n; ++i) {
  1041. int l = nearest_int(iscale * x[i]);
  1042. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1043. }
  1044. scale = sumlx/suml2; best = scale*sumlx;
  1045. }
  1046. }
  1047. return scale;
  1048. }
  1049. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1050. float max = 0;
  1051. float amax = 0;
  1052. for (int i = 0; i < n; ++i) {
  1053. float ax = fabsf(x[i]);
  1054. if (ax > amax) { amax = ax; max = x[i]; }
  1055. }
  1056. if (!amax) { // all zero
  1057. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1058. return 0.f;
  1059. }
  1060. float iscale = -nmax / max;
  1061. if (do_rmse) {
  1062. float sumlx = 0;
  1063. float suml2 = 0;
  1064. for (int i = 0; i < n; ++i) {
  1065. int l = nearest_int(iscale * x[i]);
  1066. l = MAX(-nmax, MIN(nmax-1, l));
  1067. L[i] = l;
  1068. float w = x[i]*x[i];
  1069. sumlx += w*x[i]*l;
  1070. suml2 += w*l*l;
  1071. }
  1072. for (int itry = 0; itry < 5; ++itry) {
  1073. int n_changed = 0;
  1074. for (int i = 0; i < n; ++i) {
  1075. float w = x[i]*x[i];
  1076. float slx = sumlx - w*x[i]*L[i];
  1077. if (slx > 0) {
  1078. float sl2 = suml2 - w*L[i]*L[i];
  1079. int new_l = nearest_int(x[i] * sl2 / slx);
  1080. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1081. if (new_l != L[i]) {
  1082. slx += w*x[i]*new_l;
  1083. sl2 += w*new_l*new_l;
  1084. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1085. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1086. ++n_changed;
  1087. }
  1088. }
  1089. }
  1090. }
  1091. if (!n_changed) {
  1092. break;
  1093. }
  1094. }
  1095. for (int i = 0; i < n; ++i) {
  1096. L[i] += nmax;
  1097. }
  1098. return sumlx / suml2;
  1099. }
  1100. for (int i = 0; i < n; ++i) {
  1101. int l = nearest_int(iscale * x[i]);
  1102. l = MAX(-nmax, MIN(nmax-1, l));
  1103. L[i] = l + nmax;
  1104. }
  1105. return 1/iscale;
  1106. }
  1107. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1108. int ntry, float alpha) {
  1109. float min = x[0];
  1110. float max = x[0];
  1111. for (int i = 1; i < n; ++i) {
  1112. if (x[i] < min) min = x[i];
  1113. if (x[i] > max) max = x[i];
  1114. }
  1115. if (max == min) {
  1116. for (int i = 0; i < n; ++i) L[i] = 0;
  1117. *the_min = 0;
  1118. return 0.f;
  1119. }
  1120. if (min > 0) min = 0;
  1121. float iscale = nmax/(max - min);
  1122. float scale = 1/iscale;
  1123. for (int itry = 0; itry < ntry; ++itry) {
  1124. float sumlx = 0; int suml2 = 0;
  1125. bool did_change = false;
  1126. for (int i = 0; i < n; ++i) {
  1127. int l = nearest_int(iscale*(x[i] - min));
  1128. l = MAX(0, MIN(nmax, l));
  1129. if (l != L[i]) {
  1130. L[i] = l;
  1131. did_change = true;
  1132. }
  1133. sumlx += (x[i] - min)*l;
  1134. suml2 += l*l;
  1135. }
  1136. scale = sumlx/suml2;
  1137. float sum = 0;
  1138. for (int i = 0; i < n; ++i) {
  1139. sum += x[i] - scale*L[i];
  1140. }
  1141. min = alpha*min + (1 - alpha)*sum/n;
  1142. if (min > 0) min = 0;
  1143. iscale = 1/scale;
  1144. if (!did_change) break;
  1145. }
  1146. *the_min = -min;
  1147. return scale;
  1148. }
  1149. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1150. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1151. float rmin, float rdelta, int nstep, bool use_mad) {
  1152. float min = x[0];
  1153. float max = x[0];
  1154. float sum_w = weights[0];
  1155. float sum_x = sum_w * x[0];
  1156. #ifdef HAVE_BUGGY_APPLE_LINKER
  1157. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1158. for (volatile int i = 1; i < n; ++i) {
  1159. #else
  1160. for (int i = 1; i < n; ++i) {
  1161. #endif
  1162. if (x[i] < min) min = x[i];
  1163. if (x[i] > max) max = x[i];
  1164. float w = weights[i];
  1165. sum_w += w;
  1166. sum_x += w * x[i];
  1167. }
  1168. if (min > 0) min = 0;
  1169. if (max == min) {
  1170. for (int i = 0; i < n; ++i) L[i] = 0;
  1171. *the_min = -min;
  1172. return 0.f;
  1173. }
  1174. float iscale = nmax/(max - min);
  1175. float scale = 1/iscale;
  1176. float best_mad = 0;
  1177. for (int i = 0; i < n; ++i) {
  1178. int l = nearest_int(iscale*(x[i] - min));
  1179. L[i] = MAX(0, MIN(nmax, l));
  1180. float diff = scale * L[i] + min - x[i];
  1181. diff = use_mad ? fabsf(diff) : diff * diff;
  1182. float w = weights[i];
  1183. best_mad += w * diff;
  1184. }
  1185. if (nstep < 1) {
  1186. *the_min = -min;
  1187. return scale;
  1188. }
  1189. for (int is = 0; is <= nstep; ++is) {
  1190. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1191. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1192. for (int i = 0; i < n; ++i) {
  1193. int l = nearest_int(iscale*(x[i] - min));
  1194. l = MAX(0, MIN(nmax, l));
  1195. Laux[i] = l;
  1196. float w = weights[i];
  1197. sum_l += w*l;
  1198. sum_l2 += w*l*l;
  1199. sum_xl += w*l*x[i];
  1200. }
  1201. float D = sum_w * sum_l2 - sum_l * sum_l;
  1202. if (D > 0) {
  1203. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1204. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1205. if (this_min > 0) {
  1206. this_min = 0;
  1207. this_scale = sum_xl / sum_l2;
  1208. }
  1209. float mad = 0;
  1210. for (int i = 0; i < n; ++i) {
  1211. float diff = this_scale * Laux[i] + this_min - x[i];
  1212. diff = use_mad ? fabsf(diff) : diff * diff;
  1213. float w = weights[i];
  1214. mad += w * diff;
  1215. }
  1216. if (mad < best_mad) {
  1217. for (int i = 0; i < n; ++i) {
  1218. L[i] = Laux[i];
  1219. }
  1220. best_mad = mad;
  1221. scale = this_scale;
  1222. min = this_min;
  1223. }
  1224. }
  1225. }
  1226. *the_min = -min;
  1227. return scale;
  1228. }
  1229. #if QK_K == 256
  1230. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1231. if (j < 4) {
  1232. *d = q[j] & 63; *m = q[j + 4] & 63;
  1233. } else {
  1234. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1235. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1236. }
  1237. }
  1238. #endif
  1239. //========================- 2-bit (de)-quantization
  1240. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1241. assert(k % QK_K == 0);
  1242. const int nb = k / QK_K;
  1243. uint8_t L[QK_K];
  1244. uint8_t Laux[16];
  1245. float weights[16];
  1246. float mins[QK_K/16];
  1247. float scales[QK_K/16];
  1248. const float q4scale = 15.f;
  1249. for (int i = 0; i < nb; i++) {
  1250. float max_scale = 0; // as we are deducting the min, scales are always positive
  1251. float max_min = 0;
  1252. for (int j = 0; j < QK_K/16; ++j) {
  1253. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1254. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1255. float scale = scales[j];
  1256. if (scale > max_scale) {
  1257. max_scale = scale;
  1258. }
  1259. float min = mins[j];
  1260. if (min > max_min) {
  1261. max_min = min;
  1262. }
  1263. }
  1264. if (max_scale > 0) {
  1265. float iscale = q4scale/max_scale;
  1266. for (int j = 0; j < QK_K/16; ++j) {
  1267. int l = nearest_int(iscale*scales[j]);
  1268. y[i].scales[j] = l;
  1269. }
  1270. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1271. } else {
  1272. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1273. y[i].d = GGML_FP32_TO_FP16(0.f);
  1274. }
  1275. if (max_min > 0) {
  1276. float iscale = q4scale/max_min;
  1277. for (int j = 0; j < QK_K/16; ++j) {
  1278. int l = nearest_int(iscale*mins[j]);
  1279. y[i].scales[j] |= (l << 4);
  1280. }
  1281. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1282. } else {
  1283. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1284. }
  1285. for (int j = 0; j < QK_K/16; ++j) {
  1286. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1287. if (!d) continue;
  1288. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1289. for (int ii = 0; ii < 16; ++ii) {
  1290. int l = nearest_int((x[16*j + ii] + dm)/d);
  1291. l = MAX(0, MIN(3, l));
  1292. L[16*j + ii] = l;
  1293. }
  1294. }
  1295. #if QK_K == 256
  1296. for (int j = 0; j < QK_K; j += 128) {
  1297. for (int l = 0; l < 32; ++l) {
  1298. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1299. }
  1300. }
  1301. #else
  1302. for (int l = 0; l < 16; ++l) {
  1303. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1304. }
  1305. #endif
  1306. x += QK_K;
  1307. }
  1308. }
  1309. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1310. assert(k % QK_K == 0);
  1311. const int nb = k / QK_K;
  1312. for (int i = 0; i < nb; i++) {
  1313. const float d = GGML_FP16_TO_FP32(x[i].d);
  1314. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1315. const uint8_t * q = x[i].qs;
  1316. #if QK_K == 256
  1317. int is = 0;
  1318. float dl, ml;
  1319. for (int n = 0; n < QK_K; n += 128) {
  1320. int shift = 0;
  1321. for (int j = 0; j < 4; ++j) {
  1322. uint8_t sc = x[i].scales[is++];
  1323. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1324. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1325. sc = x[i].scales[is++];
  1326. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1327. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1328. shift += 2;
  1329. }
  1330. q += 32;
  1331. }
  1332. #else
  1333. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1334. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1335. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1336. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1337. for (int l = 0; l < 16; ++l) {
  1338. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1339. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1340. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1341. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1342. }
  1343. y += QK_K;
  1344. #endif
  1345. }
  1346. }
  1347. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1348. quantize_row_q2_K_reference(x, vy, k);
  1349. }
  1350. size_t ggml_quantize_q2_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1351. (void)hist; // TODO: collect histograms
  1352. for (int j = 0; j < n; j += k) {
  1353. block_q2_K * restrict y = (block_q2_K *)dst + j/QK_K;
  1354. quantize_row_q2_K_reference(src + j, y, k);
  1355. }
  1356. return (n/QK_K*sizeof(block_q2_K));
  1357. }
  1358. static float make_qkx3_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1359. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1360. float rmin, float rdelta, int nstep, bool use_mad) {
  1361. float min = x[0];
  1362. float max = x[0];
  1363. float sum_w = weights ? weights[0] : x[0]*x[0];
  1364. float sum_x = sum_w * x[0];
  1365. #ifdef HAVE_BUGGY_APPLE_LINKER
  1366. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1367. for (volatile int i = 1; i < n; ++i) {
  1368. #else
  1369. for (int i = 1; i < n; ++i) {
  1370. #endif
  1371. if (x[i] < min) min = x[i];
  1372. if (x[i] > max) max = x[i];
  1373. float w = weights ? weights[i] : x[i]*x[i];
  1374. sum_w += w;
  1375. sum_x += w * x[i];
  1376. }
  1377. if (min > 0) {
  1378. min = 0;
  1379. }
  1380. if (max <= min) {
  1381. memset(L, 0, n);
  1382. *the_min = -min;
  1383. return 0.f;
  1384. }
  1385. float iscale = nmax/(max - min);
  1386. float scale = 1/iscale;
  1387. float best_mad = 0;
  1388. for (int i = 0; i < n; ++i) {
  1389. int l = nearest_int(iscale*(x[i] - min));
  1390. L[i] = MAX(0, MIN(nmax, l));
  1391. float diff = scale * L[i] + min - x[i];
  1392. diff = use_mad ? fabsf(diff) : diff*diff;
  1393. float w = weights ? weights[i] : x[i]*x[i];
  1394. best_mad += w * diff;
  1395. }
  1396. if (nstep < 1) {
  1397. *the_min = -min;
  1398. return scale;
  1399. }
  1400. for (int is = 0; is <= nstep; ++is) {
  1401. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1402. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1403. for (int i = 0; i < n; ++i) {
  1404. int l = nearest_int(iscale*(x[i] - min));
  1405. l = MAX(0, MIN(nmax, l));
  1406. Laux[i] = l;
  1407. float w = weights ? weights[i] : x[i]*x[i];
  1408. sum_l += w*l;
  1409. sum_l2 += w*l*l;
  1410. sum_xl += w*l*x[i];
  1411. }
  1412. float D = sum_w * sum_l2 - sum_l * sum_l;
  1413. if (D > 0) {
  1414. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1415. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1416. if (this_min > 0) {
  1417. this_min = 0;
  1418. this_scale = sum_xl / sum_l2;
  1419. }
  1420. float mad = 0;
  1421. for (int i = 0; i < n; ++i) {
  1422. float diff = this_scale * Laux[i] + this_min - x[i];
  1423. diff = use_mad ? fabsf(diff) : diff*diff;
  1424. float w = weights ? weights[i] : x[i]*x[i];
  1425. mad += w * diff;
  1426. }
  1427. if (mad < best_mad) {
  1428. for (int i = 0; i < n; ++i) {
  1429. L[i] = Laux[i];
  1430. }
  1431. best_mad = mad;
  1432. scale = this_scale;
  1433. min = this_min;
  1434. }
  1435. }
  1436. }
  1437. *the_min = -min;
  1438. return scale;
  1439. }
  1440. static float make_qp_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, const float * quant_weights) {
  1441. float max = 0;
  1442. for (int i = 0; i < n; ++i) {
  1443. max = MAX(max, x[i]);
  1444. }
  1445. if (!max) { // all zero
  1446. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1447. return 0.f;
  1448. }
  1449. float iscale = nmax / max;
  1450. for (int i = 0; i < n; ++i) {
  1451. L[i] = nearest_int(iscale * x[i]);
  1452. }
  1453. float scale = 1/iscale;
  1454. float best_mse = 0;
  1455. for (int i = 0; i < n; ++i) {
  1456. float diff = x[i] - scale*L[i];
  1457. float w = quant_weights[i];
  1458. best_mse += w*diff*diff;
  1459. }
  1460. for (int is = -4; is <= 4; ++is) {
  1461. if (is == 0) continue;
  1462. float iscale_is = (0.1f*is + nmax)/max;
  1463. float scale_is = 1/iscale_is;
  1464. float mse = 0;
  1465. for (int i = 0; i < n; ++i) {
  1466. int l = nearest_int(iscale_is*x[i]);
  1467. l = MIN(nmax, l);
  1468. float diff = x[i] - scale_is*l;
  1469. float w = quant_weights[i];
  1470. mse += w*diff*diff;
  1471. }
  1472. if (mse < best_mse) {
  1473. best_mse = mse;
  1474. iscale = iscale_is;
  1475. }
  1476. }
  1477. float sumlx = 0;
  1478. float suml2 = 0;
  1479. for (int i = 0; i < n; ++i) {
  1480. int l = nearest_int(iscale * x[i]);
  1481. l = MIN(nmax, l);
  1482. L[i] = l;
  1483. float w = quant_weights[i];
  1484. sumlx += w*x[i]*l;
  1485. suml2 += w*l*l;
  1486. }
  1487. for (int itry = 0; itry < 5; ++itry) {
  1488. int n_changed = 0;
  1489. for (int i = 0; i < n; ++i) {
  1490. float w = quant_weights[i];
  1491. float slx = sumlx - w*x[i]*L[i];
  1492. float sl2 = suml2 - w*L[i]*L[i];
  1493. if (slx > 0 && sl2 > 0) {
  1494. int new_l = nearest_int(x[i] * sl2 / slx);
  1495. new_l = MIN(nmax, new_l);
  1496. if (new_l != L[i]) {
  1497. slx += w*x[i]*new_l;
  1498. sl2 += w*new_l*new_l;
  1499. if (slx*slx*suml2 > sumlx*sumlx*sl2) {
  1500. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1501. ++n_changed;
  1502. }
  1503. }
  1504. }
  1505. }
  1506. if (!n_changed) {
  1507. break;
  1508. }
  1509. }
  1510. return sumlx / suml2;
  1511. }
  1512. static void quantize_row_q2_K_impl(const float * restrict x, block_q2_K * restrict y, int k, const float * restrict quant_weights) {
  1513. GGML_ASSERT(quant_weights);
  1514. assert(k % QK_K == 0);
  1515. const int nb = k / QK_K;
  1516. const bool requantize = true;
  1517. uint8_t L[QK_K];
  1518. uint8_t Laux[16];
  1519. float mins[QK_K/16];
  1520. float scales[QK_K/16];
  1521. float sw[QK_K/16];
  1522. float weight[QK_K/16];
  1523. uint8_t Ls[QK_K/16], Lm[QK_K/16];
  1524. for (int i = 0; i < nb; i++) {
  1525. memset(sw, 0, QK_K/16*sizeof(float));
  1526. float sumx2 = 0;
  1527. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1528. float sigma2 = sumx2/QK_K;
  1529. for (int j = 0; j < QK_K/16; ++j) {
  1530. const float * restrict qw = quant_weights + QK_K * i + 16*j;
  1531. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j + l]*x[16*j + l]);
  1532. for (int l = 0; l < 16; ++l) sw[j] += weight[l];
  1533. scales[j] = make_qkx3_quants(16, 3, x + 16*j, weight, L + 16*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  1534. }
  1535. float dm = make_qp_quants(QK_K/16, 15, scales, Ls, sw);
  1536. float mm = make_qp_quants(QK_K/16, 15, mins, Lm, sw);
  1537. y[i].d = GGML_FP32_TO_FP16(dm);
  1538. y[i].dmin = GGML_FP32_TO_FP16(mm);
  1539. dm = GGML_FP16_TO_FP32(y[i].d);
  1540. mm = GGML_FP16_TO_FP32(y[i].dmin);
  1541. for (int j = 0; j < QK_K/16; ++j) {
  1542. y[i].scales[j] = Ls[j] | (Lm[j] << 4);
  1543. }
  1544. if (requantize) {
  1545. for (int j = 0; j < QK_K/16; ++j) {
  1546. const float d = dm * (y[i].scales[j] & 0xF);
  1547. if (!d) continue;
  1548. const float m = mm * (y[i].scales[j] >> 4);
  1549. for (int ii = 0; ii < 16; ++ii) {
  1550. int l = nearest_int((x[16*j + ii] + m)/d);
  1551. l = MAX(0, MIN(3, l));
  1552. L[16*j + ii] = l;
  1553. }
  1554. }
  1555. }
  1556. #if QK_K == 256
  1557. for (int j = 0; j < QK_K; j += 128) {
  1558. for (int l = 0; l < 32; ++l) {
  1559. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1560. }
  1561. }
  1562. #else
  1563. for (int l = 0; l < 16; ++l) {
  1564. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1565. }
  1566. #endif
  1567. x += QK_K;
  1568. }
  1569. }
  1570. size_t quantize_q2_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1571. (void)hist;
  1572. size_t row_size = ggml_row_size(GGML_TYPE_Q2_K, n_per_row);
  1573. if (!quant_weights) {
  1574. quantize_row_q2_K_reference(src, dst, nrow*n_per_row);
  1575. }
  1576. else {
  1577. char * qrow = (char *)dst;
  1578. for (int row = 0; row < nrow; ++row) {
  1579. quantize_row_q2_K_impl(src, (block_q2_K*)qrow, n_per_row, quant_weights);
  1580. src += n_per_row;
  1581. qrow += row_size;
  1582. }
  1583. }
  1584. return nrow * row_size;
  1585. }
  1586. //========================= 3-bit (de)-quantization
  1587. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1588. assert(k % QK_K == 0);
  1589. const int nb = k / QK_K;
  1590. int8_t L[QK_K];
  1591. float scales[QK_K / 16];
  1592. for (int i = 0; i < nb; i++) {
  1593. float max_scale = 0;
  1594. float amax = 0;
  1595. for (int j = 0; j < QK_K/16; ++j) {
  1596. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1597. float scale = fabsf(scales[j]);
  1598. if (scale > amax) {
  1599. amax = scale; max_scale = scales[j];
  1600. }
  1601. }
  1602. #if QK_K == 256
  1603. memset(y[i].scales, 0, 12);
  1604. if (max_scale) {
  1605. float iscale = -32.f/max_scale;
  1606. for (int j = 0; j < QK_K/16; ++j) {
  1607. int8_t l = nearest_int(iscale*scales[j]);
  1608. l = MAX(-32, MIN(31, l)) + 32;
  1609. if (j < 8) {
  1610. y[i].scales[j] = l & 0xF;
  1611. } else {
  1612. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1613. }
  1614. l >>= 4;
  1615. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1616. }
  1617. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1618. } else {
  1619. y[i].d = GGML_FP32_TO_FP16(0.f);
  1620. }
  1621. int8_t sc;
  1622. for (int j = 0; j < QK_K/16; ++j) {
  1623. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1624. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1625. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1626. if (!d) {
  1627. continue;
  1628. }
  1629. for (int ii = 0; ii < 16; ++ii) {
  1630. int l = nearest_int(x[16*j + ii]/d);
  1631. l = MAX(-4, MIN(3, l));
  1632. L[16*j + ii] = l + 4;
  1633. }
  1634. }
  1635. #else
  1636. if (max_scale) {
  1637. float iscale = -8.f/max_scale;
  1638. for (int j = 0; j < QK_K/16; j+=2) {
  1639. int l1 = nearest_int(iscale*scales[j]);
  1640. l1 = 8 + MAX(-8, MIN(7, l1));
  1641. int l2 = nearest_int(iscale*scales[j+1]);
  1642. l2 = 8 + MAX(-8, MIN(7, l2));
  1643. y[i].scales[j/2] = l1 | (l2 << 4);
  1644. }
  1645. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1646. } else {
  1647. for (int j = 0; j < QK_K/16; j+=2) {
  1648. y[i].scales[j/2] = 0;
  1649. }
  1650. y[i].d = GGML_FP32_TO_FP16(0.f);
  1651. }
  1652. for (int j = 0; j < QK_K/16; ++j) {
  1653. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1654. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1655. if (!d) {
  1656. continue;
  1657. }
  1658. for (int ii = 0; ii < 16; ++ii) {
  1659. int l = nearest_int(x[16*j + ii]/d);
  1660. l = MAX(-4, MIN(3, l));
  1661. L[16*j + ii] = l + 4;
  1662. }
  1663. }
  1664. #endif
  1665. memset(y[i].hmask, 0, QK_K/8);
  1666. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1667. int m = 0;
  1668. uint8_t hm = 1;
  1669. for (int j = 0; j < QK_K; ++j) {
  1670. if (L[j] > 3) {
  1671. y[i].hmask[m] |= hm;
  1672. L[j] -= 4;
  1673. }
  1674. if (++m == QK_K/8) {
  1675. m = 0; hm <<= 1;
  1676. }
  1677. }
  1678. #if QK_K == 256
  1679. for (int j = 0; j < QK_K; j += 128) {
  1680. for (int l = 0; l < 32; ++l) {
  1681. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1682. }
  1683. }
  1684. #else
  1685. for (int l = 0; l < 16; ++l) {
  1686. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1687. }
  1688. #endif
  1689. x += QK_K;
  1690. }
  1691. }
  1692. #if QK_K == 256
  1693. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1694. assert(k % QK_K == 0);
  1695. const int nb = k / QK_K;
  1696. const uint32_t kmask1 = 0x03030303;
  1697. const uint32_t kmask2 = 0x0f0f0f0f;
  1698. uint32_t aux[4];
  1699. const int8_t * scales = (const int8_t*)aux;
  1700. for (int i = 0; i < nb; i++) {
  1701. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1702. const uint8_t * restrict q = x[i].qs;
  1703. const uint8_t * restrict hm = x[i].hmask;
  1704. uint8_t m = 1;
  1705. memcpy(aux, x[i].scales, 12);
  1706. uint32_t tmp = aux[2];
  1707. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1708. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1709. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1710. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1711. int is = 0;
  1712. float dl;
  1713. for (int n = 0; n < QK_K; n += 128) {
  1714. int shift = 0;
  1715. for (int j = 0; j < 4; ++j) {
  1716. dl = d_all * (scales[is++] - 32);
  1717. for (int l = 0; l < 16; ++l) {
  1718. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1719. }
  1720. dl = d_all * (scales[is++] - 32);
  1721. for (int l = 0; l < 16; ++l) {
  1722. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1723. }
  1724. shift += 2;
  1725. m <<= 1;
  1726. }
  1727. q += 32;
  1728. }
  1729. }
  1730. }
  1731. #else
  1732. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1733. assert(k % QK_K == 0);
  1734. assert(QK_K == 64);
  1735. const int nb = k / QK_K;
  1736. for (int i = 0; i < nb; i++) {
  1737. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1738. const uint8_t * restrict q = x[i].qs;
  1739. const uint8_t * restrict hm = x[i].hmask;
  1740. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1741. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1742. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1743. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1744. for (int l=0; l<8; ++l) {
  1745. uint8_t h = hm[l];
  1746. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1747. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1748. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1749. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1750. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1751. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1752. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1753. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1754. }
  1755. y += QK_K;
  1756. }
  1757. }
  1758. #endif
  1759. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1760. quantize_row_q3_K_reference(x, vy, k);
  1761. }
  1762. size_t ggml_quantize_q3_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1763. (void)hist; // TODO: collect histograms
  1764. for (int j = 0; j < n; j += k) {
  1765. block_q3_K * restrict y = (block_q3_K *)dst + j/QK_K;
  1766. quantize_row_q3_K_reference(src + j, y, k);
  1767. }
  1768. return (n/QK_K*sizeof(block_q3_K));
  1769. }
  1770. static void quantize_row_q3_K_impl(const float * restrict x, block_q3_K * restrict y, int n_per_row, const float * restrict quant_weights) {
  1771. #if QK_K != 256
  1772. (void)quant_weights;
  1773. quantize_row_q3_K_reference(x, y, n_per_row);
  1774. #else
  1775. assert(n_per_row % QK_K == 0);
  1776. const int nb = n_per_row / QK_K;
  1777. int8_t L[QK_K];
  1778. float scales[QK_K / 16];
  1779. float weight[16];
  1780. float sw[QK_K / 16];
  1781. int8_t Ls[QK_K / 16];
  1782. for (int i = 0; i < nb; i++) {
  1783. float sumx2 = 0;
  1784. for (int j = 0; j < QK_K; ++j) sumx2 += x[j]*x[j];
  1785. float sigma2 = 2*sumx2/QK_K;
  1786. for (int j = 0; j < QK_K/16; ++j) {
  1787. if (quant_weights) {
  1788. const float * qw = quant_weights ? quant_weights + QK_K * i + 16*j : NULL;
  1789. for (int l = 0; l < 16; ++l) weight[l] = qw[l] * sqrtf(sigma2 + x[16*j+l]*x[16*j+l]);
  1790. } else {
  1791. for (int l = 0; l < 16; ++l) weight[l] = x[16*j+l]*x[16*j+l];
  1792. }
  1793. float sumw = 0;
  1794. for (int l = 0; l < 16; ++l) sumw += weight[l];
  1795. sw[j] = sumw;
  1796. scales[j] = make_qx_quants(16, 4, x + 16*j, L + 16*j, 1, weight);
  1797. }
  1798. memset(y[i].scales, 0, 12);
  1799. float d_block = make_qx_quants(QK_K/16, 32, scales, Ls, 1, sw);
  1800. for (int j = 0; j < QK_K/16; ++j) {
  1801. int l = Ls[j];
  1802. if (j < 8) {
  1803. y[i].scales[j] = l & 0xF;
  1804. } else {
  1805. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1806. }
  1807. l >>= 4;
  1808. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1809. }
  1810. y[i].d = GGML_FP32_TO_FP16(d_block);
  1811. int8_t sc;
  1812. for (int j = 0; j < QK_K/16; ++j) {
  1813. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1814. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1815. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1816. if (!d) {
  1817. continue;
  1818. }
  1819. for (int ii = 0; ii < 16; ++ii) {
  1820. int l = nearest_int(x[16*j + ii]/d);
  1821. l = MAX(-4, MIN(3, l));
  1822. L[16*j + ii] = l + 4;
  1823. }
  1824. }
  1825. memset(y[i].hmask, 0, QK_K/8);
  1826. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1827. int m = 0;
  1828. uint8_t hm = 1;
  1829. for (int j = 0; j < QK_K; ++j) {
  1830. if (L[j] > 3) {
  1831. y[i].hmask[m] |= hm;
  1832. L[j] -= 4;
  1833. }
  1834. if (++m == QK_K/8) {
  1835. m = 0; hm <<= 1;
  1836. }
  1837. }
  1838. for (int j = 0; j < QK_K; j += 128) {
  1839. for (int l = 0; l < 32; ++l) {
  1840. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1841. }
  1842. }
  1843. x += QK_K;
  1844. }
  1845. #endif
  1846. }
  1847. size_t quantize_q3_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  1848. (void)hist;
  1849. size_t row_size = ggml_row_size(GGML_TYPE_Q3_K, n_per_row);
  1850. if (!quant_weights) {
  1851. quantize_row_q3_K_reference(src, dst, nrow*n_per_row);
  1852. }
  1853. else {
  1854. char * qrow = (char *)dst;
  1855. for (int row = 0; row < nrow; ++row) {
  1856. quantize_row_q3_K_impl(src, (block_q3_K*)qrow, n_per_row, quant_weights);
  1857. src += n_per_row;
  1858. qrow += row_size;
  1859. }
  1860. }
  1861. return nrow * row_size;
  1862. }
  1863. // ====================== 4-bit (de)-quantization
  1864. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1865. assert(k % QK_K == 0);
  1866. const int nb = k / QK_K;
  1867. uint8_t L[QK_K];
  1868. uint8_t Laux[32];
  1869. float weights[32];
  1870. float mins[QK_K/32];
  1871. float scales[QK_K/32];
  1872. for (int i = 0; i < nb; i++) {
  1873. float max_scale = 0; // as we are deducting the min, scales are always positive
  1874. float max_min = 0;
  1875. for (int j = 0; j < QK_K/32; ++j) {
  1876. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1877. float sum_x2 = 0;
  1878. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1879. float av_x = sqrtf(sum_x2/32);
  1880. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1881. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1882. float scale = scales[j];
  1883. if (scale > max_scale) {
  1884. max_scale = scale;
  1885. }
  1886. float min = mins[j];
  1887. if (min > max_min) {
  1888. max_min = min;
  1889. }
  1890. }
  1891. #if QK_K == 256
  1892. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1893. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1894. for (int j = 0; j < QK_K/32; ++j) {
  1895. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1896. uint8_t lm = nearest_int(inv_min*mins[j]);
  1897. ls = MIN(63, ls);
  1898. lm = MIN(63, lm);
  1899. if (j < 4) {
  1900. y[i].scales[j] = ls;
  1901. y[i].scales[j+4] = lm;
  1902. } else {
  1903. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1904. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1905. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1906. }
  1907. }
  1908. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1909. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1910. uint8_t sc, m;
  1911. for (int j = 0; j < QK_K/32; ++j) {
  1912. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1913. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1914. if (!d) continue;
  1915. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1916. for (int ii = 0; ii < 32; ++ii) {
  1917. int l = nearest_int((x[32*j + ii] + dm)/d);
  1918. l = MAX(0, MIN(15, l));
  1919. L[32*j + ii] = l;
  1920. }
  1921. }
  1922. #else
  1923. const float s_factor = 15.f;
  1924. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1925. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1926. int d1 = nearest_int(inv_scale*scales[0]);
  1927. int m1 = nearest_int(inv_min*mins[0]);
  1928. int d2 = nearest_int(inv_scale*scales[1]);
  1929. int m2 = nearest_int(inv_min*mins[1]);
  1930. y[i].scales[0] = d1 | (m1 << 4);
  1931. y[i].scales[1] = d2 | (m2 << 4);
  1932. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1933. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1934. float sumlx = 0;
  1935. int suml2 = 0;
  1936. for (int j = 0; j < QK_K/32; ++j) {
  1937. const uint8_t sd = y[i].scales[j] & 0xF;
  1938. const uint8_t sm = y[i].scales[j] >> 4;
  1939. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1940. if (!d) continue;
  1941. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1942. for (int ii = 0; ii < 32; ++ii) {
  1943. int l = nearest_int((x[32*j + ii] + m)/d);
  1944. l = MAX(0, MIN(15, l));
  1945. L[32*j + ii] = l;
  1946. sumlx += (x[32*j + ii] + m)*l*sd;
  1947. suml2 += l*l*sd*sd;
  1948. }
  1949. }
  1950. if (suml2) {
  1951. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1952. }
  1953. #endif
  1954. uint8_t * q = y[i].qs;
  1955. for (int j = 0; j < QK_K; j += 64) {
  1956. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1957. q += 32;
  1958. }
  1959. x += QK_K;
  1960. }
  1961. }
  1962. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  1963. assert(k % QK_K == 0);
  1964. const int nb = k / QK_K;
  1965. for (int i = 0; i < nb; i++) {
  1966. const uint8_t * q = x[i].qs;
  1967. #if QK_K == 256
  1968. const float d = GGML_FP16_TO_FP32(x[i].d);
  1969. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1970. int is = 0;
  1971. uint8_t sc, m;
  1972. for (int j = 0; j < QK_K; j += 64) {
  1973. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1974. const float d1 = d * sc; const float m1 = min * m;
  1975. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1976. const float d2 = d * sc; const float m2 = min * m;
  1977. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1978. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1979. q += 32; is += 2;
  1980. }
  1981. #else
  1982. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1983. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1984. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1985. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1986. for (int l = 0; l < 32; ++l) {
  1987. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1988. y[l+32] = d2 * (q[l] >> 4) - m2;
  1989. }
  1990. y += QK_K;
  1991. #endif
  1992. }
  1993. }
  1994. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  1995. assert(k % QK_K == 0);
  1996. block_q4_K * restrict y = vy;
  1997. quantize_row_q4_K_reference(x, y, k);
  1998. }
  1999. size_t ggml_quantize_q4_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  2000. assert(k % QK_K == 0);
  2001. (void)hist; // TODO: collect histograms
  2002. for (int j = 0; j < n; j += k) {
  2003. block_q4_K * restrict y = (block_q4_K *)dst + j/QK_K;
  2004. quantize_row_q4_K_reference(src + j, y, k);
  2005. }
  2006. return (n/QK_K*sizeof(block_q4_K));
  2007. }
  2008. static void quantize_row_q4_K_impl(const float * restrict x, block_q4_K * restrict y, int n_per_row, const float * quant_weights) {
  2009. #if QK_K != 256
  2010. (void)quant_weights;
  2011. quantize_row_q4_K_reference(x, y, n_per_row);
  2012. #else
  2013. assert(n_per_row % QK_K == 0);
  2014. const int nb = n_per_row / QK_K;
  2015. uint8_t L[QK_K];
  2016. uint8_t Laux[32];
  2017. float weights[32];
  2018. float mins[QK_K/32];
  2019. float scales[QK_K/32];
  2020. for (int i = 0; i < nb; i++) {
  2021. float sum_x2 = 0;
  2022. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2023. float sigma2 = sum_x2/QK_K;
  2024. float av_x = sqrtf(sigma2);
  2025. float max_scale = 0; // as we are deducting the min, scales are always positive
  2026. float max_min = 0;
  2027. for (int j = 0; j < QK_K/32; ++j) {
  2028. if (quant_weights) {
  2029. const float * qw = quant_weights + QK_K*i + 32*j;
  2030. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2031. } else {
  2032. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2033. }
  2034. scales[j] = make_qkx3_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2035. //scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  2036. float scale = scales[j];
  2037. if (scale > max_scale) {
  2038. max_scale = scale;
  2039. }
  2040. float min = mins[j];
  2041. if (min > max_min) {
  2042. max_min = min;
  2043. }
  2044. }
  2045. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2046. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2047. for (int j = 0; j < QK_K/32; ++j) {
  2048. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2049. uint8_t lm = nearest_int(inv_min*mins[j]);
  2050. ls = MIN(63, ls);
  2051. lm = MIN(63, lm);
  2052. if (j < 4) {
  2053. y[i].scales[j] = ls;
  2054. y[i].scales[j+4] = lm;
  2055. } else {
  2056. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2057. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2058. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2059. }
  2060. }
  2061. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2062. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2063. uint8_t sc, m;
  2064. for (int j = 0; j < QK_K/32; ++j) {
  2065. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2066. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2067. if (!d) continue;
  2068. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2069. for (int ii = 0; ii < 32; ++ii) {
  2070. int l = nearest_int((x[32*j + ii] + dm)/d);
  2071. l = MAX(0, MIN(15, l));
  2072. L[32*j + ii] = l;
  2073. }
  2074. }
  2075. uint8_t * q = y[i].qs;
  2076. for (int j = 0; j < QK_K; j += 64) {
  2077. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  2078. q += 32;
  2079. }
  2080. x += QK_K;
  2081. }
  2082. #endif
  2083. }
  2084. size_t quantize_q4_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2085. (void)hist;
  2086. size_t row_size = ggml_row_size(GGML_TYPE_Q4_K, n_per_row);
  2087. if (!quant_weights) {
  2088. quantize_row_q4_K_reference(src, dst, nrow*n_per_row);
  2089. }
  2090. else {
  2091. char * qrow = (char *)dst;
  2092. for (int row = 0; row < nrow; ++row) {
  2093. quantize_row_q4_K_impl(src, (block_q4_K*)qrow, n_per_row, quant_weights);
  2094. src += n_per_row;
  2095. qrow += row_size;
  2096. }
  2097. }
  2098. return nrow * row_size;
  2099. }
  2100. // ====================== 5-bit (de)-quantization
  2101. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  2102. assert(k % QK_K == 0);
  2103. const int nb = k / QK_K;
  2104. #if QK_K == 256
  2105. uint8_t L[QK_K];
  2106. float mins[QK_K/32];
  2107. float scales[QK_K/32];
  2108. float weights[32];
  2109. uint8_t Laux[32];
  2110. #else
  2111. int8_t L[QK_K];
  2112. float scales[QK_K/16];
  2113. #endif
  2114. for (int i = 0; i < nb; i++) {
  2115. #if QK_K == 256
  2116. float max_scale = 0; // as we are deducting the min, scales are always positive
  2117. float max_min = 0;
  2118. for (int j = 0; j < QK_K/32; ++j) {
  2119. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  2120. float sum_x2 = 0;
  2121. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  2122. float av_x = sqrtf(sum_x2/32);
  2123. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2124. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  2125. float scale = scales[j];
  2126. if (scale > max_scale) {
  2127. max_scale = scale;
  2128. }
  2129. float min = mins[j];
  2130. if (min > max_min) {
  2131. max_min = min;
  2132. }
  2133. }
  2134. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2135. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2136. for (int j = 0; j < QK_K/32; ++j) {
  2137. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2138. uint8_t lm = nearest_int(inv_min*mins[j]);
  2139. ls = MIN(63, ls);
  2140. lm = MIN(63, lm);
  2141. if (j < 4) {
  2142. y[i].scales[j] = ls;
  2143. y[i].scales[j+4] = lm;
  2144. } else {
  2145. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2146. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2147. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2148. }
  2149. }
  2150. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2151. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2152. uint8_t sc, m;
  2153. for (int j = 0; j < QK_K/32; ++j) {
  2154. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2155. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2156. if (!d) continue;
  2157. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2158. for (int ii = 0; ii < 32; ++ii) {
  2159. int l = nearest_int((x[32*j + ii] + dm)/d);
  2160. l = MAX(0, MIN(31, l));
  2161. L[32*j + ii] = l;
  2162. }
  2163. }
  2164. uint8_t * restrict qh = y[i].qh;
  2165. uint8_t * restrict ql = y[i].qs;
  2166. memset(qh, 0, QK_K/8);
  2167. uint8_t m1 = 1, m2 = 2;
  2168. for (int n = 0; n < QK_K; n += 64) {
  2169. for (int j = 0; j < 32; ++j) {
  2170. int l1 = L[n + j];
  2171. if (l1 > 15) {
  2172. l1 -= 16; qh[j] |= m1;
  2173. }
  2174. int l2 = L[n + j + 32];
  2175. if (l2 > 15) {
  2176. l2 -= 16; qh[j] |= m2;
  2177. }
  2178. ql[j] = l1 | (l2 << 4);
  2179. }
  2180. m1 <<= 2; m2 <<= 2;
  2181. ql += 32;
  2182. }
  2183. #else
  2184. float max_scale = 0, amax = 0;
  2185. for (int j = 0; j < QK_K/16; ++j) {
  2186. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1, NULL);
  2187. float abs_scale = fabsf(scales[j]);
  2188. if (abs_scale > amax) {
  2189. amax = abs_scale;
  2190. max_scale = scales[j];
  2191. }
  2192. }
  2193. float iscale = -128.f/max_scale;
  2194. for (int j = 0; j < QK_K/16; ++j) {
  2195. int l = nearest_int(iscale*scales[j]);
  2196. y[i].scales[j] = MAX(-128, MIN(127, l));
  2197. }
  2198. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2199. for (int j = 0; j < QK_K/16; ++j) {
  2200. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2201. if (!d) continue;
  2202. for (int ii = 0; ii < 16; ++ii) {
  2203. int l = nearest_int(x[16*j + ii]/d);
  2204. l = MAX(-16, MIN(15, l));
  2205. L[16*j + ii] = l + 16;
  2206. }
  2207. }
  2208. uint8_t * restrict qh = y[i].qh;
  2209. uint8_t * restrict ql = y[i].qs;
  2210. memset(qh, 0, QK_K/8);
  2211. for (int j = 0; j < 32; ++j) {
  2212. int jm = j%8;
  2213. int is = j/8;
  2214. int l1 = L[j];
  2215. if (l1 > 15) {
  2216. l1 -= 16; qh[jm] |= (1 << is);
  2217. }
  2218. int l2 = L[j + 32];
  2219. if (l2 > 15) {
  2220. l2 -= 16; qh[jm] |= (1 << (4 + is));
  2221. }
  2222. ql[j] = l1 | (l2 << 4);
  2223. }
  2224. #endif
  2225. x += QK_K;
  2226. }
  2227. }
  2228. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  2229. assert(k % QK_K == 0);
  2230. const int nb = k / QK_K;
  2231. for (int i = 0; i < nb; i++) {
  2232. const uint8_t * ql = x[i].qs;
  2233. const uint8_t * qh = x[i].qh;
  2234. #if QK_K == 256
  2235. const float d = GGML_FP16_TO_FP32(x[i].d);
  2236. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  2237. int is = 0;
  2238. uint8_t sc, m;
  2239. uint8_t u1 = 1, u2 = 2;
  2240. for (int j = 0; j < QK_K; j += 64) {
  2241. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  2242. const float d1 = d * sc; const float m1 = min * m;
  2243. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  2244. const float d2 = d * sc; const float m2 = min * m;
  2245. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  2246. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  2247. ql += 32; is += 2;
  2248. u1 <<= 2; u2 <<= 2;
  2249. }
  2250. #else
  2251. float d = GGML_FP16_TO_FP32(x[i].d);
  2252. const int8_t * restrict s = x[i].scales;
  2253. for (int l = 0; l < 8; ++l) {
  2254. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  2255. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  2256. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  2257. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  2258. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  2259. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  2260. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  2261. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  2262. }
  2263. y += QK_K;
  2264. #endif
  2265. }
  2266. }
  2267. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  2268. assert(k % QK_K == 0);
  2269. block_q5_K * restrict y = vy;
  2270. quantize_row_q5_K_reference(x, y, k);
  2271. }
  2272. size_t ggml_quantize_q5_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  2273. assert(k % QK_K == 0);
  2274. (void)hist; // TODO: collect histograms
  2275. for (int j = 0; j < n; j += k) {
  2276. block_q5_K * restrict y = (block_q5_K *)dst + j/QK_K;
  2277. quantize_row_q5_K_reference(src + j, y, k);
  2278. }
  2279. return (n/QK_K*sizeof(block_q5_K));
  2280. }
  2281. static void quantize_row_q5_K_impl(const float * restrict x, block_q5_K * restrict y, int n_per_row, const float * quant_weights) {
  2282. #if QK_K != 256
  2283. (void)quant_weights;
  2284. quantize_row_q5_K_reference(x, y, n_per_row);
  2285. #else
  2286. assert(n_per_row % QK_K == 0);
  2287. const int nb = n_per_row / QK_K;
  2288. uint8_t L[QK_K];
  2289. float mins[QK_K/32];
  2290. float scales[QK_K/32];
  2291. float weights[32];
  2292. uint8_t Laux[32];
  2293. for (int i = 0; i < nb; i++) {
  2294. float sum_x2 = 0;
  2295. for (int l = 0; l < QK_K; ++l) sum_x2 += x[l] * x[l];
  2296. float sigma2 = sum_x2/QK_K;
  2297. float av_x = sqrtf(sigma2);
  2298. float max_scale = 0; // as we are deducting the min, scales are always positive
  2299. float max_min = 0;
  2300. for (int j = 0; j < QK_K/32; ++j) {
  2301. if (quant_weights) {
  2302. const float * qw = quant_weights + QK_K*i + 32*j;
  2303. for (int l = 0; l < 32; ++l) weights[l] = qw[l] * sqrtf(sigma2 + x[32*j + l]*x[32*j + l]);
  2304. } else {
  2305. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  2306. }
  2307. scales[j] = make_qkx3_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.9f, 0.05f, 36, false);
  2308. float scale = scales[j];
  2309. if (scale > max_scale) {
  2310. max_scale = scale;
  2311. }
  2312. float min = mins[j];
  2313. if (min > max_min) {
  2314. max_min = min;
  2315. }
  2316. }
  2317. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  2318. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  2319. for (int j = 0; j < QK_K/32; ++j) {
  2320. uint8_t ls = nearest_int(inv_scale*scales[j]);
  2321. uint8_t lm = nearest_int(inv_min*mins[j]);
  2322. ls = MIN(63, ls);
  2323. lm = MIN(63, lm);
  2324. if (j < 4) {
  2325. y[i].scales[j] = ls;
  2326. y[i].scales[j+4] = lm;
  2327. } else {
  2328. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  2329. y[i].scales[j-4] |= ((ls >> 4) << 6);
  2330. y[i].scales[j-0] |= ((lm >> 4) << 6);
  2331. }
  2332. }
  2333. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  2334. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  2335. uint8_t sc, m;
  2336. for (int j = 0; j < QK_K/32; ++j) {
  2337. get_scale_min_k4(j, y[i].scales, &sc, &m);
  2338. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  2339. if (!d) continue;
  2340. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  2341. for (int ii = 0; ii < 32; ++ii) {
  2342. int l = nearest_int((x[32*j + ii] + dm)/d);
  2343. l = MAX(0, MIN(31, l));
  2344. L[32*j + ii] = l;
  2345. }
  2346. }
  2347. uint8_t * restrict qh = y[i].qh;
  2348. uint8_t * restrict ql = y[i].qs;
  2349. memset(qh, 0, QK_K/8);
  2350. uint8_t m1 = 1, m2 = 2;
  2351. for (int n = 0; n < QK_K; n += 64) {
  2352. for (int j = 0; j < 32; ++j) {
  2353. int l1 = L[n + j];
  2354. if (l1 > 15) {
  2355. l1 -= 16; qh[j] |= m1;
  2356. }
  2357. int l2 = L[n + j + 32];
  2358. if (l2 > 15) {
  2359. l2 -= 16; qh[j] |= m2;
  2360. }
  2361. ql[j] = l1 | (l2 << 4);
  2362. }
  2363. m1 <<= 2; m2 <<= 2;
  2364. ql += 32;
  2365. }
  2366. x += QK_K;
  2367. }
  2368. #endif
  2369. }
  2370. size_t quantize_q5_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2371. (void)hist;
  2372. size_t row_size = ggml_row_size(GGML_TYPE_Q5_K, n_per_row);
  2373. if (!quant_weights) {
  2374. quantize_row_q5_K_reference(src, dst, nrow*n_per_row);
  2375. }
  2376. else {
  2377. char * qrow = (char *)dst;
  2378. for (int row = 0; row < nrow; ++row) {
  2379. quantize_row_q5_K_impl(src, (block_q5_K*)qrow, n_per_row, quant_weights);
  2380. src += n_per_row;
  2381. qrow += row_size;
  2382. }
  2383. }
  2384. return nrow * row_size;
  2385. }
  2386. // ====================== 6-bit (de)-quantization
  2387. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  2388. assert(k % QK_K == 0);
  2389. const int nb = k / QK_K;
  2390. int8_t L[QK_K];
  2391. float scales[QK_K/16];
  2392. for (int i = 0; i < nb; i++) {
  2393. float max_scale = 0;
  2394. float max_abs_scale = 0;
  2395. for (int ib = 0; ib < QK_K/16; ++ib) {
  2396. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2397. scales[ib] = scale;
  2398. const float abs_scale = fabsf(scale);
  2399. if (abs_scale > max_abs_scale) {
  2400. max_abs_scale = abs_scale;
  2401. max_scale = scale;
  2402. }
  2403. }
  2404. if (!max_abs_scale) {
  2405. memset(&y[i], 0, sizeof(block_q6_K));
  2406. y[i].d = GGML_FP32_TO_FP16(0.f);
  2407. x += QK_K;
  2408. continue;
  2409. }
  2410. float iscale = -128.f/max_scale;
  2411. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2412. for (int ib = 0; ib < QK_K/16; ++ib) {
  2413. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2414. }
  2415. for (int j = 0; j < QK_K/16; ++j) {
  2416. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2417. if (!d) {
  2418. continue;
  2419. }
  2420. for (int ii = 0; ii < 16; ++ii) {
  2421. int l = nearest_int(x[16*j + ii]/d);
  2422. l = MAX(-32, MIN(31, l));
  2423. L[16*j + ii] = l + 32;
  2424. }
  2425. }
  2426. uint8_t * restrict ql = y[i].ql;
  2427. uint8_t * restrict qh = y[i].qh;
  2428. #if QK_K == 256
  2429. for (int j = 0; j < QK_K; j += 128) {
  2430. for (int l = 0; l < 32; ++l) {
  2431. const uint8_t q1 = L[j + l + 0] & 0xF;
  2432. const uint8_t q2 = L[j + l + 32] & 0xF;
  2433. const uint8_t q3 = L[j + l + 64] & 0xF;
  2434. const uint8_t q4 = L[j + l + 96] & 0xF;
  2435. ql[l+ 0] = q1 | (q3 << 4);
  2436. ql[l+32] = q2 | (q4 << 4);
  2437. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2438. }
  2439. ql += 64;
  2440. qh += 32;
  2441. }
  2442. #else
  2443. for (int l = 0; l < 32; ++l) {
  2444. const uint8_t q1 = L[l + 0] & 0xF;
  2445. const uint8_t q2 = L[l + 32] & 0xF;
  2446. ql[l] = q1 | (q2 << 4);
  2447. }
  2448. for (int l = 0; l < 16; ++l) {
  2449. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  2450. }
  2451. #endif
  2452. x += QK_K;
  2453. }
  2454. }
  2455. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  2456. assert(k % QK_K == 0);
  2457. const int nb = k / QK_K;
  2458. for (int i = 0; i < nb; i++) {
  2459. const float d = GGML_FP16_TO_FP32(x[i].d);
  2460. const uint8_t * restrict ql = x[i].ql;
  2461. const uint8_t * restrict qh = x[i].qh;
  2462. const int8_t * restrict sc = x[i].scales;
  2463. #if QK_K == 256
  2464. for (int n = 0; n < QK_K; n += 128) {
  2465. for (int l = 0; l < 32; ++l) {
  2466. int is = l/16;
  2467. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2468. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2469. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2470. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2471. y[l + 0] = d * sc[is + 0] * q1;
  2472. y[l + 32] = d * sc[is + 2] * q2;
  2473. y[l + 64] = d * sc[is + 4] * q3;
  2474. y[l + 96] = d * sc[is + 6] * q4;
  2475. }
  2476. y += 128;
  2477. ql += 64;
  2478. qh += 32;
  2479. sc += 8;
  2480. }
  2481. #else
  2482. for (int l = 0; l < 16; ++l) {
  2483. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  2484. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  2485. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  2486. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  2487. y[l+ 0] = d * sc[0] * q1;
  2488. y[l+16] = d * sc[1] * q2;
  2489. y[l+32] = d * sc[2] * q3;
  2490. y[l+48] = d * sc[3] * q4;
  2491. }
  2492. y += 64;
  2493. #endif
  2494. }
  2495. }
  2496. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  2497. assert(k % QK_K == 0);
  2498. block_q6_K * restrict y = vy;
  2499. quantize_row_q6_K_reference(x, y, k);
  2500. }
  2501. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist) {
  2502. assert(k % QK_K == 0);
  2503. (void)hist; // TODO: collect histograms
  2504. for (int j = 0; j < n; j += k) {
  2505. block_q6_K * restrict y = (block_q6_K *)dst + j/QK_K;
  2506. quantize_row_q6_K_reference(src + j, y, k);
  2507. }
  2508. return (n/QK_K*sizeof(block_q6_K));
  2509. }
  2510. static void quantize_row_q6_K_impl(const float * restrict x, block_q6_K * restrict y, int n_per_row, const float * quant_weights) {
  2511. #if QK_K != 256
  2512. (void)quant_weights;
  2513. quantize_row_q6_K_reference(x, y, n_per_row);
  2514. #else
  2515. assert(n_per_row % QK_K == 0);
  2516. const int nb = n_per_row / QK_K;
  2517. int8_t L[QK_K];
  2518. float scales[QK_K/16];
  2519. //float weights[16];
  2520. for (int i = 0; i < nb; i++) {
  2521. //float sum_x2 = 0;
  2522. //for (int j = 0; j < QK_K; ++j) sum_x2 += x[j]*x[j];
  2523. //float sigma2 = sum_x2/QK_K;
  2524. float max_scale = 0;
  2525. float max_abs_scale = 0;
  2526. for (int ib = 0; ib < QK_K/16; ++ib) {
  2527. float scale;
  2528. if (quant_weights) {
  2529. const float * qw = quant_weights + QK_K*i + 16*ib;
  2530. //for (int j = 0; j < 16; ++j) weights[j] = qw[j] * sqrtf(sigma2 + x[16*ib + j]*x[16*ib + j]);
  2531. //scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, weights);
  2532. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, qw);
  2533. } else {
  2534. scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1, NULL);
  2535. }
  2536. scales[ib] = scale;
  2537. const float abs_scale = fabsf(scale);
  2538. if (abs_scale > max_abs_scale) {
  2539. max_abs_scale = abs_scale;
  2540. max_scale = scale;
  2541. }
  2542. }
  2543. if (!max_abs_scale) {
  2544. memset(&y[i], 0, sizeof(block_q6_K));
  2545. y[i].d = GGML_FP32_TO_FP16(0.f);
  2546. x += QK_K;
  2547. continue;
  2548. }
  2549. float iscale = -128.f/max_scale;
  2550. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  2551. for (int ib = 0; ib < QK_K/16; ++ib) {
  2552. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  2553. }
  2554. for (int j = 0; j < QK_K/16; ++j) {
  2555. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  2556. if (!d) {
  2557. continue;
  2558. }
  2559. for (int ii = 0; ii < 16; ++ii) {
  2560. int l = nearest_int(x[16*j + ii]/d);
  2561. l = MAX(-32, MIN(31, l));
  2562. L[16*j + ii] = l + 32;
  2563. }
  2564. }
  2565. uint8_t * restrict ql = y[i].ql;
  2566. uint8_t * restrict qh = y[i].qh;
  2567. for (int j = 0; j < QK_K; j += 128) {
  2568. for (int l = 0; l < 32; ++l) {
  2569. const uint8_t q1 = L[j + l + 0] & 0xF;
  2570. const uint8_t q2 = L[j + l + 32] & 0xF;
  2571. const uint8_t q3 = L[j + l + 64] & 0xF;
  2572. const uint8_t q4 = L[j + l + 96] & 0xF;
  2573. ql[l+ 0] = q1 | (q3 << 4);
  2574. ql[l+32] = q2 | (q4 << 4);
  2575. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  2576. }
  2577. ql += 64;
  2578. qh += 32;
  2579. }
  2580. x += QK_K;
  2581. }
  2582. #endif
  2583. }
  2584. size_t quantize_q6_K(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2585. (void)hist;
  2586. size_t row_size = ggml_row_size(GGML_TYPE_Q6_K, n_per_row);
  2587. if (!quant_weights) {
  2588. quantize_row_q6_K_reference(src, dst, nrow*n_per_row);
  2589. }
  2590. else {
  2591. char * qrow = (char *)dst;
  2592. for (int row = 0; row < nrow; ++row) {
  2593. quantize_row_q6_K_impl(src, (block_q6_K*)qrow, n_per_row, quant_weights);
  2594. src += n_per_row;
  2595. qrow += row_size;
  2596. }
  2597. }
  2598. return nrow * row_size;
  2599. }
  2600. static void quantize_row_q4_0_impl(const float * restrict x, block_q4_0 * restrict y, int n_per_row, const float * quant_weights) {
  2601. static_assert(QK4_0 == 32, "QK4_0 must be 32");
  2602. if (!quant_weights) {
  2603. quantize_row_q4_0_reference(x, y, n_per_row);
  2604. return;
  2605. }
  2606. float weight[QK4_0];
  2607. int8_t L[QK4_0];
  2608. float sum_x2 = 0;
  2609. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2610. float sigma2 = sum_x2/n_per_row;
  2611. const int nb = n_per_row/QK4_0;
  2612. for (int ib = 0; ib < nb; ++ib) {
  2613. const float * xb = x + QK4_0 * ib;
  2614. const float * qw = quant_weights + QK4_0 * ib;
  2615. for (int j = 0; j < QK4_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2616. float d = make_qx_quants(QK4_0, 8, xb, L, 1, weight);
  2617. y[ib].d = GGML_FP32_TO_FP16(d);
  2618. for (int j = 0; j < 16; ++j) {
  2619. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2620. }
  2621. }
  2622. }
  2623. size_t quantize_q4_0(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2624. if (!quant_weights) {
  2625. return ggml_quantize_q4_0(src, dst, nrow*n_per_row, n_per_row, hist);
  2626. }
  2627. size_t row_size = ggml_row_size(GGML_TYPE_Q4_0, n_per_row);
  2628. char * qrow = (char *)dst;
  2629. for (int row = 0; row < nrow; ++row) {
  2630. quantize_row_q4_0_impl(src, (block_q4_0*)qrow, n_per_row, quant_weights);
  2631. src += n_per_row;
  2632. qrow += row_size;
  2633. }
  2634. return nrow * row_size;
  2635. }
  2636. static void quantize_row_q4_1_impl(const float * restrict x, block_q4_1 * restrict y, int n_per_row, const float * quant_weights) {
  2637. static_assert(QK4_1 == 32, "QK4_1 must be 32");
  2638. if (!quant_weights) {
  2639. quantize_row_q4_1_reference(x, y, n_per_row);
  2640. return;
  2641. }
  2642. float weight[QK4_1];
  2643. uint8_t L[QK4_1], Laux[QK4_1];
  2644. float sum_x2 = 0;
  2645. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2646. float sigma2 = sum_x2/n_per_row;
  2647. const int nb = n_per_row/QK4_1;
  2648. for (int ib = 0; ib < nb; ++ib) {
  2649. const float * xb = x + QK4_1 * ib;
  2650. const float * qw = quant_weights + QK4_1 * ib;
  2651. for (int j = 0; j < QK4_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2652. float min;
  2653. float d = make_qkx3_quants(QK4_1, 15, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2654. y[ib].d = GGML_FP32_TO_FP16(d);
  2655. y[ib].m = GGML_FP32_TO_FP16(-min);
  2656. for (int j = 0; j < 16; ++j) {
  2657. y[ib].qs[j] = L[j] | (L[j+16] << 4);
  2658. }
  2659. }
  2660. }
  2661. size_t quantize_q4_1(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2662. if (!quant_weights) {
  2663. return ggml_quantize_q4_1(src, dst, nrow*n_per_row, n_per_row, hist);
  2664. }
  2665. size_t row_size = ggml_row_size(GGML_TYPE_Q4_1, n_per_row);
  2666. char * qrow = (char *)dst;
  2667. for (int row = 0; row < nrow; ++row) {
  2668. quantize_row_q4_1_impl(src, (block_q4_1*)qrow, n_per_row, quant_weights);
  2669. src += n_per_row;
  2670. qrow += row_size;
  2671. }
  2672. return nrow * row_size;
  2673. }
  2674. static void quantize_row_q5_0_impl(const float * restrict x, block_q5_0 * restrict y, int n_per_row, const float * quant_weights) {
  2675. static_assert(QK5_0 == 32, "QK5_0 must be 32");
  2676. if (!quant_weights) {
  2677. quantize_row_q5_0_reference(x, y, n_per_row);
  2678. return;
  2679. }
  2680. float weight[QK5_0];
  2681. int8_t L[QK5_0];
  2682. float sum_x2 = 0;
  2683. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2684. float sigma2 = sum_x2/n_per_row;
  2685. const int nb = n_per_row/QK5_0;
  2686. for (int ib = 0; ib < nb; ++ib) {
  2687. const float * xb = x + QK5_0 * ib;
  2688. const float * qw = quant_weights + QK5_0 * ib;
  2689. for (int j = 0; j < QK5_0; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2690. float d = make_qx_quants(QK5_0, 16, xb, L, 1, weight);
  2691. y[ib].d = GGML_FP32_TO_FP16(d);
  2692. uint32_t qh = 0;
  2693. for (int j = 0; j < 16; ++j) {
  2694. const uint8_t xi0 = L[j];
  2695. const uint8_t xi1 = L[j+16];
  2696. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2697. // get the 5-th bit and store it in qh at the right position
  2698. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2699. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2700. }
  2701. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2702. }
  2703. }
  2704. size_t quantize_q5_0(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2705. if (!quant_weights) {
  2706. return ggml_quantize_q5_0(src, dst, nrow*n_per_row, n_per_row, hist);
  2707. }
  2708. size_t row_size = ggml_row_size(GGML_TYPE_Q5_0, n_per_row);
  2709. char * qrow = (char *)dst;
  2710. for (int row = 0; row < nrow; ++row) {
  2711. quantize_row_q5_0_impl(src, (block_q5_0*)qrow, n_per_row, quant_weights);
  2712. src += n_per_row;
  2713. qrow += row_size;
  2714. }
  2715. return nrow * row_size;
  2716. }
  2717. static void quantize_row_q5_1_impl(const float * restrict x, block_q5_1 * restrict y, int n_per_row, const float * quant_weights) {
  2718. static_assert(QK5_1 == 32, "QK5_1 must be 32");
  2719. if (!quant_weights) {
  2720. quantize_row_q5_1_reference(x, y, n_per_row);
  2721. return;
  2722. }
  2723. float weight[QK5_1];
  2724. uint8_t L[QK5_1], Laux[QK5_1];
  2725. float sum_x2 = 0;
  2726. for (int j = 0; j < n_per_row; ++j) sum_x2 += x[j]*x[j];
  2727. float sigma2 = sum_x2/n_per_row;
  2728. const int nb = n_per_row/QK5_1;
  2729. for (int ib = 0; ib < nb; ++ib) {
  2730. const float * xb = x + QK5_1 * ib;
  2731. const float * qw = quant_weights + QK5_1 * ib;
  2732. for (int j = 0; j < QK5_1; ++j) weight[j] = qw[j] * sqrtf(sigma2 + xb[j]*xb[j]);
  2733. float min;
  2734. float d = make_qkx3_quants(QK5_1, 31, xb, weight, L, &min, Laux, -0.9f, 0.05f, 36, false);
  2735. y[ib].d = GGML_FP32_TO_FP16(d);
  2736. y[ib].m = GGML_FP32_TO_FP16(-min);
  2737. uint32_t qh = 0;
  2738. for (int j = 0; j < 16; ++j) {
  2739. const uint8_t xi0 = L[j];
  2740. const uint8_t xi1 = L[j+16];
  2741. y[ib].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  2742. // get the 5-th bit and store it in qh at the right position
  2743. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  2744. qh |= ((xi1 & 0x10u) >> 4) << (j + QK5_0/2);
  2745. }
  2746. memcpy(&y[ib].qh, &qh, sizeof(qh));
  2747. }
  2748. }
  2749. size_t quantize_q5_1(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  2750. if (!quant_weights) {
  2751. return ggml_quantize_q5_1(src, dst, nrow*n_per_row, n_per_row, hist);
  2752. }
  2753. size_t row_size = ggml_row_size(GGML_TYPE_Q5_1, n_per_row);
  2754. char * qrow = (char *)dst;
  2755. for (int row = 0; row < nrow; ++row) {
  2756. quantize_row_q5_1_impl(src, (block_q5_1*)qrow, n_per_row, quant_weights);
  2757. src += n_per_row;
  2758. qrow += row_size;
  2759. }
  2760. return nrow * row_size;
  2761. }
  2762. // ====================== "True" 2-bit (de)-quantization
  2763. static const uint64_t iq2xxs_grid[256] = {
  2764. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2765. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x08080808082b0808,
  2766. 0x08080808082b082b, 0x08080808082b2b08, 0x08080808082b2b2b, 0x0808080819080819,
  2767. 0x0808080819081908, 0x0808080819190808, 0x0808080819192b08, 0x08080808192b0819,
  2768. 0x08080808192b1908, 0x080808082b080808, 0x080808082b08082b, 0x080808082b082b2b,
  2769. 0x080808082b2b082b, 0x0808081908080819, 0x0808081908081908, 0x0808081908190808,
  2770. 0x0808081908191919, 0x0808081919080808, 0x080808192b081908, 0x080808192b192b08,
  2771. 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b082b082b, 0x0808082b2b08082b,
  2772. 0x0808190808080819, 0x0808190808081908, 0x0808190808190808, 0x08081908082b0819,
  2773. 0x08081908082b1908, 0x0808190819080808, 0x080819081908082b, 0x0808190819082b08,
  2774. 0x08081908192b0808, 0x080819082b080819, 0x080819082b081908, 0x080819082b190808,
  2775. 0x080819082b2b1908, 0x0808191908080808, 0x080819190808082b, 0x0808191908082b08,
  2776. 0x08081919082b0808, 0x080819191908192b, 0x08081919192b2b19, 0x080819192b080808,
  2777. 0x080819192b190819, 0x0808192b08082b19, 0x0808192b08190808, 0x0808192b19080808,
  2778. 0x0808192b2b081908, 0x0808192b2b2b1908, 0x08082b0808080808, 0x08082b0808081919,
  2779. 0x08082b0808082b08, 0x08082b0808191908, 0x08082b08082b2b08, 0x08082b0819080819,
  2780. 0x08082b0819081908, 0x08082b0819190808, 0x08082b081919082b, 0x08082b082b082b08,
  2781. 0x08082b1908081908, 0x08082b1919080808, 0x08082b2b0808082b, 0x08082b2b08191908,
  2782. 0x0819080808080819, 0x0819080808081908, 0x0819080808190808, 0x08190808082b0819,
  2783. 0x0819080819080808, 0x08190808192b0808, 0x081908082b081908, 0x081908082b190808,
  2784. 0x081908082b191919, 0x0819081908080808, 0x0819081908082b08, 0x08190819082b0808,
  2785. 0x0819081919190808, 0x0819081919192b2b, 0x081908192b080808, 0x0819082b082b1908,
  2786. 0x0819082b19081919, 0x0819190808080808, 0x0819190808082b08, 0x08191908082b0808,
  2787. 0x08191908082b1919, 0x0819190819082b19, 0x081919082b080808, 0x0819191908192b08,
  2788. 0x08191919192b082b, 0x0819192b08080808, 0x0819192b0819192b, 0x08192b0808080819,
  2789. 0x08192b0808081908, 0x08192b0808190808, 0x08192b0819080808, 0x08192b082b080819,
  2790. 0x08192b1908080808, 0x08192b1908081919, 0x08192b192b2b0808, 0x08192b2b19190819,
  2791. 0x082b080808080808, 0x082b08080808082b, 0x082b080808082b2b, 0x082b080819081908,
  2792. 0x082b0808192b0819, 0x082b08082b080808, 0x082b08082b08082b, 0x082b0819082b2b19,
  2793. 0x082b081919082b08, 0x082b082b08080808, 0x082b082b0808082b, 0x082b190808080819,
  2794. 0x082b190808081908, 0x082b190808190808, 0x082b190819080808, 0x082b19081919192b,
  2795. 0x082b191908080808, 0x082b191919080819, 0x082b1919192b1908, 0x082b192b2b190808,
  2796. 0x082b2b0808082b08, 0x082b2b08082b0808, 0x082b2b082b191908, 0x082b2b2b19081908,
  2797. 0x1908080808080819, 0x1908080808081908, 0x1908080808190808, 0x1908080808192b08,
  2798. 0x19080808082b0819, 0x19080808082b1908, 0x1908080819080808, 0x1908080819082b08,
  2799. 0x190808081919192b, 0x19080808192b0808, 0x190808082b080819, 0x190808082b081908,
  2800. 0x190808082b190808, 0x1908081908080808, 0x19080819082b0808, 0x19080819192b0819,
  2801. 0x190808192b080808, 0x190808192b081919, 0x1908082b08080819, 0x1908082b08190808,
  2802. 0x1908082b19082b08, 0x1908082b1919192b, 0x1908082b192b2b08, 0x1908190808080808,
  2803. 0x1908190808082b08, 0x19081908082b0808, 0x190819082b080808, 0x190819082b192b19,
  2804. 0x190819190819082b, 0x19081919082b1908, 0x1908192b08080808, 0x19082b0808080819,
  2805. 0x19082b0808081908, 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919,
  2806. 0x19082b1908080808, 0x19082b1919192b08, 0x19082b19192b0819, 0x19082b192b08082b,
  2807. 0x19082b2b19081919, 0x19082b2b2b190808, 0x1919080808080808, 0x1919080808082b08,
  2808. 0x1919080808190819, 0x1919080808192b19, 0x19190808082b0808, 0x191908082b080808,
  2809. 0x191908082b082b08, 0x1919081908081908, 0x191908191908082b, 0x191908192b2b1908,
  2810. 0x1919082b2b190819, 0x191919082b190808, 0x191919082b19082b, 0x1919191908082b2b,
  2811. 0x1919192b08080819, 0x1919192b19191908, 0x19192b0808080808, 0x19192b0808190819,
  2812. 0x19192b0808192b19, 0x19192b08192b1908, 0x19192b1919080808, 0x19192b2b08082b08,
  2813. 0x192b080808081908, 0x192b080808190808, 0x192b080819080808, 0x192b0808192b2b08,
  2814. 0x192b081908080808, 0x192b081919191919, 0x192b082b08192b08, 0x192b082b192b0808,
  2815. 0x192b190808080808, 0x192b190808081919, 0x192b191908190808, 0x192b19190819082b,
  2816. 0x192b19192b081908, 0x192b2b081908082b, 0x2b08080808080808, 0x2b0808080808082b,
  2817. 0x2b08080808082b2b, 0x2b08080819080819, 0x2b0808082b08082b, 0x2b08081908081908,
  2818. 0x2b08081908192b08, 0x2b08081919080808, 0x2b08082b08190819, 0x2b08190808080819,
  2819. 0x2b08190808081908, 0x2b08190808190808, 0x2b08190808191919, 0x2b08190819080808,
  2820. 0x2b081908192b0808, 0x2b08191908080808, 0x2b0819191908192b, 0x2b0819192b191908,
  2821. 0x2b08192b08082b19, 0x2b08192b19080808, 0x2b08192b192b0808, 0x2b082b080808082b,
  2822. 0x2b082b1908081908, 0x2b082b2b08190819, 0x2b19080808081908, 0x2b19080808190808,
  2823. 0x2b190808082b1908, 0x2b19080819080808, 0x2b1908082b2b0819, 0x2b1908190819192b,
  2824. 0x2b1908192b080808, 0x2b19082b19081919, 0x2b19190808080808, 0x2b191908082b082b,
  2825. 0x2b19190819081908, 0x2b19191919190819, 0x2b192b082b080819, 0x2b192b19082b0808,
  2826. 0x2b2b08080808082b, 0x2b2b080819190808, 0x2b2b08082b081919, 0x2b2b081908082b19,
  2827. 0x2b2b082b08080808, 0x2b2b190808192b08, 0x2b2b2b0819190808, 0x2b2b2b1908081908,
  2828. };
  2829. static const uint64_t iq2xs_grid[512] = {
  2830. 0x0808080808080808, 0x080808080808082b, 0x0808080808081919, 0x0808080808082b08,
  2831. 0x0808080808082b2b, 0x0808080808190819, 0x0808080808191908, 0x080808080819192b,
  2832. 0x0808080808192b19, 0x08080808082b0808, 0x08080808082b082b, 0x08080808082b1919,
  2833. 0x08080808082b2b08, 0x0808080819080819, 0x0808080819081908, 0x080808081908192b,
  2834. 0x0808080819082b19, 0x0808080819190808, 0x080808081919082b, 0x0808080819191919,
  2835. 0x0808080819192b08, 0x08080808192b0819, 0x08080808192b1908, 0x080808082b080808,
  2836. 0x080808082b08082b, 0x080808082b081919, 0x080808082b082b08, 0x080808082b190819,
  2837. 0x080808082b191908, 0x080808082b192b19, 0x080808082b2b0808, 0x0808081908080819,
  2838. 0x0808081908081908, 0x080808190808192b, 0x0808081908082b19, 0x0808081908190808,
  2839. 0x080808190819082b, 0x0808081908191919, 0x0808081908192b08, 0x0808081908192b2b,
  2840. 0x08080819082b0819, 0x08080819082b1908, 0x0808081919080808, 0x080808191908082b,
  2841. 0x0808081919081919, 0x0808081919082b08, 0x0808081919190819, 0x0808081919191908,
  2842. 0x08080819192b0808, 0x08080819192b2b08, 0x080808192b080819, 0x080808192b081908,
  2843. 0x080808192b190808, 0x0808082b08080808, 0x0808082b0808082b, 0x0808082b08081919,
  2844. 0x0808082b08082b08, 0x0808082b08190819, 0x0808082b08191908, 0x0808082b082b0808,
  2845. 0x0808082b19080819, 0x0808082b19081908, 0x0808082b19190808, 0x0808082b19191919,
  2846. 0x0808082b2b080808, 0x0808082b2b082b2b, 0x0808190808080819, 0x0808190808081908,
  2847. 0x080819080808192b, 0x0808190808082b19, 0x0808190808190808, 0x080819080819082b,
  2848. 0x0808190808191919, 0x0808190808192b08, 0x08081908082b0819, 0x08081908082b1908,
  2849. 0x0808190819080808, 0x080819081908082b, 0x0808190819081919, 0x0808190819082b08,
  2850. 0x0808190819190819, 0x0808190819191908, 0x080819081919192b, 0x08081908192b0808,
  2851. 0x080819082b080819, 0x080819082b081908, 0x080819082b190808, 0x0808191908080808,
  2852. 0x080819190808082b, 0x0808191908081919, 0x0808191908082b08, 0x0808191908190819,
  2853. 0x0808191908191908, 0x08081919082b0808, 0x0808191919080819, 0x0808191919081908,
  2854. 0x0808191919190808, 0x08081919192b0819, 0x080819192b080808, 0x0808192b08080819,
  2855. 0x0808192b08081908, 0x0808192b08190808, 0x0808192b082b192b, 0x0808192b19080808,
  2856. 0x0808192b1908082b, 0x0808192b2b081908, 0x08082b0808080808, 0x08082b080808082b,
  2857. 0x08082b0808081919, 0x08082b0808082b08, 0x08082b0808082b2b, 0x08082b0808190819,
  2858. 0x08082b0808191908, 0x08082b08082b0808, 0x08082b08082b1919, 0x08082b0819080819,
  2859. 0x08082b0819081908, 0x08082b0819190808, 0x08082b0819192b08, 0x08082b082b080808,
  2860. 0x08082b082b2b0808, 0x08082b082b2b2b2b, 0x08082b1908080819, 0x08082b1908081908,
  2861. 0x08082b1908190808, 0x08082b1919080808, 0x08082b192b080819, 0x08082b192b082b19,
  2862. 0x08082b2b08080808, 0x08082b2b082b0808, 0x08082b2b082b2b08, 0x08082b2b2b19192b,
  2863. 0x08082b2b2b2b0808, 0x0819080808080819, 0x0819080808081908, 0x081908080808192b,
  2864. 0x0819080808082b19, 0x0819080808190808, 0x081908080819082b, 0x0819080808191919,
  2865. 0x0819080808192b08, 0x08190808082b0819, 0x08190808082b1908, 0x0819080819080808,
  2866. 0x081908081908082b, 0x0819080819081919, 0x0819080819082b08, 0x0819080819190819,
  2867. 0x0819080819191908, 0x08190808192b0808, 0x08190808192b2b2b, 0x081908082b080819,
  2868. 0x081908082b081908, 0x081908082b190808, 0x0819081908080808, 0x081908190808082b,
  2869. 0x0819081908081919, 0x0819081908082b08, 0x0819081908190819, 0x0819081908191908,
  2870. 0x08190819082b0808, 0x0819081919080819, 0x0819081919081908, 0x0819081919190808,
  2871. 0x081908192b080808, 0x081908192b191908, 0x081908192b19192b, 0x0819082b08080819,
  2872. 0x0819082b08081908, 0x0819082b0808192b, 0x0819082b08190808, 0x0819082b19080808,
  2873. 0x0819082b192b0808, 0x0819190808080808, 0x081919080808082b, 0x0819190808081919,
  2874. 0x0819190808082b08, 0x0819190808190819, 0x0819190808191908, 0x08191908082b0808,
  2875. 0x0819190819080819, 0x0819190819081908, 0x0819190819082b19, 0x0819190819190808,
  2876. 0x08191908192b1908, 0x081919082b080808, 0x0819191908080819, 0x0819191908081908,
  2877. 0x0819191908190808, 0x0819191919080808, 0x0819192b08080808, 0x0819192b08191908,
  2878. 0x0819192b19082b19, 0x08192b0808080819, 0x08192b0808081908, 0x08192b0808190808,
  2879. 0x08192b080819082b, 0x08192b0819080808, 0x08192b0819191908, 0x08192b082b08192b,
  2880. 0x08192b1908080808, 0x08192b1908081919, 0x08192b19192b192b, 0x08192b2b19190819,
  2881. 0x08192b2b2b2b2b19, 0x082b080808080808, 0x082b08080808082b, 0x082b080808081919,
  2882. 0x082b080808082b08, 0x082b080808082b2b, 0x082b080808190819, 0x082b080808191908,
  2883. 0x082b0808082b0808, 0x082b080819080819, 0x082b080819081908, 0x082b080819190808,
  2884. 0x082b08082b080808, 0x082b08082b2b0808, 0x082b081908080819, 0x082b081908081908,
  2885. 0x082b081908190808, 0x082b081919080808, 0x082b081919082b08, 0x082b0819192b1919,
  2886. 0x082b082b08080808, 0x082b082b082b082b, 0x082b082b2b080808, 0x082b082b2b2b2b08,
  2887. 0x082b190808080819, 0x082b190808081908, 0x082b190808190808, 0x082b1908082b2b19,
  2888. 0x082b190819080808, 0x082b191908080808, 0x082b191919080819, 0x082b19191919082b,
  2889. 0x082b19192b192b19, 0x082b192b08080819, 0x082b192b08192b2b, 0x082b192b2b2b192b,
  2890. 0x082b2b0808080808, 0x082b2b0808082b08, 0x082b2b0808082b2b, 0x082b2b08082b0808,
  2891. 0x082b2b0819191919, 0x082b2b082b082b08, 0x082b2b082b2b082b, 0x082b2b19192b2b08,
  2892. 0x082b2b192b190808, 0x082b2b2b08082b08, 0x082b2b2b082b0808, 0x082b2b2b2b08082b,
  2893. 0x082b2b2b2b082b08, 0x082b2b2b2b082b2b, 0x1908080808080819, 0x1908080808081908,
  2894. 0x190808080808192b, 0x1908080808082b19, 0x1908080808190808, 0x190808080819082b,
  2895. 0x1908080808191919, 0x1908080808192b08, 0x19080808082b0819, 0x19080808082b1908,
  2896. 0x1908080819080808, 0x190808081908082b, 0x1908080819081919, 0x1908080819082b08,
  2897. 0x1908080819082b2b, 0x1908080819190819, 0x1908080819191908, 0x19080808192b0808,
  2898. 0x19080808192b1919, 0x190808082b080819, 0x190808082b081908, 0x190808082b190808,
  2899. 0x1908081908080808, 0x190808190808082b, 0x1908081908081919, 0x1908081908082b08,
  2900. 0x1908081908190819, 0x1908081908191908, 0x19080819082b0808, 0x1908081919080819,
  2901. 0x1908081919081908, 0x1908081919190808, 0x190808192b080808, 0x190808192b081919,
  2902. 0x190808192b2b082b, 0x1908082b08080819, 0x1908082b08081908, 0x1908082b08190808,
  2903. 0x1908082b0819082b, 0x1908082b082b2b19, 0x1908082b19080808, 0x1908190808080808,
  2904. 0x190819080808082b, 0x1908190808081919, 0x1908190808082b08, 0x1908190808190819,
  2905. 0x1908190808191908, 0x1908190808192b19, 0x19081908082b0808, 0x1908190819080819,
  2906. 0x1908190819081908, 0x1908190819190808, 0x190819082b080808, 0x190819082b191908,
  2907. 0x1908191908080819, 0x1908191908081908, 0x1908191908190808, 0x19081919082b1908,
  2908. 0x1908191919080808, 0x190819192b192b2b, 0x1908192b08080808, 0x1908192b08082b2b,
  2909. 0x1908192b19081908, 0x1908192b19190808, 0x19082b0808080819, 0x19082b0808081908,
  2910. 0x19082b0808190808, 0x19082b0819080808, 0x19082b0819081919, 0x19082b0819191908,
  2911. 0x19082b08192b082b, 0x19082b1908080808, 0x19082b1908190819, 0x19082b1919081908,
  2912. 0x19082b1919190808, 0x19082b19192b2b19, 0x19082b2b08081908, 0x1919080808080808,
  2913. 0x191908080808082b, 0x1919080808081919, 0x1919080808082b08, 0x1919080808190819,
  2914. 0x1919080808191908, 0x19190808082b0808, 0x19190808082b2b08, 0x1919080819080819,
  2915. 0x1919080819081908, 0x1919080819190808, 0x191908082b080808, 0x1919081908080819,
  2916. 0x1919081908081908, 0x1919081908190808, 0x1919081908191919, 0x1919081919080808,
  2917. 0x191908191908082b, 0x1919082b08080808, 0x1919082b19081908, 0x1919082b2b2b2b2b,
  2918. 0x1919190808080819, 0x1919190808081908, 0x1919190808190808, 0x19191908082b0819,
  2919. 0x1919190819080808, 0x19191908192b0808, 0x191919082b080819, 0x191919082b2b0819,
  2920. 0x1919191908080808, 0x1919191908082b08, 0x191919192b080808, 0x191919192b082b08,
  2921. 0x1919192b082b0819, 0x1919192b192b2b08, 0x1919192b2b2b0819, 0x19192b0808080808,
  2922. 0x19192b0808191908, 0x19192b0819080819, 0x19192b0819190808, 0x19192b082b192b19,
  2923. 0x19192b1908192b2b, 0x19192b1919080808, 0x19192b191908082b, 0x19192b2b2b081919,
  2924. 0x192b080808080819, 0x192b080808081908, 0x192b080808190808, 0x192b080819080808,
  2925. 0x192b080819191908, 0x192b0808192b082b, 0x192b08082b08192b, 0x192b08082b2b2b19,
  2926. 0x192b081908080808, 0x192b082b082b1908, 0x192b082b19082b2b, 0x192b082b2b19082b,
  2927. 0x192b190808080808, 0x192b19080819192b, 0x192b191908190808, 0x192b191919080808,
  2928. 0x192b191919081919, 0x192b19192b2b1908, 0x192b2b0808080819, 0x192b2b08192b2b2b,
  2929. 0x192b2b19082b1919, 0x192b2b2b0808192b, 0x192b2b2b19191908, 0x192b2b2b192b082b,
  2930. 0x2b08080808080808, 0x2b0808080808082b, 0x2b08080808081919, 0x2b08080808082b08,
  2931. 0x2b08080808190819, 0x2b08080808191908, 0x2b080808082b0808, 0x2b080808082b2b2b,
  2932. 0x2b08080819080819, 0x2b08080819081908, 0x2b08080819190808, 0x2b0808082b080808,
  2933. 0x2b0808082b08082b, 0x2b0808082b2b2b08, 0x2b0808082b2b2b2b, 0x2b08081908080819,
  2934. 0x2b08081908081908, 0x2b0808190808192b, 0x2b08081908190808, 0x2b08081919080808,
  2935. 0x2b08081919190819, 0x2b08081919192b19, 0x2b08082b08080808, 0x2b08082b082b0808,
  2936. 0x2b08082b2b080808, 0x2b08082b2b08082b, 0x2b08082b2b2b0808, 0x2b08082b2b2b2b08,
  2937. 0x2b08190808080819, 0x2b08190808081908, 0x2b08190808190808, 0x2b0819080819082b,
  2938. 0x2b08190808191919, 0x2b08190819080808, 0x2b081908192b0808, 0x2b0819082b082b19,
  2939. 0x2b08191908080808, 0x2b08191919081908, 0x2b0819192b2b1919, 0x2b08192b08192b08,
  2940. 0x2b08192b192b2b2b, 0x2b082b0808080808, 0x2b082b0808082b08, 0x2b082b08082b1919,
  2941. 0x2b082b0819192b2b, 0x2b082b082b080808, 0x2b082b082b08082b, 0x2b082b082b2b2b08,
  2942. 0x2b082b190808192b, 0x2b082b2b082b082b, 0x2b082b2b2b080808, 0x2b082b2b2b082b08,
  2943. 0x2b082b2b2b19192b, 0x2b082b2b2b2b2b08, 0x2b19080808080819, 0x2b19080808081908,
  2944. 0x2b19080808190808, 0x2b19080819080808, 0x2b1908081919192b, 0x2b1908082b081908,
  2945. 0x2b19081908080808, 0x2b190819082b082b, 0x2b190819192b1908, 0x2b19082b1919192b,
  2946. 0x2b19082b2b082b19, 0x2b19190808080808, 0x2b19190808081919, 0x2b19190819081908,
  2947. 0x2b19190819190808, 0x2b19190819192b08, 0x2b191919082b2b19, 0x2b1919192b190808,
  2948. 0x2b1919192b19082b, 0x2b19192b19080819, 0x2b192b0819190819, 0x2b192b082b2b192b,
  2949. 0x2b192b1919082b19, 0x2b192b2b08191919, 0x2b192b2b192b0808, 0x2b2b080808080808,
  2950. 0x2b2b08080808082b, 0x2b2b080808082b08, 0x2b2b080808082b2b, 0x2b2b0808082b0808,
  2951. 0x2b2b0808082b2b2b, 0x2b2b08082b2b0808, 0x2b2b081919190819, 0x2b2b081919192b19,
  2952. 0x2b2b08192b2b192b, 0x2b2b082b08080808, 0x2b2b082b0808082b, 0x2b2b082b08082b08,
  2953. 0x2b2b082b082b2b2b, 0x2b2b082b2b080808, 0x2b2b082b2b2b0808, 0x2b2b190819080808,
  2954. 0x2b2b19082b191919, 0x2b2b192b192b1919, 0x2b2b192b2b192b08, 0x2b2b2b0808082b2b,
  2955. 0x2b2b2b08082b0808, 0x2b2b2b08082b082b, 0x2b2b2b08082b2b08, 0x2b2b2b082b2b0808,
  2956. 0x2b2b2b082b2b2b08, 0x2b2b2b1908081908, 0x2b2b2b192b081908, 0x2b2b2b192b08192b,
  2957. 0x2b2b2b2b082b2b08, 0x2b2b2b2b082b2b2b, 0x2b2b2b2b2b190819, 0x2b2b2b2b2b2b2b2b,
  2958. };
  2959. static const uint8_t ksigns_iq2xs[128] = {
  2960. 0, 129, 130, 3, 132, 5, 6, 135, 136, 9, 10, 139, 12, 141, 142, 15,
  2961. 144, 17, 18, 147, 20, 149, 150, 23, 24, 153, 154, 27, 156, 29, 30, 159,
  2962. 160, 33, 34, 163, 36, 165, 166, 39, 40, 169, 170, 43, 172, 45, 46, 175,
  2963. 48, 177, 178, 51, 180, 53, 54, 183, 184, 57, 58, 187, 60, 189, 190, 63,
  2964. 192, 65, 66, 195, 68, 197, 198, 71, 72, 201, 202, 75, 204, 77, 78, 207,
  2965. 80, 209, 210, 83, 212, 85, 86, 215, 216, 89, 90, 219, 92, 221, 222, 95,
  2966. 96, 225, 226, 99, 228, 101, 102, 231, 232, 105, 106, 235, 108, 237, 238, 111,
  2967. 240, 113, 114, 243, 116, 245, 246, 119, 120, 249, 250, 123, 252, 125, 126, 255,
  2968. };
  2969. static const uint8_t kmask_iq2xs[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  2970. void dequantize_row_iq2_xxs(const block_iq2_xxs * restrict x, float * restrict y, int k) {
  2971. assert(k % QK_K == 0);
  2972. const int nb = k / QK_K;
  2973. uint32_t aux32[2];
  2974. const uint8_t * aux8 = (const uint8_t *)aux32;
  2975. for (int i = 0; i < nb; i++) {
  2976. const float d = GGML_FP16_TO_FP32(x[i].d);
  2977. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2978. memcpy(aux32, x[i].qs + 4*ib32, 2*sizeof(uint32_t));
  2979. const float db = d * (0.5f + (aux32[1] >> 28)) * 0.25f;
  2980. for (int l = 0; l < 4; ++l) {
  2981. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  2982. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  2983. for (int j = 0; j < 8; ++j) {
  2984. y[j] = db * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  2985. }
  2986. y += 8;
  2987. }
  2988. }
  2989. }
  2990. }
  2991. // ====================== 2.3125 bpw (de)-quantization
  2992. void dequantize_row_iq2_xs(const block_iq2_xs * restrict x, float * restrict y, int k) {
  2993. assert(k % QK_K == 0);
  2994. const int nb = k / QK_K;
  2995. float db[2];
  2996. for (int i = 0; i < nb; i++) {
  2997. const float d = GGML_FP16_TO_FP32(x[i].d);
  2998. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  2999. db[0] = d * (0.5f + (x[i].scales[ib32] & 0xf)) * 0.25f;
  3000. db[1] = d * (0.5f + (x[i].scales[ib32] >> 4)) * 0.25f;
  3001. for (int l = 0; l < 4; ++l) {
  3002. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (x[i].qs[4*ib32 + l] & 511));
  3003. const uint8_t signs = ksigns_iq2xs[x[i].qs[4*ib32 + l] >> 9];
  3004. for (int j = 0; j < 8; ++j) {
  3005. y[j] = db[l/2] * grid[j] * (signs & kmask_iq2xs[j] ? -1.f : 1.f);
  3006. }
  3007. y += 8;
  3008. }
  3009. }
  3010. }
  3011. }
  3012. //===================================== Q8_K ==============================================
  3013. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  3014. assert(k % QK_K == 0);
  3015. const int nb = k / QK_K;
  3016. for (int i = 0; i < nb; i++) {
  3017. float max = 0;
  3018. float amax = 0;
  3019. for (int j = 0; j < QK_K; ++j) {
  3020. float ax = fabsf(x[j]);
  3021. if (ax > amax) {
  3022. amax = ax; max = x[j];
  3023. }
  3024. }
  3025. if (!amax) {
  3026. y[i].d = 0;
  3027. memset(y[i].qs, 0, QK_K);
  3028. x += QK_K;
  3029. continue;
  3030. }
  3031. //const float iscale = -128.f/max;
  3032. // We need this change for IQ2_XXS, else the AVX implementation becomes very awkward
  3033. const float iscale = -127.f/max;
  3034. for (int j = 0; j < QK_K; ++j) {
  3035. int v = nearest_int(iscale*x[j]);
  3036. y[i].qs[j] = MIN(127, v);
  3037. }
  3038. for (int j = 0; j < QK_K/16; ++j) {
  3039. int sum = 0;
  3040. for (int ii = 0; ii < 16; ++ii) {
  3041. sum += y[i].qs[j*16 + ii];
  3042. }
  3043. y[i].bsums[j] = sum;
  3044. }
  3045. y[i].d = 1/iscale;
  3046. x += QK_K;
  3047. }
  3048. }
  3049. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  3050. assert(k % QK_K == 0);
  3051. const int nb = k / QK_K;
  3052. for (int i = 0; i < nb; i++) {
  3053. for (int j = 0; j < QK_K; ++j) {
  3054. *y++ = x[i].d * x[i].qs[j];
  3055. }
  3056. }
  3057. }
  3058. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  3059. quantize_row_q8_K_reference(x, y, k);
  3060. }
  3061. //===================================== Dot ptoducts =================================
  3062. //
  3063. // Helper functions
  3064. //
  3065. #if __AVX__ || __AVX2__ || __AVX512F__
  3066. // shuffles to pick the required scales in dot products
  3067. static inline __m256i get_scale_shuffle_q3k(int i) {
  3068. static const uint8_t k_shuffle[128] = {
  3069. 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,
  3070. 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,
  3071. 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,
  3072. 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,
  3073. };
  3074. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3075. }
  3076. static inline __m256i get_scale_shuffle_k4(int i) {
  3077. static const uint8_t k_shuffle[256] = {
  3078. 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,
  3079. 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,
  3080. 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,
  3081. 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,
  3082. 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,
  3083. 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,
  3084. 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,
  3085. 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
  3086. };
  3087. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  3088. }
  3089. static inline __m128i get_scale_shuffle(int i) {
  3090. static const uint8_t k_shuffle[128] = {
  3091. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  3092. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  3093. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3094. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  3095. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  3096. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  3097. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  3098. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  3099. };
  3100. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  3101. }
  3102. #endif
  3103. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3104. const int qk = QK8_0;
  3105. const int nb = n / qk;
  3106. assert(n % qk == 0);
  3107. const block_q4_0 * restrict x = vx;
  3108. const block_q8_0 * restrict y = vy;
  3109. #if defined(__ARM_NEON)
  3110. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3111. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3112. assert(nb % 2 == 0); // TODO: handle odd nb
  3113. for (int i = 0; i < nb; i += 2) {
  3114. const block_q4_0 * restrict x0 = &x[i + 0];
  3115. const block_q4_0 * restrict x1 = &x[i + 1];
  3116. const block_q8_0 * restrict y0 = &y[i + 0];
  3117. const block_q8_0 * restrict y1 = &y[i + 1];
  3118. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3119. const int8x16_t s8b = vdupq_n_s8(0x8);
  3120. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3121. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3122. // 4-bit -> 8-bit
  3123. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3124. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3125. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3126. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3127. // sub 8
  3128. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  3129. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  3130. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  3131. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  3132. // load y
  3133. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3134. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3135. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3136. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3137. // dot product into int32x4_t
  3138. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  3139. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  3140. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3141. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3142. }
  3143. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3144. #elif defined(__AVX2__)
  3145. // Initialize accumulator with zeros
  3146. __m256 acc = _mm256_setzero_ps();
  3147. // Main loop
  3148. for (int i = 0; i < nb; ++i) {
  3149. /* Compute combined scale for the block */
  3150. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3151. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3152. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  3153. const __m256i off = _mm256_set1_epi8( 8 );
  3154. bx = _mm256_sub_epi8( bx, off );
  3155. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3156. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3157. /* Multiply q with scale and accumulate */
  3158. acc = _mm256_fmadd_ps( d, q, acc );
  3159. }
  3160. *s = hsum_float_8(acc);
  3161. #elif defined(__AVX__)
  3162. // Initialize accumulator with zeros
  3163. __m256 acc = _mm256_setzero_ps();
  3164. // Main loop
  3165. for (int i = 0; i < nb; ++i) {
  3166. // Compute combined scale for the block
  3167. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3168. const __m128i lowMask = _mm_set1_epi8(0xF);
  3169. const __m128i off = _mm_set1_epi8(8);
  3170. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  3171. __m128i bx = _mm_and_si128(lowMask, tmp);
  3172. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  3173. bx = _mm_sub_epi8(bx, off);
  3174. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  3175. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  3176. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3177. bx = _mm_sub_epi8(bx, off);
  3178. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  3179. // Convert int32_t to float
  3180. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  3181. // Apply the scale, and accumulate
  3182. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  3183. }
  3184. *s = hsum_float_8(acc);
  3185. #elif defined(__SSSE3__)
  3186. // set constants
  3187. const __m128i lowMask = _mm_set1_epi8(0xF);
  3188. const __m128i off = _mm_set1_epi8(8);
  3189. // Initialize accumulator with zeros
  3190. __m128 acc_0 = _mm_setzero_ps();
  3191. __m128 acc_1 = _mm_setzero_ps();
  3192. __m128 acc_2 = _mm_setzero_ps();
  3193. __m128 acc_3 = _mm_setzero_ps();
  3194. // First round without accumulation
  3195. {
  3196. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  3197. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  3198. // Compute combined scale for the block 0 and 1
  3199. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  3200. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  3201. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3202. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  3203. bx_0 = _mm_sub_epi8(bx_0, off);
  3204. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3205. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3206. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  3207. bx_1 = _mm_sub_epi8(bx_1, off);
  3208. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3209. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  3210. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  3211. // Compute combined scale for the block 2 and 3
  3212. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  3213. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  3214. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3215. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  3216. bx_2 = _mm_sub_epi8(bx_2, off);
  3217. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3218. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3219. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  3220. bx_3 = _mm_sub_epi8(bx_3, off);
  3221. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3222. // Convert int32_t to float
  3223. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3224. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3225. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3226. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3227. // Apply the scale
  3228. acc_0 = _mm_mul_ps( d_0_1, p0 );
  3229. acc_1 = _mm_mul_ps( d_0_1, p1 );
  3230. acc_2 = _mm_mul_ps( d_2_3, p2 );
  3231. acc_3 = _mm_mul_ps( d_2_3, p3 );
  3232. }
  3233. assert(nb % 2 == 0); // TODO: handle odd nb
  3234. // Main loop
  3235. for (int i = 2; i < nb; i+=2) {
  3236. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  3237. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  3238. // Compute combined scale for the block 0 and 1
  3239. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  3240. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  3241. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  3242. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  3243. bx_0 = _mm_sub_epi8(bx_0, off);
  3244. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  3245. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  3246. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  3247. bx_1 = _mm_sub_epi8(bx_1, off);
  3248. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  3249. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  3250. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  3251. // Compute combined scale for the block 2 and 3
  3252. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  3253. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  3254. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  3255. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  3256. bx_2 = _mm_sub_epi8(bx_2, off);
  3257. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  3258. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  3259. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  3260. bx_3 = _mm_sub_epi8(bx_3, off);
  3261. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  3262. // Convert int32_t to float
  3263. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  3264. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  3265. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  3266. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  3267. // Apply the scale
  3268. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  3269. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  3270. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  3271. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  3272. // Acummulate
  3273. acc_0 = _mm_add_ps(p0_d, acc_0);
  3274. acc_1 = _mm_add_ps(p1_d, acc_1);
  3275. acc_2 = _mm_add_ps(p2_d, acc_2);
  3276. acc_3 = _mm_add_ps(p3_d, acc_3);
  3277. }
  3278. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  3279. #elif defined(__riscv_v_intrinsic)
  3280. float sumf = 0.0;
  3281. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3282. for (int i = 0; i < nb; i++) {
  3283. // load elements
  3284. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3285. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3286. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3287. // mask and store lower part of x, and then upper part
  3288. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3289. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3290. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3291. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3292. // subtract offset
  3293. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  3294. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  3295. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3296. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3297. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3298. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3299. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3300. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3301. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3302. }
  3303. *s = sumf;
  3304. #else
  3305. // scalar
  3306. float sumf = 0.0;
  3307. for (int i = 0; i < nb; i++) {
  3308. int sumi = 0;
  3309. for (int j = 0; j < qk/2; ++j) {
  3310. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  3311. const int v1 = (x[i].qs[j] >> 4) - 8;
  3312. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3313. }
  3314. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  3315. }
  3316. *s = sumf;
  3317. #endif
  3318. }
  3319. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3320. const int qk = QK8_1;
  3321. const int nb = n / qk;
  3322. assert(n % qk == 0);
  3323. const block_q4_1 * restrict x = vx;
  3324. const block_q8_1 * restrict y = vy;
  3325. // TODO: add WASM SIMD
  3326. #if defined(__ARM_NEON)
  3327. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3328. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3329. float summs = 0;
  3330. assert(nb % 2 == 0); // TODO: handle odd nb
  3331. for (int i = 0; i < nb; i += 2) {
  3332. const block_q4_1 * restrict x0 = &x[i + 0];
  3333. const block_q4_1 * restrict x1 = &x[i + 1];
  3334. const block_q8_1 * restrict y0 = &y[i + 0];
  3335. const block_q8_1 * restrict y1 = &y[i + 1];
  3336. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  3337. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3338. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3339. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3340. // 4-bit -> 8-bit
  3341. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3342. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3343. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3344. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3345. // load y
  3346. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3347. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3348. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3349. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3350. // dot product into int32x4_t
  3351. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  3352. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  3353. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3354. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3355. }
  3356. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  3357. #elif defined(__AVX2__) || defined(__AVX__)
  3358. // Initialize accumulator with zeros
  3359. __m256 acc = _mm256_setzero_ps();
  3360. float summs = 0;
  3361. // Main loop
  3362. for (int i = 0; i < nb; ++i) {
  3363. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  3364. const float d1 = y[i].d;
  3365. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3366. const __m256 d0v = _mm256_set1_ps( d0 );
  3367. const __m256 d1v = _mm256_set1_ps( d1 );
  3368. // Compute combined scales
  3369. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  3370. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  3371. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3372. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  3373. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  3374. // Accumulate d0*d1*x*y
  3375. #if defined(__AVX2__)
  3376. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  3377. #else
  3378. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  3379. #endif
  3380. }
  3381. *s = hsum_float_8(acc) + summs;
  3382. #elif defined(__riscv_v_intrinsic)
  3383. float sumf = 0.0;
  3384. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3385. for (int i = 0; i < nb; i++) {
  3386. // load elements
  3387. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3388. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3389. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3390. // mask and store lower part of x, and then upper part
  3391. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3392. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3393. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3394. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3395. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3396. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3397. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3398. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3399. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3400. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3401. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3402. }
  3403. *s = sumf;
  3404. #else
  3405. // scalar
  3406. float sumf = 0.0;
  3407. for (int i = 0; i < nb; i++) {
  3408. int sumi = 0;
  3409. for (int j = 0; j < qk/2; ++j) {
  3410. const int v0 = (x[i].qs[j] & 0x0F);
  3411. const int v1 = (x[i].qs[j] >> 4);
  3412. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  3413. }
  3414. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3415. }
  3416. *s = sumf;
  3417. #endif
  3418. }
  3419. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3420. const int qk = QK8_0;
  3421. const int nb = n / qk;
  3422. assert(n % qk == 0);
  3423. assert(qk == QK5_0);
  3424. const block_q5_0 * restrict x = vx;
  3425. const block_q8_0 * restrict y = vy;
  3426. #if defined(__ARM_NEON)
  3427. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3428. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3429. uint32_t qh0;
  3430. uint32_t qh1;
  3431. uint64_t tmp0[4];
  3432. uint64_t tmp1[4];
  3433. assert(nb % 2 == 0); // TODO: handle odd nb
  3434. for (int i = 0; i < nb; i += 2) {
  3435. const block_q5_0 * restrict x0 = &x[i];
  3436. const block_q5_0 * restrict x1 = &x[i + 1];
  3437. const block_q8_0 * restrict y0 = &y[i];
  3438. const block_q8_0 * restrict y1 = &y[i + 1];
  3439. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3440. // extract the 5th bit via lookup table ((!b) << 4)
  3441. memcpy(&qh0, x0->qh, sizeof(qh0));
  3442. memcpy(&qh1, x1->qh, sizeof(qh1));
  3443. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  3444. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  3445. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  3446. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  3447. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  3448. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  3449. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  3450. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  3451. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3452. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3453. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3454. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3455. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3456. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3457. // 4-bit -> 8-bit
  3458. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3459. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3460. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3461. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3462. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3463. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  3464. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  3465. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  3466. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  3467. // load y
  3468. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3469. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3470. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3471. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3472. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3473. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3474. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3475. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3476. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3477. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3478. }
  3479. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3480. #elif defined(__wasm_simd128__)
  3481. v128_t sumv = wasm_f32x4_splat(0.0f);
  3482. uint32_t qh;
  3483. uint64_t tmp[4];
  3484. // TODO: check if unrolling this is better
  3485. for (int i = 0; i < nb; ++i) {
  3486. const block_q5_0 * restrict x0 = &x[i];
  3487. const block_q8_0 * restrict y0 = &y[i];
  3488. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3489. // extract the 5th bit
  3490. memcpy(&qh, x0->qh, sizeof(qh));
  3491. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  3492. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  3493. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  3494. tmp[3] = table_b2b_1[(qh >> 24) ];
  3495. const v128_t qhl = wasm_v128_load(tmp + 0);
  3496. const v128_t qhh = wasm_v128_load(tmp + 2);
  3497. const v128_t v0 = wasm_v128_load(x0->qs);
  3498. // 4-bit -> 8-bit
  3499. const v128_t v0l = wasm_v128_and (v0, m4b);
  3500. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3501. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  3502. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  3503. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  3504. // load y
  3505. const v128_t v1l = wasm_v128_load(y0->qs);
  3506. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3507. // int8x16 -> int16x8
  3508. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3509. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3510. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3511. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3512. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3513. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3514. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3515. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3516. // dot product
  3517. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  3518. wasm_i32x4_add(
  3519. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3520. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3521. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3522. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3523. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  3524. }
  3525. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3526. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  3527. #elif defined(__AVX2__)
  3528. // Initialize accumulator with zeros
  3529. __m256 acc = _mm256_setzero_ps();
  3530. // Main loop
  3531. for (int i = 0; i < nb; i++) {
  3532. /* Compute combined scale for the block */
  3533. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3534. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3535. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3536. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  3537. bx = _mm256_or_si256(bx, bxhi);
  3538. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3539. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3540. /* Multiply q with scale and accumulate */
  3541. acc = _mm256_fmadd_ps(d, q, acc);
  3542. }
  3543. *s = hsum_float_8(acc);
  3544. #elif defined(__AVX__)
  3545. // Initialize accumulator with zeros
  3546. __m256 acc = _mm256_setzero_ps();
  3547. __m128i mask = _mm_set1_epi8((char)0xF0);
  3548. // Main loop
  3549. for (int i = 0; i < nb; i++) {
  3550. /* Compute combined scale for the block */
  3551. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3552. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3553. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3554. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3555. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3556. bxhil = _mm_andnot_si128(bxhil, mask);
  3557. bxhih = _mm_andnot_si128(bxhih, mask);
  3558. __m128i bxl = _mm256_castsi256_si128(bx);
  3559. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3560. bxl = _mm_or_si128(bxl, bxhil);
  3561. bxh = _mm_or_si128(bxh, bxhih);
  3562. bx = MM256_SET_M128I(bxh, bxl);
  3563. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3564. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3565. /* Multiply q with scale and accumulate */
  3566. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  3567. }
  3568. *s = hsum_float_8(acc);
  3569. #elif defined(__riscv_v_intrinsic)
  3570. float sumf = 0.0;
  3571. uint32_t qh;
  3572. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3573. // These temporary registers are for masking and shift operations
  3574. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3575. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  3576. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  3577. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3578. for (int i = 0; i < nb; i++) {
  3579. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3580. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3581. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  3582. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  3583. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3584. // ((qh & (1u << (j + 16))) >> (j + 12));
  3585. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  3586. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  3587. // narrowing
  3588. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  3589. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3590. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  3591. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3592. // load
  3593. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3594. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3595. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3596. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3597. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3598. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3599. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3600. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3601. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3602. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  3603. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  3604. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3605. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3606. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3607. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3608. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3609. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3610. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3611. }
  3612. *s = sumf;
  3613. #else
  3614. // scalar
  3615. float sumf = 0.0;
  3616. for (int i = 0; i < nb; i++) {
  3617. uint32_t qh;
  3618. memcpy(&qh, x[i].qh, sizeof(qh));
  3619. int sumi = 0;
  3620. for (int j = 0; j < qk/2; ++j) {
  3621. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  3622. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  3623. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  3624. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  3625. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3626. }
  3627. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  3628. }
  3629. *s = sumf;
  3630. #endif
  3631. }
  3632. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3633. const int qk = QK8_1;
  3634. const int nb = n / qk;
  3635. assert(n % qk == 0);
  3636. assert(qk == QK5_1);
  3637. const block_q5_1 * restrict x = vx;
  3638. const block_q8_1 * restrict y = vy;
  3639. #if defined(__ARM_NEON)
  3640. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3641. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3642. float summs0 = 0.0f;
  3643. float summs1 = 0.0f;
  3644. uint32_t qh0;
  3645. uint32_t qh1;
  3646. uint64_t tmp0[4];
  3647. uint64_t tmp1[4];
  3648. assert(nb % 2 == 0); // TODO: handle odd nb
  3649. for (int i = 0; i < nb; i += 2) {
  3650. const block_q5_1 * restrict x0 = &x[i];
  3651. const block_q5_1 * restrict x1 = &x[i + 1];
  3652. const block_q8_1 * restrict y0 = &y[i];
  3653. const block_q8_1 * restrict y1 = &y[i + 1];
  3654. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  3655. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3656. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  3657. // extract the 5th bit via lookup table ((b) << 4)
  3658. memcpy(&qh0, x0->qh, sizeof(qh0));
  3659. memcpy(&qh1, x1->qh, sizeof(qh1));
  3660. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  3661. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  3662. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  3663. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  3664. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  3665. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  3666. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  3667. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  3668. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  3669. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  3670. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  3671. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  3672. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  3673. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  3674. // 4-bit -> 8-bit
  3675. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  3676. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  3677. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  3678. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  3679. // add high bit
  3680. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  3681. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  3682. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  3683. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  3684. // load y
  3685. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  3686. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  3687. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  3688. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  3689. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3690. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  3691. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  3692. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3693. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  3694. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  3695. }
  3696. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  3697. #elif defined(__wasm_simd128__)
  3698. v128_t sumv = wasm_f32x4_splat(0.0f);
  3699. float summs = 0.0f;
  3700. uint32_t qh;
  3701. uint64_t tmp[4];
  3702. // TODO: check if unrolling this is better
  3703. for (int i = 0; i < nb; ++i) {
  3704. const block_q5_1 * restrict x0 = &x[i];
  3705. const block_q8_1 * restrict y0 = &y[i];
  3706. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  3707. const v128_t m4b = wasm_i8x16_splat(0x0F);
  3708. // extract the 5th bit
  3709. memcpy(&qh, x0->qh, sizeof(qh));
  3710. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  3711. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  3712. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  3713. tmp[3] = table_b2b_0[(qh >> 24) ];
  3714. const v128_t qhl = wasm_v128_load(tmp + 0);
  3715. const v128_t qhh = wasm_v128_load(tmp + 2);
  3716. const v128_t v0 = wasm_v128_load(x0->qs);
  3717. // 4-bit -> 8-bit
  3718. const v128_t v0l = wasm_v128_and (v0, m4b);
  3719. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  3720. // add high bit
  3721. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  3722. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  3723. // load y
  3724. const v128_t v1l = wasm_v128_load(y0->qs);
  3725. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  3726. // int8x16 -> int16x8
  3727. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  3728. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  3729. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  3730. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  3731. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  3732. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  3733. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  3734. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  3735. // dot product
  3736. sumv = wasm_f32x4_add(sumv,
  3737. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  3738. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  3739. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  3740. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  3741. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  3742. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  3743. }
  3744. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  3745. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  3746. #elif defined(__AVX2__)
  3747. // Initialize accumulator with zeros
  3748. __m256 acc = _mm256_setzero_ps();
  3749. float summs = 0.0f;
  3750. // Main loop
  3751. for (int i = 0; i < nb; i++) {
  3752. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3753. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3754. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3755. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3756. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  3757. bx = _mm256_or_si256(bx, bxhi);
  3758. const __m256 dy = _mm256_set1_ps(y[i].d);
  3759. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3760. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3761. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  3762. }
  3763. *s = hsum_float_8(acc) + summs;
  3764. #elif defined(__AVX__)
  3765. // Initialize accumulator with zeros
  3766. __m256 acc = _mm256_setzero_ps();
  3767. __m128i mask = _mm_set1_epi8(0x10);
  3768. float summs = 0.0f;
  3769. // Main loop
  3770. for (int i = 0; i < nb; i++) {
  3771. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  3772. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  3773. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  3774. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  3775. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  3776. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  3777. bxhil = _mm_and_si128(bxhil, mask);
  3778. bxhih = _mm_and_si128(bxhih, mask);
  3779. __m128i bxl = _mm256_castsi256_si128(bx);
  3780. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  3781. bxl = _mm_or_si128(bxl, bxhil);
  3782. bxh = _mm_or_si128(bxh, bxhih);
  3783. bx = MM256_SET_M128I(bxh, bxl);
  3784. const __m256 dy = _mm256_set1_ps(y[i].d);
  3785. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3786. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  3787. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  3788. }
  3789. *s = hsum_float_8(acc) + summs;
  3790. #elif defined(__riscv_v_intrinsic)
  3791. float sumf = 0.0;
  3792. uint32_t qh;
  3793. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  3794. // temporary registers for shift operations
  3795. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  3796. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  3797. for (int i = 0; i < nb; i++) {
  3798. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  3799. // load qh
  3800. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  3801. // ((qh >> (j + 0)) << 4) & 0x10;
  3802. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  3803. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  3804. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  3805. // ((qh >> (j + 12)) ) & 0x10;
  3806. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  3807. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  3808. // narrowing
  3809. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  3810. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  3811. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  3812. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  3813. // load
  3814. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  3815. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  3816. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  3817. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  3818. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  3819. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  3820. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  3821. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  3822. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  3823. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  3824. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  3825. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3826. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  3827. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  3828. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  3829. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3830. }
  3831. *s = sumf;
  3832. #else
  3833. // scalar
  3834. float sumf = 0.0;
  3835. for (int i = 0; i < nb; i++) {
  3836. uint32_t qh;
  3837. memcpy(&qh, x[i].qh, sizeof(qh));
  3838. int sumi = 0;
  3839. for (int j = 0; j < qk/2; ++j) {
  3840. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  3841. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  3842. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  3843. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  3844. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  3845. }
  3846. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  3847. }
  3848. *s = sumf;
  3849. #endif
  3850. }
  3851. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3852. const int qk = QK8_0;
  3853. const int nb = n / qk;
  3854. assert(n % qk == 0);
  3855. const block_q8_0 * restrict x = vx;
  3856. const block_q8_0 * restrict y = vy;
  3857. #if defined(__ARM_NEON)
  3858. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  3859. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  3860. assert(nb % 2 == 0); // TODO: handle odd nb
  3861. for (int i = 0; i < nb; i += 2) {
  3862. const block_q8_0 * restrict x0 = &x[i + 0];
  3863. const block_q8_0 * restrict x1 = &x[i + 1];
  3864. const block_q8_0 * restrict y0 = &y[i + 0];
  3865. const block_q8_0 * restrict y1 = &y[i + 1];
  3866. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  3867. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  3868. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  3869. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  3870. // load y
  3871. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  3872. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  3873. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  3874. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  3875. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  3876. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  3877. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  3878. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  3879. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  3880. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  3881. }
  3882. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  3883. #elif defined(__AVX2__) || defined(__AVX__)
  3884. // Initialize accumulator with zeros
  3885. __m256 acc = _mm256_setzero_ps();
  3886. // Main loop
  3887. for (int i = 0; i < nb; ++i) {
  3888. // Compute combined scale for the block
  3889. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  3890. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  3891. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  3892. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  3893. // Multiply q with scale and accumulate
  3894. #if defined(__AVX2__)
  3895. acc = _mm256_fmadd_ps( d, q, acc );
  3896. #else
  3897. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  3898. #endif
  3899. }
  3900. *s = hsum_float_8(acc);
  3901. #elif defined(__riscv_v_intrinsic)
  3902. float sumf = 0.0;
  3903. size_t vl = __riscv_vsetvl_e8m1(qk);
  3904. for (int i = 0; i < nb; i++) {
  3905. // load elements
  3906. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  3907. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  3908. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  3909. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  3910. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  3911. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  3912. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3913. }
  3914. *s = sumf;
  3915. #else
  3916. // scalar
  3917. float sumf = 0.0;
  3918. for (int i = 0; i < nb; i++) {
  3919. int sumi = 0;
  3920. for (int j = 0; j < qk; j++) {
  3921. sumi += x[i].qs[j]*y[i].qs[j];
  3922. }
  3923. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  3924. }
  3925. *s = sumf;
  3926. #endif
  3927. }
  3928. #if QK_K == 256
  3929. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3930. const block_q2_K * restrict x = vx;
  3931. const block_q8_K * restrict y = vy;
  3932. const int nb = n / QK_K;
  3933. #ifdef __ARM_NEON
  3934. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3935. const uint8x16_t m4 = vdupq_n_u8(0xF);
  3936. const int32x4_t vzero = vdupq_n_s32(0);
  3937. ggml_int8x16x2_t q2bytes;
  3938. uint8_t aux[16];
  3939. float sum = 0;
  3940. for (int i = 0; i < nb; ++i) {
  3941. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3942. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3943. const uint8_t * restrict q2 = x[i].qs;
  3944. const int8_t * restrict q8 = y[i].qs;
  3945. const uint8_t * restrict sc = x[i].scales;
  3946. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  3947. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  3948. vst1q_u8(aux, scales);
  3949. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  3950. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  3951. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  3952. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  3953. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  3954. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  3955. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  3956. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  3957. int isum = 0;
  3958. int is = 0;
  3959. // We use this macro instead of a function call because for some reason
  3960. // the code runs 2-3% slower, even if the function is declared inline
  3961. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  3962. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  3963. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  3964. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  3965. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  3966. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  3967. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  3968. MULTIPLY_ACCUM_WITH_SCALE((index));
  3969. for (int j = 0; j < QK_K/128; ++j) {
  3970. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  3971. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  3972. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  3973. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  3974. MULTIPLY_ACCUM_WITH_SCALE(0);
  3975. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  3976. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  3977. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  3978. is += 8;
  3979. }
  3980. sum += d * isum;
  3981. }
  3982. *s = sum;
  3983. #elif defined __AVX2__
  3984. const __m256i m3 = _mm256_set1_epi8(3);
  3985. const __m128i m4 = _mm_set1_epi8(0xF);
  3986. __m256 acc = _mm256_setzero_ps();
  3987. for (int i = 0; i < nb; ++i) {
  3988. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3989. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3990. const uint8_t * restrict q2 = x[i].qs;
  3991. const int8_t * restrict q8 = y[i].qs;
  3992. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3993. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  3994. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  3995. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  3996. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  3997. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  3998. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  3999. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4000. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4001. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4002. __m256i sumi = _mm256_setzero_si256();
  4003. for (int j = 0; j < QK_K/128; ++j) {
  4004. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  4005. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4006. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4007. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4008. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4009. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  4010. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  4011. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  4012. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  4013. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4014. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4015. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  4016. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  4017. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  4018. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  4019. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  4020. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  4021. p0 = _mm256_add_epi32(p0, p1);
  4022. p2 = _mm256_add_epi32(p2, p3);
  4023. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  4024. }
  4025. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4026. }
  4027. *s = hsum_float_8(acc);
  4028. #elif defined __AVX__
  4029. const __m128i m3 = _mm_set1_epi8(0x3);
  4030. const __m128i m4 = _mm_set1_epi8(0xF);
  4031. const __m128i m2 = _mm_set1_epi8(0x2);
  4032. __m256 acc = _mm256_setzero_ps();
  4033. for (int i = 0; i < nb; ++i) {
  4034. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4035. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4036. const uint8_t * restrict q2 = x[i].qs;
  4037. const int8_t * restrict q8 = y[i].qs;
  4038. // load mins and scales from block_q2_K.scales[QK_K/16]
  4039. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4040. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  4041. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  4042. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  4043. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  4044. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  4045. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  4046. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  4047. // sumf += -dmin * summs in 32bits*8
  4048. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  4049. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  4050. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  4051. const __m128i scales[2] = { scales_0, scales_1 };
  4052. __m128i sumi_0 = _mm_setzero_si128();
  4053. __m128i sumi_1 = _mm_setzero_si128();
  4054. for (int j = 0; j < QK_K/128; ++j) {
  4055. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  4056. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4057. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4058. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4059. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4060. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4061. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4062. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4063. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4064. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  4065. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4066. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4067. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4068. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4069. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4070. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  4071. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  4072. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4073. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4074. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4075. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  4076. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  4077. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  4078. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  4079. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  4080. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  4081. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  4082. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  4083. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  4084. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  4085. __m128i shuffle = _mm_set1_epi16(0x0100);
  4086. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  4087. shuffle = _mm_add_epi16(shuffle, m2);
  4088. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  4089. shuffle = _mm_add_epi16(shuffle, m2);
  4090. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  4091. shuffle = _mm_add_epi16(shuffle, m2);
  4092. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  4093. shuffle = _mm_add_epi16(shuffle, m2);
  4094. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  4095. shuffle = _mm_add_epi16(shuffle, m2);
  4096. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  4097. shuffle = _mm_add_epi16(shuffle, m2);
  4098. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  4099. shuffle = _mm_add_epi16(shuffle, m2);
  4100. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  4101. p0 = _mm_add_epi32(p0, p1);
  4102. p2 = _mm_add_epi32(p2, p3);
  4103. p4 = _mm_add_epi32(p4, p5);
  4104. p6 = _mm_add_epi32(p6, p7);
  4105. // isum in 32bits*4*2
  4106. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  4107. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  4108. }
  4109. // sumf += dall * isum - dmin * summs in 32bits
  4110. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4111. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  4112. }
  4113. *s = hsum_float_8(acc);
  4114. #elif defined __riscv_v_intrinsic
  4115. float sumf = 0;
  4116. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  4117. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  4118. for (int i = 0; i < nb; ++i) {
  4119. const uint8_t * q2 = x[i].qs;
  4120. const int8_t * q8 = y[i].qs;
  4121. const uint8_t * sc = x[i].scales;
  4122. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4123. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4124. size_t vl = 16;
  4125. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  4126. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  4127. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  4128. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  4129. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  4130. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  4131. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  4132. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4133. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  4134. vl = 32;
  4135. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4136. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  4137. uint8_t is=0;
  4138. int isum=0;
  4139. for (int j = 0; j < QK_K/128; ++j) {
  4140. // load Q2
  4141. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  4142. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  4143. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  4144. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  4145. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  4146. // duplicate scale elements for product
  4147. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  4148. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  4149. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  4150. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  4151. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  4152. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  4153. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  4154. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  4155. // load Q8
  4156. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4157. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4158. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  4159. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  4160. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  4161. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  4162. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  4163. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  4164. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  4165. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  4166. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  4167. q2+=32; q8+=128; is=8;
  4168. }
  4169. sumf += dall * isum;
  4170. }
  4171. *s = sumf;
  4172. #else
  4173. float sumf = 0;
  4174. for (int i = 0; i < nb; ++i) {
  4175. const uint8_t * q2 = x[i].qs;
  4176. const int8_t * q8 = y[i].qs;
  4177. const uint8_t * sc = x[i].scales;
  4178. int summs = 0;
  4179. for (int j = 0; j < 16; ++j) {
  4180. summs += y[i].bsums[j] * (sc[j] >> 4);
  4181. }
  4182. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4183. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4184. int isum = 0;
  4185. int is = 0;
  4186. int d;
  4187. for (int k = 0; k < QK_K/128; ++k) {
  4188. int shift = 0;
  4189. for (int j = 0; j < 4; ++j) {
  4190. d = sc[is++] & 0xF;
  4191. int isuml = 0;
  4192. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4193. isum += d * isuml;
  4194. d = sc[is++] & 0xF;
  4195. isuml = 0;
  4196. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  4197. isum += d * isuml;
  4198. shift += 2;
  4199. q8 += 32;
  4200. }
  4201. q2 += 32;
  4202. }
  4203. sumf += dall * isum - dmin * summs;
  4204. }
  4205. *s = sumf;
  4206. #endif
  4207. }
  4208. #else
  4209. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4210. const block_q2_K * restrict x = vx;
  4211. const block_q8_K * restrict y = vy;
  4212. const int nb = n / QK_K;
  4213. #ifdef __ARM_NEON
  4214. const uint8x16_t m3 = vdupq_n_u8(0x3);
  4215. const int32x4_t vzero = vdupq_n_s32(0);
  4216. ggml_int8x16x4_t q2bytes;
  4217. uint32_t aux32[2];
  4218. const uint8_t * scales = (const uint8_t *)aux32;
  4219. float sum = 0;
  4220. for (int i = 0; i < nb; ++i) {
  4221. const float d = y[i].d * (float)x[i].d;
  4222. const float dmin = -y[i].d * (float)x[i].dmin;
  4223. const uint8_t * restrict q2 = x[i].qs;
  4224. const int8_t * restrict q8 = y[i].qs;
  4225. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4226. aux32[0] = sc[0] & 0x0f0f0f0f;
  4227. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4228. 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]);
  4229. int isum1 = 0, isum2 = 0;
  4230. const uint8x16_t q2bits = vld1q_u8(q2);
  4231. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4232. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  4233. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  4234. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  4235. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  4236. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  4237. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  4238. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  4239. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  4240. sum += d * (isum1 + isum2);
  4241. }
  4242. *s = sum;
  4243. #elif defined __AVX2__
  4244. const __m256i m3 = _mm256_set1_epi8(3);
  4245. __m256 acc = _mm256_setzero_ps();
  4246. uint32_t ud, um;
  4247. const uint8_t * restrict db = (const uint8_t *)&ud;
  4248. const uint8_t * restrict mb = (const uint8_t *)&um;
  4249. float summs = 0;
  4250. // TODO: optimize this
  4251. for (int i = 0; i < nb; ++i) {
  4252. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4253. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4254. const uint8_t * restrict q2 = x[i].qs;
  4255. const int8_t * restrict q8 = y[i].qs;
  4256. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4257. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4258. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4259. 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];
  4260. summs += dmin * smin;
  4261. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4262. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  4263. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  4264. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4265. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4266. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  4267. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  4268. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  4269. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  4270. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  4271. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  4272. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  4273. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  4274. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  4275. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  4276. }
  4277. *s = hsum_float_8(acc) + summs;
  4278. #elif defined __AVX__
  4279. const __m128i m3 = _mm_set1_epi8(3);
  4280. __m256 acc = _mm256_setzero_ps();
  4281. uint32_t ud, um;
  4282. const uint8_t * restrict db = (const uint8_t *)&ud;
  4283. const uint8_t * restrict mb = (const uint8_t *)&um;
  4284. float summs = 0;
  4285. // TODO: optimize this
  4286. for (int i = 0; i < nb; ++i) {
  4287. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4288. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4289. const uint8_t * restrict q2 = x[i].qs;
  4290. const int8_t * restrict q8 = y[i].qs;
  4291. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4292. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  4293. um = (sc[0] >> 4) & 0x0f0f0f0f;
  4294. 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];
  4295. summs += dmin * smin;
  4296. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  4297. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  4298. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  4299. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  4300. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  4301. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4302. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4303. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  4304. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  4305. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  4306. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  4307. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  4308. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  4309. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  4310. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  4311. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  4312. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  4313. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  4314. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  4315. }
  4316. *s = hsum_float_8(acc) + summs;
  4317. #elif defined __riscv_v_intrinsic
  4318. uint32_t aux32[2];
  4319. const uint8_t * scales = (const uint8_t *)aux32;
  4320. float sumf = 0;
  4321. for (int i = 0; i < nb; ++i) {
  4322. const float d = y[i].d * (float)x[i].d;
  4323. const float dmin = -y[i].d * (float)x[i].dmin;
  4324. const uint8_t * restrict q2 = x[i].qs;
  4325. const int8_t * restrict q8 = y[i].qs;
  4326. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  4327. aux32[0] = sc[0] & 0x0f0f0f0f;
  4328. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  4329. 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]);
  4330. int isum1 = 0;
  4331. int isum2 = 0;
  4332. size_t vl = 16;
  4333. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4334. // load Q2
  4335. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  4336. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  4337. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  4338. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  4339. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  4340. // load Q8, and take product with Q2
  4341. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4342. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4343. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4344. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4345. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  4346. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  4347. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  4348. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  4349. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  4350. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  4351. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  4352. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  4353. sumf += d * (isum1 + isum2);
  4354. }
  4355. *s = sumf;
  4356. #else
  4357. float sumf = 0;
  4358. int isum[4];
  4359. for (int i = 0; i < nb; ++i) {
  4360. const uint8_t * q2 = x[i].qs;
  4361. const int8_t * q8 = y[i].qs;
  4362. const uint8_t * sc = x[i].scales;
  4363. int summs = 0;
  4364. for (int j = 0; j < QK_K/16; ++j) {
  4365. summs += y[i].bsums[j] * (sc[j] >> 4);
  4366. }
  4367. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4368. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4369. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  4370. for (int l = 0; l < 16; ++l) {
  4371. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  4372. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  4373. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  4374. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  4375. }
  4376. for (int l = 0; l < 4; ++l) {
  4377. isum[l] *= (sc[l] & 0xF);
  4378. }
  4379. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  4380. }
  4381. *s = sumf;
  4382. #endif
  4383. }
  4384. #endif
  4385. #if QK_K == 256
  4386. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4387. assert(n % QK_K == 0);
  4388. const uint32_t kmask1 = 0x03030303;
  4389. const uint32_t kmask2 = 0x0f0f0f0f;
  4390. const block_q3_K * restrict x = vx;
  4391. const block_q8_K * restrict y = vy;
  4392. const int nb = n / QK_K;
  4393. #ifdef __ARM_NEON
  4394. uint32_t aux[3];
  4395. uint32_t utmp[4];
  4396. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4397. const int32x4_t vzero = vdupq_n_s32(0);
  4398. const uint8x16_t m0 = vdupq_n_u8(1);
  4399. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  4400. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  4401. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  4402. const int8_t m32 = 32;
  4403. ggml_int8x16x4_t q3bytes;
  4404. float sum = 0;
  4405. for (int i = 0; i < nb; ++i) {
  4406. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4407. const uint8_t * restrict q3 = x[i].qs;
  4408. const uint8_t * restrict qh = x[i].hmask;
  4409. const int8_t * restrict q8 = y[i].qs;
  4410. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4411. ggml_uint8x16x4_t q3h;
  4412. int32_t isum = 0;
  4413. // Set up scales
  4414. memcpy(aux, x[i].scales, 12);
  4415. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4416. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4417. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4418. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4419. int8_t * scale = (int8_t *)utmp;
  4420. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  4421. for (int j = 0; j < QK_K/128; ++j) {
  4422. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  4423. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4424. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  4425. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  4426. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  4427. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  4428. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  4429. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4430. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4431. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4432. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4433. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  4434. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  4435. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  4436. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  4437. scale += 4;
  4438. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  4439. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  4440. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  4441. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  4442. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  4443. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  4444. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  4445. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  4446. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  4447. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  4448. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  4449. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  4450. scale += 4;
  4451. if (j == 0) {
  4452. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  4453. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  4454. }
  4455. }
  4456. sum += d * isum;
  4457. }
  4458. *s = sum;
  4459. #elif defined __AVX2__
  4460. const __m256i m3 = _mm256_set1_epi8(3);
  4461. const __m256i mone = _mm256_set1_epi8(1);
  4462. const __m128i m32 = _mm_set1_epi8(32);
  4463. __m256 acc = _mm256_setzero_ps();
  4464. uint32_t aux[3];
  4465. for (int i = 0; i < nb; ++i) {
  4466. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4467. const uint8_t * restrict q3 = x[i].qs;
  4468. const int8_t * restrict q8 = y[i].qs;
  4469. // Set up scales
  4470. memcpy(aux, x[i].scales, 12);
  4471. __m128i scales128 = _mm_set_epi32(
  4472. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4473. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4474. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4475. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4476. scales128 = _mm_sub_epi8(scales128, m32);
  4477. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  4478. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  4479. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  4480. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  4481. // high bit
  4482. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  4483. // integer accumulator
  4484. __m256i sumi = _mm256_setzero_si256();
  4485. int bit = 0;
  4486. int is = 0;
  4487. for (int j = 0; j < QK_K/128; ++j) {
  4488. // load low 2 bits
  4489. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  4490. // prepare low and high bits
  4491. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  4492. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4493. ++bit;
  4494. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  4495. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4496. ++bit;
  4497. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  4498. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4499. ++bit;
  4500. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  4501. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  4502. ++bit;
  4503. // load Q8 quants
  4504. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4505. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4506. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4507. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4508. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4509. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4510. // and 2 if the high bit was set)
  4511. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4512. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4513. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  4514. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  4515. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4516. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4517. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  4518. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  4519. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4520. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4521. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  4522. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  4523. // multiply with scales
  4524. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  4525. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  4526. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  4527. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  4528. // accumulate
  4529. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4530. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  4531. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  4532. }
  4533. // multiply with block scale and accumulate
  4534. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  4535. }
  4536. *s = hsum_float_8(acc);
  4537. #elif defined __AVX__
  4538. const __m128i m3 = _mm_set1_epi8(3);
  4539. const __m128i mone = _mm_set1_epi8(1);
  4540. const __m128i m32 = _mm_set1_epi8(32);
  4541. const __m128i m2 = _mm_set1_epi8(2);
  4542. __m256 acc = _mm256_setzero_ps();
  4543. const uint32_t *aux;
  4544. for (int i = 0; i < nb; ++i) {
  4545. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4546. const uint8_t * restrict q3 = x[i].qs;
  4547. const int8_t * restrict q8 = y[i].qs;
  4548. // Set up scales
  4549. aux = (const uint32_t *)x[i].scales;
  4550. __m128i scales128 = _mm_set_epi32(
  4551. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  4552. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  4553. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  4554. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  4555. scales128 = _mm_sub_epi8(scales128, m32);
  4556. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  4557. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  4558. const __m128i scales[2] = { scales_0, scales_1 };
  4559. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  4560. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  4561. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  4562. // integer accumulator
  4563. __m128i sumi_0 = _mm_setzero_si128();
  4564. __m128i sumi_1 = _mm_setzero_si128();
  4565. for (int j = 0; j < QK_K/128; ++j) {
  4566. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  4567. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4568. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  4569. // prepare low and high bits
  4570. const int bit = j << 2;
  4571. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  4572. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  4573. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  4574. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  4575. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  4576. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  4577. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4578. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  4579. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  4580. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  4581. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4582. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  4583. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  4584. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  4585. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4586. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  4587. // load Q8 quants from block_q8_K.qs[QK_K]
  4588. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4589. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4590. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4591. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4592. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4593. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4594. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4595. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4596. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4597. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4598. // and 2 if the high bit was set)
  4599. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  4600. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  4601. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  4602. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  4603. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  4604. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  4605. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  4606. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  4607. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  4608. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  4609. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  4610. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  4611. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  4612. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  4613. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  4614. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  4615. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4616. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4617. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4618. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4619. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  4620. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  4621. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  4622. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  4623. // multiply with scales
  4624. __m128i shuffle = _mm_set1_epi16(0x0100);
  4625. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  4626. shuffle = _mm_add_epi16(shuffle, m2);
  4627. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  4628. shuffle = _mm_add_epi16(shuffle, m2);
  4629. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  4630. shuffle = _mm_add_epi16(shuffle, m2);
  4631. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  4632. shuffle = _mm_add_epi16(shuffle, m2);
  4633. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  4634. shuffle = _mm_add_epi16(shuffle, m2);
  4635. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  4636. shuffle = _mm_add_epi16(shuffle, m2);
  4637. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  4638. shuffle = _mm_add_epi16(shuffle, m2);
  4639. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  4640. // accumulate
  4641. p16_0 = _mm_add_epi32(p16_0, p16_1);
  4642. p16_2 = _mm_add_epi32(p16_2, p16_3);
  4643. p16_4 = _mm_add_epi32(p16_4, p16_5);
  4644. p16_6 = _mm_add_epi32(p16_6, p16_7);
  4645. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4646. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  4647. }
  4648. // multiply with block scale and accumulate
  4649. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4650. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  4651. }
  4652. *s = hsum_float_8(acc);
  4653. #elif defined __riscv_v_intrinsic
  4654. uint32_t aux[3];
  4655. uint32_t utmp[4];
  4656. float sumf = 0;
  4657. for (int i = 0; i < nb; ++i) {
  4658. const uint8_t * restrict q3 = x[i].qs;
  4659. const uint8_t * restrict qh = x[i].hmask;
  4660. const int8_t * restrict q8 = y[i].qs;
  4661. memcpy(aux, x[i].scales, 12);
  4662. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  4663. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  4664. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  4665. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  4666. int8_t * scale = (int8_t *)utmp;
  4667. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  4668. size_t vl = 32;
  4669. uint8_t m = 1;
  4670. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4671. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  4672. int sum_t = 0;
  4673. for (int j = 0; j < QK_K; j += 128) {
  4674. vl = 32;
  4675. // load Q3
  4676. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  4677. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  4678. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  4679. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  4680. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  4681. // compute mask for subtraction
  4682. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4683. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  4684. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  4685. m <<= 1;
  4686. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4687. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  4688. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  4689. m <<= 1;
  4690. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4691. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  4692. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  4693. m <<= 1;
  4694. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4695. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  4696. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  4697. m <<= 1;
  4698. // load Q8 and take product with Q3
  4699. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  4700. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4701. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  4702. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  4703. vl = 16;
  4704. // retrieve lane to multiply with scale
  4705. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  4706. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  4707. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  4708. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  4709. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  4710. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  4711. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  4712. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  4713. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  4714. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  4715. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  4716. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  4717. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  4718. q3 += 32; q8 += 128; scale += 8;
  4719. }
  4720. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4721. sumf += d*sum_t;
  4722. }
  4723. *s = sumf;
  4724. #else
  4725. // scalar version
  4726. // This function is written like this so the compiler can manage to vectorize most of it
  4727. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  4728. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  4729. // The ideal situation would be if we could just write the code once, and the compiler would
  4730. // automatically produce the best possible set of machine instructions, instead of us having to manually
  4731. // write vectorized versions for AVX, ARM_NEON, etc.
  4732. int8_t aux8[QK_K];
  4733. int16_t aux16[8];
  4734. float sums [8];
  4735. int32_t aux32[8];
  4736. memset(sums, 0, 8*sizeof(float));
  4737. uint32_t auxs[4];
  4738. const int8_t * scales = (const int8_t*)auxs;
  4739. float sumf = 0;
  4740. for (int i = 0; i < nb; ++i) {
  4741. const uint8_t * restrict q3 = x[i].qs;
  4742. const uint8_t * restrict hm = x[i].hmask;
  4743. const int8_t * restrict q8 = y[i].qs;
  4744. memset(aux32, 0, 8*sizeof(int32_t));
  4745. int8_t * restrict a = aux8;
  4746. uint8_t m = 1;
  4747. for (int j = 0; j < QK_K; j += 128) {
  4748. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  4749. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4750. a += 32; m <<= 1;
  4751. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  4752. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4753. a += 32; m <<= 1;
  4754. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  4755. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4756. a += 32; m <<= 1;
  4757. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  4758. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  4759. a += 32; m <<= 1;
  4760. q3 += 32;
  4761. }
  4762. a = aux8;
  4763. memcpy(auxs, x[i].scales, 12);
  4764. uint32_t tmp = auxs[2];
  4765. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  4766. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  4767. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  4768. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  4769. for (int j = 0; j < QK_K/16; ++j) {
  4770. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4771. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4772. q8 += 8; a += 8;
  4773. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4774. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  4775. q8 += 8; a += 8;
  4776. }
  4777. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4778. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4779. }
  4780. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4781. *s = sumf;
  4782. #endif
  4783. }
  4784. #else
  4785. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4786. assert(n % QK_K == 0);
  4787. const block_q3_K * restrict x = vx;
  4788. const block_q8_K * restrict y = vy;
  4789. const int nb = n / QK_K;
  4790. #ifdef __ARM_NEON
  4791. const int32x4_t vzero = vdupq_n_s32(0);
  4792. const uint8x16_t m3b = vdupq_n_u8(0x3);
  4793. const uint8x16_t mh = vdupq_n_u8(4);
  4794. ggml_int8x16x4_t q3bytes;
  4795. uint16_t aux16[2];
  4796. int8_t * scales = (int8_t *)aux16;
  4797. float sum = 0;
  4798. for (int i = 0; i < nb; ++i) {
  4799. ggml_uint8x16x4_t q3h;
  4800. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  4801. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  4802. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  4803. const uint16_t a = *(const uint16_t *)x[i].scales;
  4804. aux16[0] = a & 0x0f0f;
  4805. aux16[1] = (a >> 4) & 0x0f0f;
  4806. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4807. 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]);
  4808. const float d = y[i].d * (float)x[i].d;
  4809. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  4810. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  4811. q3h.val[1] = vandq_u8(mh, htmp);
  4812. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  4813. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  4814. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  4815. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  4816. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  4817. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  4818. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  4819. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  4820. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  4821. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  4822. sum += d * isum;
  4823. }
  4824. *s = sum;
  4825. #elif defined __AVX2__
  4826. const __m256i m3 = _mm256_set1_epi8(3);
  4827. const __m256i m1 = _mm256_set1_epi8(1);
  4828. __m256 acc = _mm256_setzero_ps();
  4829. uint64_t aux64;
  4830. uint16_t aux16[2];
  4831. const int8_t * aux8 = (const int8_t *)aux16;
  4832. for (int i = 0; i < nb; ++i) {
  4833. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4834. const uint8_t * restrict q3 = x[i].qs;
  4835. const int8_t * restrict q8 = y[i].qs;
  4836. const uint16_t a = *(const uint16_t *)x[i].scales;
  4837. aux16[0] = a & 0x0f0f;
  4838. aux16[1] = (a >> 4) & 0x0f0f;
  4839. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  4840. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  4841. memcpy(&aux64, x[i].hmask, 8);
  4842. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4843. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  4844. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  4845. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  4846. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  4847. // load low 2 bits
  4848. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4849. // prepare low and high bits
  4850. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  4851. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  4852. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  4853. // load Q8 quants
  4854. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4855. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4856. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  4857. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4858. // and 2 if the high bit was set)
  4859. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  4860. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  4861. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  4862. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  4863. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  4864. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  4865. // multiply with scales
  4866. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4867. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4868. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  4869. // multiply with block scale and accumulate
  4870. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  4871. }
  4872. *s = hsum_float_8(acc);
  4873. #elif defined __AVX__
  4874. const __m128i m3 = _mm_set1_epi8(3);
  4875. const __m128i m1 = _mm_set1_epi8(1);
  4876. __m256 acc = _mm256_setzero_ps();
  4877. uint64_t aux64;
  4878. uint16_t aux16[2];
  4879. const int8_t * aux8 = (const int8_t *)aux16;
  4880. for (int i = 0; i < nb; ++i) {
  4881. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4882. const uint8_t * restrict q3 = x[i].qs;
  4883. const int8_t * restrict q8 = y[i].qs;
  4884. const uint16_t a = *(const uint16_t *)x[i].scales;
  4885. aux16[0] = a & 0x0f0f;
  4886. aux16[1] = (a >> 4) & 0x0f0f;
  4887. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  4888. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  4889. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  4890. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  4891. memcpy(&aux64, x[i].hmask, 8);
  4892. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  4893. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  4894. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  4895. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  4896. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  4897. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  4898. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  4899. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  4900. // load low 2 bits
  4901. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  4902. // prepare low and high bits
  4903. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  4904. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  4905. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  4906. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  4907. // load Q8 quants
  4908. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4909. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4910. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  4911. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  4912. // and 2 if the high bit was set)
  4913. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  4914. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  4915. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  4916. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  4917. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  4918. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  4919. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  4920. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  4921. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  4922. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  4923. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  4924. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  4925. // multiply with scales
  4926. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4927. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  4928. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  4929. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  4930. p16_0 = _mm_add_epi32(p16_0, p16_2);
  4931. p16_1 = _mm_add_epi32(p16_1, p16_3);
  4932. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  4933. // multiply with block scale and accumulate
  4934. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  4935. }
  4936. *s = hsum_float_8(acc);
  4937. #elif defined __riscv_v_intrinsic
  4938. uint16_t aux16[2];
  4939. int8_t * scales = (int8_t *)aux16;
  4940. float sumf = 0;
  4941. for (int i = 0; i < nb; ++i) {
  4942. const uint8_t * restrict q3 = x[i].qs;
  4943. const int8_t * restrict q8 = y[i].qs;
  4944. const uint16_t a = *(const uint16_t *)x[i].scales;
  4945. aux16[0] = a & 0x0f0f;
  4946. aux16[1] = (a >> 4) & 0x0f0f;
  4947. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  4948. 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]);
  4949. const float d = y[i].d * (float)x[i].d;
  4950. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4951. // load qh
  4952. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  4953. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4954. size_t vl = 16;
  4955. // extend and combine both qh_x1 and qh_x2
  4956. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4957. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4958. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  4959. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  4960. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  4961. // load Q3
  4962. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  4963. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  4964. 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);
  4965. 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);
  4966. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  4967. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  4968. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  4969. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  4970. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  4971. // load Q8 and take product with Q3
  4972. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4973. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4974. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4975. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4976. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4977. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4978. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4979. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4980. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  4981. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  4982. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  4983. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  4984. sumf += d * isum;
  4985. }
  4986. *s = sumf;
  4987. #else
  4988. int8_t aux8[QK_K];
  4989. int16_t aux16[8];
  4990. float sums [8];
  4991. int32_t aux32[8];
  4992. int32_t scales[4];
  4993. memset(sums, 0, 8*sizeof(float));
  4994. float sumf = 0;
  4995. for (int i = 0; i < nb; ++i) {
  4996. const uint8_t * restrict q3 = x[i].qs;
  4997. const uint8_t * restrict hm = x[i].hmask;
  4998. const int8_t * restrict q8 = y[i].qs;
  4999. int8_t * restrict a = aux8;
  5000. for (int l = 0; l < 8; ++l) {
  5001. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  5002. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  5003. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  5004. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  5005. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  5006. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  5007. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  5008. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  5009. }
  5010. scales[0] = (x[i].scales[0] & 0xF) - 8;
  5011. scales[1] = (x[i].scales[0] >> 4) - 8;
  5012. scales[2] = (x[i].scales[1] & 0xF) - 8;
  5013. scales[3] = (x[i].scales[1] >> 4) - 8;
  5014. memset(aux32, 0, 8*sizeof(int32_t));
  5015. for (int j = 0; j < QK_K/16; ++j) {
  5016. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5017. q8 += 8; a += 8;
  5018. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  5019. q8 += 8; a += 8;
  5020. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  5021. }
  5022. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5023. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5024. }
  5025. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5026. *s = sumf;
  5027. #endif
  5028. }
  5029. #endif
  5030. #if QK_K == 256
  5031. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5032. assert(n % QK_K == 0);
  5033. const block_q4_K * restrict x = vx;
  5034. const block_q8_K * restrict y = vy;
  5035. const int nb = n / QK_K;
  5036. static const uint32_t kmask1 = 0x3f3f3f3f;
  5037. static const uint32_t kmask2 = 0x0f0f0f0f;
  5038. static const uint32_t kmask3 = 0x03030303;
  5039. uint32_t utmp[4];
  5040. #ifdef __ARM_NEON
  5041. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5042. const int32x4_t mzero = vdupq_n_s32(0);
  5043. ggml_int8x16x2_t q4bytes;
  5044. ggml_int8x16x2_t q8bytes;
  5045. float sumf = 0;
  5046. for (int i = 0; i < nb; ++i) {
  5047. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5048. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5049. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5050. memcpy(utmp, x[i].scales, 12);
  5051. uint32x2_t mins8 = { 0 };
  5052. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  5053. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  5054. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5055. utmp[0] &= kmask1;
  5056. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  5057. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5058. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5059. sumf -= dmin * vaddvq_s32(prod);
  5060. const uint8_t * scales = (const uint8_t *)utmp;
  5061. const uint8_t * restrict q4 = x[i].qs;
  5062. const int8_t * restrict q8 = y[i].qs;
  5063. int32_t sumi1 = 0;
  5064. int32_t sumi2 = 0;
  5065. for (int j = 0; j < QK_K/64; ++j) {
  5066. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  5067. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5068. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5069. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5070. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5071. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  5072. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  5073. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5074. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5075. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5076. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  5077. }
  5078. sumf += d * (sumi1 + sumi2);
  5079. }
  5080. *s = sumf;
  5081. #elif defined __AVX2__
  5082. const __m256i m4 = _mm256_set1_epi8(0xF);
  5083. __m256 acc = _mm256_setzero_ps();
  5084. __m128 acc_m = _mm_setzero_ps();
  5085. for (int i = 0; i < nb; ++i) {
  5086. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5087. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5088. memcpy(utmp, x[i].scales, 12);
  5089. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5090. const uint32_t uaux = utmp[1] & kmask1;
  5091. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5092. utmp[2] = uaux;
  5093. utmp[0] &= kmask1;
  5094. const uint8_t * restrict q4 = x[i].qs;
  5095. const int8_t * restrict q8 = y[i].qs;
  5096. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5097. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5098. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5099. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5100. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  5101. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5102. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5103. __m256i sumi = _mm256_setzero_si256();
  5104. for (int j = 0; j < QK_K/64; ++j) {
  5105. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5106. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5107. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5108. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5109. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5110. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5111. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5112. p16l = _mm256_madd_epi16(scale_l, p16l);
  5113. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5114. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5115. p16h = _mm256_madd_epi16(scale_h, p16h);
  5116. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  5117. sumi = _mm256_add_epi32(sumi, sumj);
  5118. }
  5119. __m256 vd = _mm256_set1_ps(d);
  5120. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5121. }
  5122. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5123. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5124. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5125. #elif defined __AVX__
  5126. const __m128i m4 = _mm_set1_epi8(0xF);
  5127. const __m128i m2 = _mm_set1_epi8(0x2);
  5128. __m256 acc = _mm256_setzero_ps();
  5129. __m128 acc_m = _mm_setzero_ps();
  5130. for (int i = 0; i < nb; ++i) {
  5131. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5132. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5133. const uint8_t * restrict q4 = x[i].qs;
  5134. const int8_t * restrict q8 = y[i].qs;
  5135. memcpy(utmp, x[i].scales, 12);
  5136. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5137. const uint32_t uaux = utmp[1] & kmask1;
  5138. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5139. utmp[2] = uaux;
  5140. utmp[0] &= kmask1;
  5141. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5142. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5143. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5144. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5145. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5146. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5147. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5148. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  5149. __m128i sumi_0 = _mm_setzero_si128();
  5150. __m128i sumi_1 = _mm_setzero_si128();
  5151. __m128i shuffle = _mm_set1_epi16(0x0100);
  5152. for (int j = 0; j < QK_K/64; ++j) {
  5153. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  5154. shuffle = _mm_add_epi16(shuffle, m2);
  5155. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  5156. shuffle = _mm_add_epi16(shuffle, m2);
  5157. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5158. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  5159. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5160. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5161. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  5162. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  5163. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5164. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  5165. p16l = _mm_madd_epi16(scale_l, p16l);
  5166. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  5167. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5168. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  5169. p16l = _mm_madd_epi16(scale_l, p16l);
  5170. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  5171. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5172. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  5173. p16h = _mm_madd_epi16(scale_h, p16h);
  5174. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  5175. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5176. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  5177. p16h = _mm_madd_epi16(scale_h, p16h);
  5178. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  5179. }
  5180. __m256 vd = _mm256_set1_ps(d);
  5181. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5182. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5183. }
  5184. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  5185. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  5186. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  5187. #elif defined __riscv_v_intrinsic
  5188. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5189. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5190. float sumf = 0;
  5191. for (int i = 0; i < nb; ++i) {
  5192. size_t vl = 8;
  5193. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5194. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5195. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5196. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5197. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5198. memcpy(utmp, x[i].scales, 12);
  5199. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5200. const uint32_t uaux = utmp[1] & kmask1;
  5201. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5202. utmp[2] = uaux;
  5203. utmp[0] &= kmask1;
  5204. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5205. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5206. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5207. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5208. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5209. const uint8_t * restrict q4 = x[i].qs;
  5210. const int8_t * restrict q8 = y[i].qs;
  5211. vl = 32;
  5212. int32_t sum_1 = 0;
  5213. int32_t sum_2 = 0;
  5214. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5215. for (int j = 0; j < QK_K/64; ++j) {
  5216. // load Q4
  5217. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5218. // load Q8 and multiply it with lower Q4 nibble
  5219. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  5220. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5221. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  5222. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  5223. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  5224. // load Q8 and multiply it with upper Q4 nibble
  5225. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  5226. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5227. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  5228. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  5229. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  5230. q4 += 32; q8 += 64;
  5231. }
  5232. sumf += d*(sum_1 + sum_2);
  5233. }
  5234. *s = sumf;
  5235. #else
  5236. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5237. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5238. int8_t aux8[QK_K];
  5239. int16_t aux16[8];
  5240. float sums [8];
  5241. int32_t aux32[8];
  5242. memset(sums, 0, 8*sizeof(float));
  5243. float sumf = 0;
  5244. for (int i = 0; i < nb; ++i) {
  5245. const uint8_t * restrict q4 = x[i].qs;
  5246. const int8_t * restrict q8 = y[i].qs;
  5247. memset(aux32, 0, 8*sizeof(int32_t));
  5248. int8_t * restrict a = aux8;
  5249. for (int j = 0; j < QK_K/64; ++j) {
  5250. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5251. a += 32;
  5252. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5253. a += 32; q4 += 32;
  5254. }
  5255. memcpy(utmp, x[i].scales, 12);
  5256. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5257. const uint32_t uaux = utmp[1] & kmask1;
  5258. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5259. utmp[2] = uaux;
  5260. utmp[0] &= kmask1;
  5261. int sumi = 0;
  5262. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5263. a = aux8;
  5264. int is = 0;
  5265. for (int j = 0; j < QK_K/32; ++j) {
  5266. int32_t scale = scales[is++];
  5267. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5268. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5269. q8 += 8; a += 8;
  5270. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5271. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5272. q8 += 8; a += 8;
  5273. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5274. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5275. q8 += 8; a += 8;
  5276. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5277. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5278. q8 += 8; a += 8;
  5279. }
  5280. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5281. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5282. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5283. sumf -= dmin * sumi;
  5284. }
  5285. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5286. *s = sumf;
  5287. #endif
  5288. }
  5289. #else
  5290. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5291. assert(n % QK_K == 0);
  5292. const block_q4_K * restrict x = vx;
  5293. const block_q8_K * restrict y = vy;
  5294. const int nb = n / QK_K;
  5295. #ifdef __ARM_NEON
  5296. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5297. const int32x4_t mzero = vdupq_n_s32(0);
  5298. float sumf = 0;
  5299. ggml_int8x16x2_t q4bytes;
  5300. ggml_int8x16x4_t q8bytes;
  5301. float sum_mins = 0.f;
  5302. uint16_t aux16[2];
  5303. const uint8_t * restrict scales = (const uint8_t *)aux16;
  5304. for (int i = 0; i < nb; ++i) {
  5305. const uint8_t * restrict q4 = x[i].qs;
  5306. const int8_t * restrict q8 = y[i].qs;
  5307. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  5308. aux16[0] = a[0] & 0x0f0f;
  5309. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5310. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  5311. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  5312. const float d = y[i].d * (float)x[i].d[0];
  5313. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  5314. q8bytes = ggml_vld1q_s8_x4(q8);
  5315. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  5316. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  5317. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  5318. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  5319. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  5320. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  5321. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  5322. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  5323. sumf += d * (sumi1 + sumi2);
  5324. }
  5325. *s = sumf - sum_mins;
  5326. #elif defined __AVX2__
  5327. const __m256i m4 = _mm256_set1_epi8(0xF);
  5328. __m256 acc = _mm256_setzero_ps();
  5329. float summs = 0;
  5330. uint16_t aux16[2];
  5331. const uint8_t * scales = (const uint8_t *)aux16;
  5332. for (int i = 0; i < nb; ++i) {
  5333. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5334. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5335. const __m256 vd = _mm256_set1_ps(d);
  5336. const uint16_t * a = (const uint16_t *)x[i].scales;
  5337. aux16[0] = a[0] & 0x0f0f;
  5338. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5339. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5340. const uint8_t * restrict q4 = x[i].qs;
  5341. const int8_t * restrict q8 = y[i].qs;
  5342. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5343. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  5344. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  5345. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5346. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  5347. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  5348. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  5349. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  5350. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  5351. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  5352. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  5353. }
  5354. *s = hsum_float_8(acc) - summs;
  5355. #elif defined __AVX__
  5356. const __m128i m4 = _mm_set1_epi8(0xF);
  5357. __m256 acc = _mm256_setzero_ps();
  5358. float summs = 0;
  5359. uint16_t aux16[2];
  5360. const uint8_t * scales = (const uint8_t *)aux16;
  5361. for (int i = 0; i < nb; ++i) {
  5362. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  5363. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  5364. const __m256 vd = _mm256_set1_ps(d);
  5365. const uint16_t * a = (const uint16_t *)x[i].scales;
  5366. aux16[0] = a[0] & 0x0f0f;
  5367. aux16[1] = (a[0] >> 4) & 0x0f0f;
  5368. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  5369. const uint8_t * restrict q4 = x[i].qs;
  5370. const int8_t * restrict q8 = y[i].qs;
  5371. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  5372. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  5373. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  5374. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  5375. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  5376. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  5377. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  5378. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5379. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5380. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5381. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5382. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5383. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5384. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  5385. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  5386. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  5387. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  5388. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  5389. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  5390. }
  5391. *s = hsum_float_8(acc) - summs;
  5392. #elif defined __riscv_v_intrinsic
  5393. uint16_t s16[2];
  5394. const uint8_t * restrict scales = (const uint8_t *)s16;
  5395. float sumf = 0;
  5396. for (int i = 0; i < nb; ++i) {
  5397. const uint8_t * restrict q4 = x[i].qs;
  5398. const int8_t * restrict q8 = y[i].qs;
  5399. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5400. s16[0] = b[0] & 0x0f0f;
  5401. s16[1] = (b[0] >> 4) & 0x0f0f;
  5402. 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]));
  5403. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5404. size_t vl = 32;
  5405. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  5406. // load Q4
  5407. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  5408. // load Q8 and multiply it with lower Q4 nibble
  5409. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  5410. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  5411. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  5412. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  5413. // load Q8 and multiply it with upper Q4 nibble
  5414. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  5415. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5416. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  5417. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  5418. }
  5419. *s = sumf;
  5420. #else
  5421. uint8_t aux8[QK_K];
  5422. int16_t aux16[16];
  5423. float sums [8];
  5424. memset(sums, 0, 8*sizeof(float));
  5425. uint16_t s16[2];
  5426. const uint8_t * restrict scales = (const uint8_t *)s16;
  5427. float sumf = 0;
  5428. for (int i = 0; i < nb; ++i) {
  5429. const uint8_t * restrict q4 = x[i].qs;
  5430. const int8_t * restrict q8 = y[i].qs;
  5431. uint8_t * restrict a = aux8;
  5432. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  5433. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  5434. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  5435. s16[0] = b[0] & 0x0f0f;
  5436. s16[1] = (b[0] >> 4) & 0x0f0f;
  5437. 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]));
  5438. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  5439. for (int j = 0; j < QK_K/32; ++j) {
  5440. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5441. q8 += 16; a += 16;
  5442. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  5443. q8 += 16; a += 16;
  5444. const float dl = d * scales[j];
  5445. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  5446. }
  5447. }
  5448. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5449. *s = sumf;
  5450. #endif
  5451. }
  5452. #endif
  5453. #if QK_K == 256
  5454. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5455. assert(n % QK_K == 0);
  5456. const block_q5_K * restrict x = vx;
  5457. const block_q8_K * restrict y = vy;
  5458. const int nb = n / QK_K;
  5459. static const uint32_t kmask1 = 0x3f3f3f3f;
  5460. static const uint32_t kmask2 = 0x0f0f0f0f;
  5461. static const uint32_t kmask3 = 0x03030303;
  5462. uint32_t utmp[4];
  5463. #ifdef __ARM_NEON
  5464. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5465. const uint8x16_t mone = vdupq_n_u8(1);
  5466. const uint8x16_t mtwo = vdupq_n_u8(2);
  5467. const int32x4_t mzero = vdupq_n_s32(0);
  5468. ggml_int8x16x4_t q5bytes;
  5469. float sumf = 0;
  5470. for (int i = 0; i < nb; ++i) {
  5471. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5472. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5473. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  5474. memcpy(utmp, x[i].scales, 12);
  5475. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5476. const uint32_t uaux = utmp[1] & kmask1;
  5477. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5478. utmp[2] = uaux;
  5479. utmp[0] &= kmask1;
  5480. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  5481. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  5482. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  5483. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  5484. int32_t sumi_mins = vaddvq_s32(prod);
  5485. const uint8_t * scales = (const uint8_t *)utmp;
  5486. const uint8_t * restrict q5 = x[i].qs;
  5487. const uint8_t * restrict qh = x[i].qh;
  5488. const int8_t * restrict q8 = y[i].qs;
  5489. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  5490. ggml_uint8x16x4_t q5h;
  5491. int32_t sumi = 0;
  5492. for (int j = 0; j < QK_K/64; ++j) {
  5493. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  5494. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5495. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5496. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5497. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  5498. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  5499. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  5500. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  5501. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  5502. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  5503. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  5504. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  5505. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  5506. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  5507. }
  5508. sumf += d * sumi - dmin * sumi_mins;
  5509. }
  5510. *s = sumf;
  5511. #elif defined __AVX2__
  5512. const __m256i m4 = _mm256_set1_epi8(0xF);
  5513. const __m128i mzero = _mm_setzero_si128();
  5514. const __m256i mone = _mm256_set1_epi8(1);
  5515. __m256 acc = _mm256_setzero_ps();
  5516. float summs = 0.f;
  5517. for (int i = 0; i < nb; ++i) {
  5518. const uint8_t * restrict q5 = x[i].qs;
  5519. const int8_t * restrict q8 = y[i].qs;
  5520. #if QK_K == 256
  5521. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5522. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5523. memcpy(utmp, x[i].scales, 12);
  5524. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5525. const uint32_t uaux = utmp[1] & kmask1;
  5526. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5527. utmp[2] = uaux;
  5528. utmp[0] &= kmask1;
  5529. #else
  5530. // TODO
  5531. const float d = 0, dmin = 0;
  5532. #endif
  5533. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  5534. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  5535. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  5536. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  5537. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5538. summs += dmin * _mm_extract_epi32(hsum, 0);
  5539. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  5540. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  5541. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  5542. __m256i hmask = mone;
  5543. __m256i sumi = _mm256_setzero_si256();
  5544. int bit = 0;
  5545. for (int j = 0; j < QK_K/64; ++j) {
  5546. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  5547. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  5548. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  5549. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5550. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5551. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  5552. hmask = _mm256_slli_epi16(hmask, 1);
  5553. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5554. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  5555. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  5556. hmask = _mm256_slli_epi16(hmask, 1);
  5557. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5558. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5559. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  5560. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  5561. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  5562. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  5563. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5564. }
  5565. __m256 vd = _mm256_set1_ps(d);
  5566. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  5567. }
  5568. *s = hsum_float_8(acc) + summs;
  5569. #elif defined __AVX__
  5570. const __m128i m4 = _mm_set1_epi8(0xF);
  5571. const __m128i mzero = _mm_setzero_si128();
  5572. const __m128i mone = _mm_set1_epi8(1);
  5573. const __m128i m2 = _mm_set1_epi8(2);
  5574. __m256 acc = _mm256_setzero_ps();
  5575. float summs = 0.f;
  5576. for (int i = 0; i < nb; ++i) {
  5577. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5578. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  5579. const uint8_t * restrict q5 = x[i].qs;
  5580. const int8_t * restrict q8 = y[i].qs;
  5581. memcpy(utmp, x[i].scales, 12);
  5582. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5583. const uint32_t uaux = utmp[1] & kmask1;
  5584. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5585. utmp[2] = uaux;
  5586. utmp[0] &= kmask1;
  5587. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  5588. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  5589. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  5590. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  5591. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  5592. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  5593. const __m128i prod = _mm_madd_epi16(mins, q8s);
  5594. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  5595. summs += dmin * _mm_extract_epi32(hsum, 0);
  5596. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  5597. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  5598. __m128i hmask = mone;
  5599. __m128i sumi_0 = _mm_setzero_si128();
  5600. __m128i sumi_1 = _mm_setzero_si128();
  5601. int bit = 0;
  5602. __m128i shuffle = _mm_set1_epi16(0x0100);
  5603. for (int j = 0; j < QK_K/64; ++j) {
  5604. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5605. shuffle = _mm_add_epi16(shuffle, m2);
  5606. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5607. shuffle = _mm_add_epi16(shuffle, m2);
  5608. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5609. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  5610. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  5611. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  5612. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5613. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5614. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5615. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5616. hmask = _mm_slli_epi16(hmask, 1);
  5617. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5618. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5619. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  5620. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  5621. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  5622. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  5623. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  5624. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  5625. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  5626. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  5627. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  5628. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  5629. hmask = _mm_slli_epi16(hmask, 1);
  5630. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5631. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5632. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  5633. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  5634. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  5635. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  5636. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5637. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5638. }
  5639. __m256 vd = _mm256_set1_ps(d);
  5640. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5641. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  5642. }
  5643. *s = hsum_float_8(acc) + summs;
  5644. #elif defined __riscv_v_intrinsic
  5645. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5646. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5647. float sumf = 0;
  5648. float sums = 0.0;
  5649. size_t vl;
  5650. for (int i = 0; i < nb; ++i) {
  5651. vl = 8;
  5652. const uint8_t * restrict q5 = x[i].qs;
  5653. const uint8_t * restrict hm = x[i].qh;
  5654. const int8_t * restrict q8 = y[i].qs;
  5655. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5656. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5657. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  5658. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  5659. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  5660. memcpy(utmp, x[i].scales, 12);
  5661. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5662. const uint32_t uaux = utmp[1] & kmask1;
  5663. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5664. utmp[2] = uaux;
  5665. utmp[0] &= kmask1;
  5666. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  5667. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  5668. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  5669. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  5670. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  5671. vl = 32;
  5672. int32_t aux32 = 0;
  5673. int is = 0;
  5674. uint8_t m = 1;
  5675. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5676. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  5677. for (int j = 0; j < QK_K/64; ++j) {
  5678. // load Q5 and Q8
  5679. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  5680. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  5681. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  5682. // compute mask for addition
  5683. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  5684. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5685. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  5686. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  5687. m <<= 1;
  5688. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  5689. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  5690. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  5691. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  5692. m <<= 1;
  5693. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  5694. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  5695. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  5696. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  5697. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  5698. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  5699. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  5700. q5 += 32; q8 += 64;
  5701. }
  5702. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  5703. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  5704. }
  5705. *s = sumf+sums;
  5706. #else
  5707. const uint8_t * scales = (const uint8_t*)&utmp[0];
  5708. const uint8_t * mins = (const uint8_t*)&utmp[2];
  5709. int8_t aux8[QK_K];
  5710. int16_t aux16[8];
  5711. float sums [8];
  5712. int32_t aux32[8];
  5713. memset(sums, 0, 8*sizeof(float));
  5714. float sumf = 0;
  5715. for (int i = 0; i < nb; ++i) {
  5716. const uint8_t * restrict q4 = x[i].qs;
  5717. const uint8_t * restrict hm = x[i].qh;
  5718. const int8_t * restrict q8 = y[i].qs;
  5719. memset(aux32, 0, 8*sizeof(int32_t));
  5720. int8_t * restrict a = aux8;
  5721. uint8_t m = 1;
  5722. for (int j = 0; j < QK_K/64; ++j) {
  5723. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  5724. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5725. a += 32; m <<= 1;
  5726. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  5727. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  5728. a += 32; m <<= 1;
  5729. q4 += 32;
  5730. }
  5731. memcpy(utmp, x[i].scales, 12);
  5732. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  5733. const uint32_t uaux = utmp[1] & kmask1;
  5734. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  5735. utmp[2] = uaux;
  5736. utmp[0] &= kmask1;
  5737. int sumi = 0;
  5738. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  5739. a = aux8;
  5740. int is = 0;
  5741. for (int j = 0; j < QK_K/32; ++j) {
  5742. int32_t scale = scales[is++];
  5743. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5744. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5745. q8 += 8; a += 8;
  5746. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5747. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5748. q8 += 8; a += 8;
  5749. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5750. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5751. q8 += 8; a += 8;
  5752. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5753. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5754. q8 += 8; a += 8;
  5755. }
  5756. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5757. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5758. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  5759. sumf -= dmin * sumi;
  5760. }
  5761. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5762. *s = sumf;
  5763. #endif
  5764. }
  5765. #else
  5766. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5767. assert(n % QK_K == 0);
  5768. const block_q5_K * restrict x = vx;
  5769. const block_q8_K * restrict y = vy;
  5770. const int nb = n / QK_K;
  5771. #ifdef __ARM_NEON
  5772. const uint8x16_t m4b = vdupq_n_u8(0xf);
  5773. const uint8x16_t mh = vdupq_n_u8(16);
  5774. const int32x4_t mzero = vdupq_n_s32(0);
  5775. ggml_int8x16x4_t q5bytes;
  5776. ggml_uint8x16x4_t q5h;
  5777. float sumf = 0;
  5778. for (int i = 0; i < nb; ++i) {
  5779. const float d = y[i].d * (float)x[i].d;
  5780. const int8_t * sc = x[i].scales;
  5781. const uint8_t * restrict q5 = x[i].qs;
  5782. const uint8_t * restrict qh = x[i].qh;
  5783. const int8_t * restrict q8 = y[i].qs;
  5784. const uint8x8_t qhbits = vld1_u8(qh);
  5785. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  5786. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5787. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  5788. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  5789. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  5790. q5h.val[2] = vbicq_u8(mh, htmp);
  5791. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  5792. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  5793. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  5794. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  5795. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  5796. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  5797. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  5798. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  5799. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  5800. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5801. }
  5802. *s = sumf;
  5803. #elif defined __AVX2__
  5804. const __m256i m4 = _mm256_set1_epi8(0xF);
  5805. const __m256i mone = _mm256_set1_epi8(1);
  5806. __m256 acc = _mm256_setzero_ps();
  5807. for (int i = 0; i < nb; ++i) {
  5808. const uint8_t * restrict q5 = x[i].qs;
  5809. const int8_t * restrict q8 = y[i].qs;
  5810. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5811. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5812. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  5813. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  5814. int64_t aux64;
  5815. memcpy(&aux64, x[i].qh, 8);
  5816. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  5817. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  5818. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  5819. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  5820. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  5821. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  5822. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5823. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5824. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  5825. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  5826. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  5827. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  5828. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  5829. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  5830. }
  5831. *s = hsum_float_8(acc);
  5832. #elif defined __AVX__
  5833. const __m128i m4 = _mm_set1_epi8(0xF);
  5834. const __m128i mone = _mm_set1_epi8(1);
  5835. __m256 acc = _mm256_setzero_ps();
  5836. for (int i = 0; i < nb; ++i) {
  5837. const uint8_t * restrict q5 = x[i].qs;
  5838. const int8_t * restrict q8 = y[i].qs;
  5839. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5840. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  5841. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  5842. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  5843. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  5844. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  5845. int64_t aux64;
  5846. memcpy(&aux64, x[i].qh, 8);
  5847. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  5848. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  5849. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  5850. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  5851. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  5852. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  5853. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  5854. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  5855. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  5856. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  5857. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5858. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5859. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  5860. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  5861. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  5862. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  5863. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  5864. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  5865. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  5866. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  5867. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  5868. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  5869. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  5870. }
  5871. *s = hsum_float_8(acc);
  5872. #elif defined __riscv_v_intrinsic
  5873. float sumf = 0;
  5874. for (int i = 0; i < nb; ++i) {
  5875. const float d = y[i].d * (float)x[i].d;
  5876. const int8_t * sc = x[i].scales;
  5877. const uint8_t * restrict q5 = x[i].qs;
  5878. const uint8_t * restrict qh = x[i].qh;
  5879. const int8_t * restrict q8 = y[i].qs;
  5880. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5881. // load qh
  5882. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  5883. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  5884. size_t vl = 16;
  5885. // combine both qh_1 and qh_2
  5886. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  5887. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5888. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  5889. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  5890. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  5891. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  5892. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  5893. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  5894. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  5895. // load q5
  5896. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  5897. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  5898. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  5899. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  5900. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  5901. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  5902. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  5903. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  5904. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  5905. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  5906. // load Q8 and multiply it with Q5
  5907. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5908. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5909. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5910. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5911. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5912. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5913. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5914. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5915. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  5916. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  5917. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  5918. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  5919. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  5920. }
  5921. *s = sumf;
  5922. #else
  5923. int8_t aux8[QK_K];
  5924. int16_t aux16[16];
  5925. float sums [8];
  5926. memset(sums, 0, 8*sizeof(float));
  5927. float sumf = 0;
  5928. for (int i = 0; i < nb; ++i) {
  5929. const uint8_t * restrict q4 = x[i].qs;
  5930. const uint8_t * restrict hm = x[i].qh;
  5931. const int8_t * restrict q8 = y[i].qs;
  5932. int8_t * restrict a = aux8;
  5933. for (int l = 0; l < 32; ++l) {
  5934. a[l+ 0] = q4[l] & 0xF;
  5935. a[l+32] = q4[l] >> 4;
  5936. }
  5937. for (int is = 0; is < 8; ++is) {
  5938. uint8_t m = 1 << is;
  5939. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  5940. }
  5941. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5942. const int8_t * restrict sc = x[i].scales;
  5943. for (int j = 0; j < QK_K/16; ++j) {
  5944. const float dl = d * sc[j];
  5945. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  5946. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  5947. q8 += 16; a += 16;
  5948. }
  5949. }
  5950. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5951. *s = sumf;
  5952. #endif
  5953. }
  5954. #endif
  5955. #if QK_K == 256
  5956. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5957. assert(n % QK_K == 0);
  5958. const block_q6_K * restrict x = vx;
  5959. const block_q8_K * restrict y = vy;
  5960. const int nb = n / QK_K;
  5961. #ifdef __ARM_NEON
  5962. float sum = 0;
  5963. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5964. const int32x4_t vzero = vdupq_n_s32(0);
  5965. //const int8x16_t m32s = vdupq_n_s8(32);
  5966. const uint8x16_t mone = vdupq_n_u8(3);
  5967. ggml_int8x16x4_t q6bytes;
  5968. ggml_uint8x16x4_t q6h;
  5969. for (int i = 0; i < nb; ++i) {
  5970. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  5971. const uint8_t * restrict q6 = x[i].ql;
  5972. const uint8_t * restrict qh = x[i].qh;
  5973. const int8_t * restrict q8 = y[i].qs;
  5974. const int8_t * restrict scale = x[i].scales;
  5975. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  5976. const int8x16_t scales = vld1q_s8(scale);
  5977. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  5978. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  5979. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  5980. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  5981. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  5982. int32_t isum_mins = vaddvq_s32(prod);
  5983. int32_t isum = 0;
  5984. for (int j = 0; j < QK_K/128; ++j) {
  5985. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  5986. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  5987. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  5988. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  5989. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  5990. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  5991. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5992. shifted = vshrq_n_u8(qhbits.val[1], 2);
  5993. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5994. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5995. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5996. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  5997. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  5998. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  5999. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  6000. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  6001. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  6002. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6003. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6004. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6005. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6006. scale += 4;
  6007. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  6008. shifted = vshrq_n_u8(qhbits.val[0], 4);
  6009. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6010. shifted = vshrq_n_u8(qhbits.val[1], 4);
  6011. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6012. shifted = vshrq_n_u8(qhbits.val[0], 6);
  6013. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6014. shifted = vshrq_n_u8(qhbits.val[1], 6);
  6015. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6016. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  6017. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  6018. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  6019. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  6020. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  6021. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  6022. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  6023. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  6024. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6025. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6026. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6027. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6028. scale += 4;
  6029. }
  6030. //sum += isum * d_all * y[i].d;
  6031. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  6032. }
  6033. *s = sum;
  6034. #elif defined __AVX2__
  6035. const __m256i m4 = _mm256_set1_epi8(0xF);
  6036. const __m256i m2 = _mm256_set1_epi8(3);
  6037. const __m256i m32s = _mm256_set1_epi8(32);
  6038. __m256 acc = _mm256_setzero_ps();
  6039. for (int i = 0; i < nb; ++i) {
  6040. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6041. const uint8_t * restrict q4 = x[i].ql;
  6042. const uint8_t * restrict qh = x[i].qh;
  6043. const int8_t * restrict q8 = y[i].qs;
  6044. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6045. __m256i sumi = _mm256_setzero_si256();
  6046. int is = 0;
  6047. for (int j = 0; j < QK_K/128; ++j) {
  6048. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  6049. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  6050. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  6051. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  6052. is += 4;
  6053. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6054. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  6055. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  6056. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  6057. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  6058. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  6059. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  6060. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6061. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  6062. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  6063. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  6064. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6065. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6066. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6067. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  6068. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6069. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6070. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  6071. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  6072. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6073. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6074. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  6075. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  6076. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6077. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6078. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  6079. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  6080. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6081. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6082. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  6083. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  6084. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6085. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  6086. }
  6087. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6088. }
  6089. *s = hsum_float_8(acc);
  6090. #elif defined __AVX__
  6091. const __m128i m4 = _mm_set1_epi8(0xF);
  6092. const __m128i m3 = _mm_set1_epi8(3);
  6093. const __m128i m32s = _mm_set1_epi8(32);
  6094. const __m128i m2 = _mm_set1_epi8(2);
  6095. __m256 acc = _mm256_setzero_ps();
  6096. for (int i = 0; i < nb; ++i) {
  6097. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6098. const uint8_t * restrict q4 = x[i].ql;
  6099. const uint8_t * restrict qh = x[i].qh;
  6100. const int8_t * restrict q8 = y[i].qs;
  6101. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  6102. __m128i sumi_0 = _mm_setzero_si128();
  6103. __m128i sumi_1 = _mm_setzero_si128();
  6104. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  6105. for (int j = 0; j < QK_K/128; ++j) {
  6106. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6107. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  6108. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  6109. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  6110. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  6111. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  6112. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  6113. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  6114. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  6115. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  6116. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6117. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6118. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6119. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  6120. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  6121. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  6122. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  6123. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  6124. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  6125. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  6126. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  6127. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  6128. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6129. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6130. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6131. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6132. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6133. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6134. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6135. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  6136. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  6137. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  6138. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  6139. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  6140. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  6141. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  6142. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  6143. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  6144. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  6145. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  6146. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  6147. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  6148. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  6149. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  6150. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  6151. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  6152. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6153. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6154. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6155. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6156. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  6157. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  6158. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  6159. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  6160. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  6161. shuffle = _mm_add_epi8(shuffle, m2);
  6162. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  6163. shuffle = _mm_add_epi8(shuffle, m2);
  6164. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  6165. shuffle = _mm_add_epi8(shuffle, m2);
  6166. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  6167. shuffle = _mm_add_epi8(shuffle, m2);
  6168. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6169. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6170. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6171. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6172. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  6173. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  6174. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  6175. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  6176. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6177. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6178. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  6179. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  6180. }
  6181. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  6182. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  6183. }
  6184. *s = hsum_float_8(acc);
  6185. #elif defined __riscv_v_intrinsic
  6186. float sumf = 0;
  6187. for (int i = 0; i < nb; ++i) {
  6188. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6189. const uint8_t * restrict q6 = x[i].ql;
  6190. const uint8_t * restrict qh = x[i].qh;
  6191. const int8_t * restrict q8 = y[i].qs;
  6192. const int8_t * restrict scale = x[i].scales;
  6193. size_t vl;
  6194. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6195. int sum_t = 0;
  6196. int is = 0;
  6197. for (int j = 0; j < QK_K/128; ++j) {
  6198. vl = 32;
  6199. // load qh
  6200. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  6201. // load Q6
  6202. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  6203. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  6204. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  6205. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  6206. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  6207. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  6208. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  6209. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  6210. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  6211. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  6212. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  6213. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  6214. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  6215. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  6216. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  6217. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  6218. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  6219. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  6220. // load Q8 and take product
  6221. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  6222. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  6223. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  6224. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  6225. vl = 16;
  6226. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  6227. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  6228. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  6229. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  6230. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  6231. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  6232. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  6233. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  6234. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  6235. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  6236. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  6237. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  6238. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  6239. q6 += 64; qh += 32; q8 += 128; is=8;
  6240. }
  6241. sumf += d * sum_t;
  6242. }
  6243. *s = sumf;
  6244. #else
  6245. int8_t aux8[QK_K];
  6246. int16_t aux16[8];
  6247. float sums [8];
  6248. int32_t aux32[8];
  6249. memset(sums, 0, 8*sizeof(float));
  6250. float sumf = 0;
  6251. for (int i = 0; i < nb; ++i) {
  6252. const uint8_t * restrict q4 = x[i].ql;
  6253. const uint8_t * restrict qh = x[i].qh;
  6254. const int8_t * restrict q8 = y[i].qs;
  6255. memset(aux32, 0, 8*sizeof(int32_t));
  6256. int8_t * restrict a = aux8;
  6257. for (int j = 0; j < QK_K; j += 128) {
  6258. for (int l = 0; l < 32; ++l) {
  6259. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6260. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6261. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6262. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6263. }
  6264. a += 128;
  6265. q4 += 64;
  6266. qh += 32;
  6267. }
  6268. a = aux8;
  6269. int is = 0;
  6270. for (int j = 0; j < QK_K/16; ++j) {
  6271. int scale = x[i].scales[is++];
  6272. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6273. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6274. q8 += 8; a += 8;
  6275. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6276. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6277. q8 += 8; a += 8;
  6278. }
  6279. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6280. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6281. }
  6282. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6283. *s = sumf;
  6284. #endif
  6285. }
  6286. #else
  6287. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6288. assert(n % QK_K == 0);
  6289. const block_q6_K * restrict x = vx;
  6290. const block_q8_K * restrict y = vy;
  6291. const int nb = n / QK_K;
  6292. #ifdef __ARM_NEON
  6293. float sum = 0;
  6294. const uint8x16_t m4b = vdupq_n_u8(0xF);
  6295. const int8x16_t m32s = vdupq_n_s8(32);
  6296. const int32x4_t vzero = vdupq_n_s32(0);
  6297. const uint8x16_t mone = vdupq_n_u8(3);
  6298. ggml_int8x16x4_t q6bytes;
  6299. ggml_uint8x16x4_t q6h;
  6300. for (int i = 0; i < nb; ++i) {
  6301. const float d_all = (float)x[i].d;
  6302. const uint8_t * restrict q6 = x[i].ql;
  6303. const uint8_t * restrict qh = x[i].qh;
  6304. const int8_t * restrict q8 = y[i].qs;
  6305. const int8_t * restrict scale = x[i].scales;
  6306. int32_t isum = 0;
  6307. uint8x16_t qhbits = vld1q_u8(qh);
  6308. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  6309. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  6310. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  6311. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  6312. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6313. shifted = vshrq_n_u8(qhbits, 4);
  6314. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6315. shifted = vshrq_n_u8(qhbits, 6);
  6316. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  6317. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  6318. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  6319. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  6320. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  6321. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  6322. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  6323. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  6324. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  6325. sum += isum * d_all * y[i].d;
  6326. }
  6327. *s = sum;
  6328. #elif defined __AVX2__
  6329. const __m256i m4 = _mm256_set1_epi8(0xF);
  6330. const __m256i m2 = _mm256_set1_epi8(3);
  6331. const __m256i m32s = _mm256_set1_epi8(32);
  6332. __m256 acc = _mm256_setzero_ps();
  6333. for (int i = 0; i < nb; ++i) {
  6334. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6335. const uint8_t * restrict q4 = x[i].ql;
  6336. const uint8_t * restrict qh = x[i].qh;
  6337. const int8_t * restrict q8 = y[i].qs;
  6338. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6339. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6340. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6341. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6342. __m256i sumi = _mm256_setzero_si256();
  6343. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6344. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6345. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6346. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6347. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  6348. 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);
  6349. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  6350. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  6351. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6352. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6353. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  6354. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  6355. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  6356. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  6357. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  6358. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  6359. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  6360. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  6361. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  6362. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  6363. }
  6364. *s = hsum_float_8(acc);
  6365. #elif defined __AVX__
  6366. const __m128i m4 = _mm_set1_epi8(0xF);
  6367. const __m128i m2 = _mm_set1_epi8(3);
  6368. const __m128i m32s = _mm_set1_epi8(32);
  6369. __m256 acc = _mm256_setzero_ps();
  6370. for (int i = 0; i < nb; ++i) {
  6371. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  6372. const uint8_t * restrict q4 = x[i].ql;
  6373. const uint8_t * restrict qh = x[i].qh;
  6374. const int8_t * restrict q8 = y[i].qs;
  6375. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  6376. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  6377. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  6378. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  6379. __m128i sumi_0 = _mm_setzero_si128();
  6380. __m128i sumi_1 = _mm_setzero_si128();
  6381. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  6382. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  6383. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  6384. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  6385. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  6386. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  6387. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  6388. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  6389. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  6390. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  6391. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  6392. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  6393. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  6394. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  6395. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  6396. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  6397. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  6398. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  6399. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  6400. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  6401. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  6402. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  6403. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  6404. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  6405. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  6406. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  6407. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  6408. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  6409. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  6410. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  6411. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  6412. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  6413. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  6414. }
  6415. *s = hsum_float_8(acc);
  6416. #elif defined __riscv_v_intrinsic
  6417. float sumf = 0;
  6418. for (int i = 0; i < nb; ++i) {
  6419. const float d_all = (float)x[i].d;
  6420. const uint8_t * restrict q6 = x[i].ql;
  6421. const uint8_t * restrict qh = x[i].qh;
  6422. const int8_t * restrict q8 = y[i].qs;
  6423. const int8_t * restrict scale = x[i].scales;
  6424. int32_t isum = 0;
  6425. size_t vl = 16;
  6426. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  6427. // load Q6
  6428. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  6429. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  6430. // load qh
  6431. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  6432. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6433. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6434. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6435. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6436. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6437. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  6438. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  6439. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  6440. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  6441. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  6442. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  6443. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  6444. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  6445. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  6446. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  6447. // load Q8 and take product
  6448. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  6449. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  6450. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  6451. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  6452. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  6453. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  6454. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  6455. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  6456. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  6457. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  6458. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  6459. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  6460. sumf += isum * d_all * y[i].d;
  6461. }
  6462. *s = sumf;
  6463. #else
  6464. int8_t aux8[QK_K];
  6465. int16_t aux16[8];
  6466. float sums [8];
  6467. int32_t aux32[8];
  6468. memset(sums, 0, 8*sizeof(float));
  6469. float sumf = 0;
  6470. for (int i = 0; i < nb; ++i) {
  6471. const uint8_t * restrict q4 = x[i].ql;
  6472. const uint8_t * restrict qh = x[i].qh;
  6473. const int8_t * restrict q8 = y[i].qs;
  6474. memset(aux32, 0, 8*sizeof(int32_t));
  6475. int8_t * restrict a = aux8;
  6476. for (int l = 0; l < 16; ++l) {
  6477. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  6478. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  6479. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  6480. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  6481. }
  6482. int is = 0;
  6483. for (int j = 0; j < QK_K/16; ++j) {
  6484. int scale = x[i].scales[is++];
  6485. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6486. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6487. q8 += 8; a += 8;
  6488. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  6489. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  6490. q8 += 8; a += 8;
  6491. }
  6492. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6493. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  6494. }
  6495. for (int l = 0; l < 8; ++l) sumf += sums[l];
  6496. *s = sumf;
  6497. #endif
  6498. }
  6499. #endif
  6500. static const int8_t keven_signs_q2xs[1024] = {
  6501. 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, 1,
  6502. 1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, -1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, -1,
  6503. 1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, -1,
  6504. 1, 1, -1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, 1,
  6505. 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, -1,
  6506. 1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, 1,
  6507. 1, 1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, 1,
  6508. 1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, 1, 1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, -1,
  6509. 1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1, 1, -1,
  6510. 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, 1,
  6511. 1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, 1,
  6512. 1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, -1,
  6513. 1, 1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, 1,
  6514. 1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, -1,
  6515. 1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, 1, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, -1,
  6516. 1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, 1,
  6517. 1, 1, 1, 1, 1, 1, -1, -1, -1, 1, 1, 1, 1, 1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, -1, -1, 1, 1, 1, 1, -1, -1,
  6518. 1, 1, -1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, 1,
  6519. 1, 1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1,
  6520. 1, 1, -1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, -1,
  6521. 1, 1, 1, 1, -1, 1, -1, 1, -1, 1, 1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, 1,
  6522. 1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, 1, 1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, -1,
  6523. 1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, -1,
  6524. 1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, 1,
  6525. 1, 1, 1, 1, 1, -1, -1, 1, -1, 1, 1, 1, 1, -1, -1, -1, 1, -1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, 1,
  6526. 1, 1, -1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, -1, -1, -1, 1, 1, -1, -1, -1,
  6527. 1, 1, 1, -1, 1, -1, -1, -1, -1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, -1,
  6528. 1, 1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, 1,
  6529. 1, 1, 1, 1, -1, -1, -1, -1, -1, 1, 1, 1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, -1, -1,
  6530. 1, 1, -1, 1, -1, -1, -1, 1, -1, 1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, 1,
  6531. 1, 1, 1, -1, -1, -1, -1, 1, -1, 1, 1, -1, -1, -1, -1, -1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1,
  6532. 1, 1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1,
  6533. };
  6534. void ggml_vec_dot_iq2_xxs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6535. assert(n % QK_K == 0);
  6536. const block_iq2_xxs * restrict x = vx;
  6537. const block_q8_K * restrict y = vy;
  6538. const int nb = n / QK_K;
  6539. #if defined(__ARM_NEON)
  6540. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6541. uint32_t aux32[4];
  6542. const uint8_t * aux8 = (const uint8_t *)aux32;
  6543. ggml_int8x16x4_t q2u;
  6544. ggml_int8x16x4_t q2s;
  6545. ggml_int8x16x4_t q8b;
  6546. float sumf = 0;
  6547. for (int i = 0; i < nb; ++i) {
  6548. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6549. const uint16_t * restrict q2 = x[i].qs;
  6550. const int8_t * restrict q8 = y[i].qs;
  6551. float sumf1 = 0, sumf2 = 0;
  6552. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6553. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6554. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6555. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 0])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 1])));
  6556. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 2])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 3])));
  6557. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[ 8])), vld1_s8((const void *)(iq2xxs_grid + aux8[ 9])));
  6558. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xxs_grid + aux8[10])), vld1_s8((const void *)(iq2xxs_grid + aux8[11])));
  6559. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 7) & 127))));
  6560. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[1] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[1] >> 21) & 127))));
  6561. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 0) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 7) & 127))));
  6562. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + ((aux32[3] >> 14) & 127))), vld1_s8((const void *)(signs64 + ((aux32[3] >> 21) & 127))));
  6563. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6564. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6565. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6566. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6567. 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]);
  6568. 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]);
  6569. sumf1 += vaddvq_s32(p1) * (0.5f + (aux32[1] >> 28));
  6570. sumf2 += vaddvq_s32(p2) * (0.5f + (aux32[3] >> 28));
  6571. }
  6572. sumf += d*(sumf1 + sumf2);
  6573. }
  6574. *s = 0.25f * sumf;
  6575. #elif defined(__AVX2__)
  6576. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6577. uint32_t aux32[4];
  6578. const uint8_t * aux8 = (const uint8_t *)aux32;
  6579. __m256 accumf = _mm256_setzero_ps();
  6580. for (int i = 0; i < nb; ++i) {
  6581. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6582. const uint16_t * restrict q2 = x[i].qs;
  6583. const int8_t * restrict q8 = y[i].qs;
  6584. __m256i sumi1 = _mm256_setzero_si256();
  6585. __m256i sumi2 = _mm256_setzero_si256();
  6586. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6587. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6588. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6589. memcpy(aux32, q2, 4*sizeof(uint32_t)); q2 += 8;
  6590. const __m256i q2_1 = _mm256_set_epi64x(iq2xxs_grid[aux8[ 3]], iq2xxs_grid[aux8[ 2]], iq2xxs_grid[aux8[1]], iq2xxs_grid[aux8[0]]);
  6591. const __m256i q2_2 = _mm256_set_epi64x(iq2xxs_grid[aux8[11]], iq2xxs_grid[aux8[10]], iq2xxs_grid[aux8[9]], iq2xxs_grid[aux8[8]]);
  6592. const __m256i s2_1 = _mm256_set_epi64x(signs64[(aux32[1] >> 21) & 127], signs64[(aux32[1] >> 14) & 127],
  6593. signs64[(aux32[1] >> 7) & 127], signs64[(aux32[1] >> 0) & 127]);
  6594. const __m256i s2_2 = _mm256_set_epi64x(signs64[(aux32[3] >> 21) & 127], signs64[(aux32[3] >> 14) & 127],
  6595. signs64[(aux32[3] >> 7) & 127], signs64[(aux32[3] >> 0) & 127]);
  6596. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6597. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6598. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6599. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6600. const uint16_t ls1 = aux32[1] >> 28;
  6601. const uint16_t ls2 = aux32[3] >> 28;
  6602. const __m256i p1 = _mm256_madd_epi16(dot1, _mm256_set1_epi16(2*ls1+1));
  6603. const __m256i p2 = _mm256_madd_epi16(dot2, _mm256_set1_epi16(2*ls2+1));
  6604. sumi1 = _mm256_add_epi32(sumi1, p1);
  6605. sumi2 = _mm256_add_epi32(sumi2, p2);
  6606. }
  6607. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6608. }
  6609. *s = 0.125f * hsum_float_8(accumf);
  6610. #else
  6611. uint32_t aux32[2];
  6612. const uint8_t * aux8 = (const uint8_t *)aux32;
  6613. float sumf = 0.f;
  6614. for (int i = 0; i < nb; ++i) {
  6615. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6616. const uint16_t * restrict q2 = x[i].qs;
  6617. const int8_t * restrict q8 = y[i].qs;
  6618. int32_t bsum = 0;
  6619. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6620. memcpy(aux32, q2, 2*sizeof(uint32_t));
  6621. q2 += 4;
  6622. const uint32_t ls = 2*(aux32[1] >> 28) + 1;
  6623. int32_t sumi = 0;
  6624. for (int l = 0; l < 4; ++l) {
  6625. const uint8_t * grid = (const uint8_t *)(iq2xxs_grid + aux8[l]);
  6626. const uint8_t signs = ksigns_iq2xs[(aux32[1] >> 7*l) & 127];
  6627. for (int j = 0; j < 8; ++j) {
  6628. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6629. }
  6630. q8 += 8;
  6631. }
  6632. bsum += sumi * ls;
  6633. }
  6634. sumf += d * bsum;
  6635. }
  6636. *s = 0.125f * sumf;
  6637. #endif
  6638. }
  6639. void ggml_vec_dot_iq2_xs_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  6640. assert(n % QK_K == 0);
  6641. const block_iq2_xs * restrict x = vx;
  6642. const block_q8_K * restrict y = vy;
  6643. const int nb = n / QK_K;
  6644. #if defined(__ARM_NEON)
  6645. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6646. ggml_int8x16x4_t q2u;
  6647. ggml_int8x16x4_t q2s;
  6648. ggml_int8x16x4_t q8b;
  6649. int32x4x4_t scales32;
  6650. float sumf = 0;
  6651. for (int i = 0; i < nb; ++i) {
  6652. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6653. const uint16_t * restrict q2 = x[i].qs;
  6654. const int8_t * restrict q8 = y[i].qs;
  6655. const uint8x8_t scales8 = vld1_u8(x[i].scales);
  6656. const uint8x8_t scales_l = vand_u8(scales8, vdup_n_u8(0xf));
  6657. const uint8x8_t scales_h = vshr_n_u8(scales8, 4);
  6658. uint8x16_t scales = vcombine_u8(vzip1_u8(scales_l, scales_h), vzip2_u8(scales_l, scales_h));
  6659. scales = vaddq_u8(vshlq_n_u8(scales, 1), vdupq_n_u8(1));
  6660. const uint16x8_t scales1 = vmovl_u8(vget_low_u8(scales));
  6661. const uint16x8_t scales2 = vmovl_u8(vget_high_u8(scales));
  6662. scales32.val[0] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales1)));
  6663. scales32.val[1] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales1)));
  6664. scales32.val[2] = vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(scales2)));
  6665. scales32.val[3] = vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(scales2)));
  6666. int32x4_t sumi = vdupq_n_s32(0);
  6667. for (int ib64 = 0; ib64 < QK_K/64; ++ib64) {
  6668. q8b = ggml_vld1q_s8_x4(q8); q8 += 64;
  6669. q2u.val[0] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[0] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[1] & 511))));
  6670. q2u.val[1] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[2] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[3] & 511))));
  6671. q2u.val[2] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[4] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[5] & 511))));
  6672. q2u.val[3] = vcombine_s8(vld1_s8((const void *)(iq2xs_grid + (q2[6] & 511))), vld1_s8((const void *)(iq2xs_grid + (q2[7] & 511))));
  6673. q2s.val[0] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[0] >> 9))), vld1_s8((const void *)(signs64 + (q2[1] >> 9))));
  6674. q2s.val[1] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[2] >> 9))), vld1_s8((const void *)(signs64 + (q2[3] >> 9))));
  6675. q2s.val[2] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[4] >> 9))), vld1_s8((const void *)(signs64 + (q2[5] >> 9))));
  6676. q2s.val[3] = vcombine_s8(vld1_s8((const void *)(signs64 + (q2[6] >> 9))), vld1_s8((const void *)(signs64 + (q2[7] >> 9))));
  6677. q2u.val[0] = vmulq_s8(q2u.val[0], q2s.val[0]);
  6678. q2u.val[1] = vmulq_s8(q2u.val[1], q2s.val[1]);
  6679. q2u.val[2] = vmulq_s8(q2u.val[2], q2s.val[2]);
  6680. q2u.val[3] = vmulq_s8(q2u.val[3], q2s.val[3]);
  6681. const int32x4_t p1 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[0], q8b.val[0]);
  6682. const int32x4_t p2 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[1], q8b.val[1]);
  6683. const int32x4_t p3 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[2], q8b.val[2]);
  6684. const int32x4_t p4 = ggml_vdotq_s32(vdupq_n_s32(0), q2u.val[3], q8b.val[3]);
  6685. const int32x4_t p = vpaddq_s32(vpaddq_s32(p1, p2), vpaddq_s32(p3, p4));
  6686. sumi = vmlaq_s32(sumi, p, scales32.val[ib64]);
  6687. q2 += 8;
  6688. }
  6689. sumf += d*vaddvq_s32(sumi);
  6690. }
  6691. *s = 0.125f * sumf;
  6692. #elif defined(__AVX2__)
  6693. const __m128i m4 = _mm_set1_epi8(0xf);
  6694. const __m128i m1 = _mm_set1_epi8(1);
  6695. const __m128i m511 = _mm_set1_epi16(511);
  6696. const __m128i m127 = _mm_set1_epi16(127);
  6697. const uint64_t * signs64 = (const uint64_t *)keven_signs_q2xs;
  6698. uint64_t aux64;
  6699. // somewhat hacky, but gives a significant boost in performance
  6700. __m128i aux_gindex, aux_sindex;
  6701. const uint16_t * gindex = (const uint16_t *)&aux_gindex;
  6702. const uint16_t * sindex = (const uint16_t *)&aux_sindex;
  6703. __m256 accumf = _mm256_setzero_ps();
  6704. for (int i = 0; i < nb; ++i) {
  6705. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6706. const uint16_t * restrict q2 = x[i].qs;
  6707. const int8_t * restrict q8 = y[i].qs;
  6708. memcpy(&aux64, x[i].scales, 8);
  6709. __m128i stmp = _mm_set1_epi64x(aux64);
  6710. stmp = _mm_unpacklo_epi8(_mm_and_si128(stmp, m4), _mm_and_si128(_mm_srli_epi16(stmp, 4), m4));
  6711. const __m128i scales = _mm_add_epi8(_mm_slli_epi16(stmp, 1), m1);
  6712. __m256i sumi1 = _mm256_setzero_si256();
  6713. __m256i sumi2 = _mm256_setzero_si256();
  6714. for (int ib32 = 0; ib32 < QK_K/32; ib32 += 2) {
  6715. const __m256i q8_1 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6716. const __m256i q8_2 = _mm256_loadu_si256((const __m256i *)q8); q8 += 32;
  6717. const __m128i q2_data = _mm_loadu_si128((const __m128i*)q2); q2 += 8;
  6718. aux_gindex = _mm_and_si128(q2_data, m511);
  6719. aux_sindex = _mm_and_si128(_mm_srli_epi16(q2_data, 9), m127);
  6720. const __m256i q2_1 = _mm256_set_epi64x(iq2xs_grid[gindex[3]], iq2xs_grid[gindex[2]], iq2xs_grid[gindex[1]], iq2xs_grid[gindex[0]]);
  6721. const __m256i q2_2 = _mm256_set_epi64x(iq2xs_grid[gindex[7]], iq2xs_grid[gindex[6]], iq2xs_grid[gindex[5]], iq2xs_grid[gindex[4]]);
  6722. const __m256i s2_1 = _mm256_set_epi64x(signs64[sindex[3]], signs64[sindex[2]], signs64[sindex[1]], signs64[sindex[0]]);
  6723. const __m256i s2_2 = _mm256_set_epi64x(signs64[sindex[7]], signs64[sindex[6]], signs64[sindex[5]], signs64[sindex[4]]);
  6724. const __m256i q8s_1 = _mm256_sign_epi8(q8_1, s2_1);
  6725. const __m256i q8s_2 = _mm256_sign_epi8(q8_2, s2_2);
  6726. const __m256i dot1 = _mm256_maddubs_epi16(q2_1, q8s_1);
  6727. const __m256i dot2 = _mm256_maddubs_epi16(q2_2, q8s_2);
  6728. const __m256i sc1 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+0)));
  6729. const __m256i sc2 = _mm256_cvtepi8_epi16(_mm_shuffle_epi8(scales, get_scale_shuffle(ib32+1)));
  6730. sumi1 = _mm256_add_epi32(sumi1, _mm256_madd_epi16(dot1, sc1));
  6731. sumi2 = _mm256_add_epi32(sumi2, _mm256_madd_epi16(dot2, sc2));
  6732. }
  6733. accumf = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(sumi1, sumi2)), accumf);
  6734. }
  6735. *s = 0.125f * hsum_float_8(accumf);
  6736. #else
  6737. float sumf = 0.f;
  6738. for (int i = 0; i < nb; ++i) {
  6739. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  6740. const uint16_t * restrict q2 = x[i].qs;
  6741. const uint8_t * restrict sc = x[i].scales;
  6742. const int8_t * restrict q8 = y[i].qs;
  6743. int32_t bsum = 0;
  6744. for (int ib32 = 0; ib32 < QK_K/32; ++ib32) {
  6745. const uint16_t ls1 = 2*(sc[ib32] & 0xf) + 1;
  6746. const uint16_t ls2 = 2*(sc[ib32] >> 4) + 1;
  6747. int32_t sumi = 0;
  6748. for (int l = 0; l < 2; ++l) {
  6749. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6750. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6751. for (int j = 0; j < 8; ++j) {
  6752. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6753. }
  6754. q8 += 8;
  6755. }
  6756. bsum += sumi * ls1;
  6757. sumi = 0;
  6758. for (int l = 2; l < 4; ++l) {
  6759. const uint8_t * grid = (const uint8_t *)(iq2xs_grid + (q2[l] & 511));
  6760. const uint8_t signs = ksigns_iq2xs[q2[l] >> 9];
  6761. for (int j = 0; j < 8; ++j) {
  6762. sumi += grid[j] * q8[j] * (signs & kmask_iq2xs[j] ? -1 : 1);
  6763. }
  6764. q8 += 8;
  6765. }
  6766. bsum += sumi * ls2;
  6767. q2 += 4;
  6768. }
  6769. sumf += d * bsum;
  6770. }
  6771. *s = 0.125f * sumf;
  6772. #endif
  6773. }
  6774. // ================================ IQ2 quantization =============================================
  6775. typedef struct {
  6776. uint64_t * grid;
  6777. int * map;
  6778. uint16_t * neighbours;
  6779. } iq2_entry_t;
  6780. static iq2_entry_t iq2_data[2] = {
  6781. {NULL, NULL, NULL},
  6782. {NULL, NULL, NULL},
  6783. };
  6784. static inline int iq2_data_index(int grid_size) {
  6785. GGML_ASSERT(grid_size == 256 || grid_size == 512);
  6786. return grid_size == 256 ? 0 : 1;
  6787. }
  6788. static int iq2_compare_func(const void * left, const void * right) {
  6789. const int * l = (const int *)left;
  6790. const int * r = (const int *)right;
  6791. return l[0] < r[0] ? -1 : l[0] > r[0] ? 1 : l[1] < r[1] ? -1 : l[1] > r[1] ? 1 : 0;
  6792. }
  6793. void iq2xs_init_impl(int grid_size) {
  6794. const int gindex = iq2_data_index(grid_size);
  6795. if (iq2_data[gindex].grid) {
  6796. return;
  6797. }
  6798. static const uint16_t kgrid_256[256] = {
  6799. 0, 2, 5, 8, 10, 17, 20, 32, 34, 40, 42, 65, 68, 80, 88, 97,
  6800. 100, 128, 130, 138, 162, 257, 260, 272, 277, 320, 388, 408, 512, 514, 546, 642,
  6801. 1025, 1028, 1040, 1057, 1060, 1088, 1090, 1096, 1120, 1153, 1156, 1168, 1188, 1280, 1282, 1288,
  6802. 1312, 1350, 1385, 1408, 1425, 1545, 1552, 1600, 1668, 1700, 2048, 2053, 2056, 2068, 2088, 2113,
  6803. 2116, 2128, 2130, 2184, 2308, 2368, 2562, 2580, 4097, 4100, 4112, 4129, 4160, 4192, 4228, 4240,
  6804. 4245, 4352, 4360, 4384, 4432, 4442, 4480, 4644, 4677, 5120, 5128, 5152, 5157, 5193, 5248, 5400,
  6805. 5474, 5632, 5654, 6145, 6148, 6160, 6208, 6273, 6400, 6405, 6560, 6737, 8192, 8194, 8202, 8260,
  6806. 8289, 8320, 8322, 8489, 8520, 8704, 8706, 9217, 9220, 9232, 9280, 9302, 9472, 9537, 9572, 9872,
  6807. 10248, 10272, 10388, 10820, 16385, 16388, 16400, 16408, 16417, 16420, 16448, 16456, 16470, 16480, 16513, 16516,
  6808. 16528, 16640, 16672, 16737, 16768, 16773, 16897, 16912, 16968, 16982, 17000, 17408, 17416, 17440, 17536, 17561,
  6809. 17682, 17700, 17920, 18433, 18436, 18448, 18496, 18501, 18688, 18776, 18785, 18818, 19013, 19088, 20480, 20488,
  6810. 20497, 20505, 20512, 20608, 20616, 20740, 20802, 20900, 21137, 21648, 21650, 21770, 22017, 22100, 22528, 22545,
  6811. 22553, 22628, 22848, 23048, 24580, 24592, 24640, 24680, 24832, 24917, 25112, 25184, 25600, 25605, 25872, 25874,
  6812. 25988, 26690, 32768, 32770, 32778, 32833, 32898, 33028, 33048, 33088, 33297, 33793, 33796, 33808, 33813, 33856,
  6813. 33888, 34048, 34118, 34196, 34313, 34368, 34400, 34818, 35076, 35345, 36868, 36880, 36900, 36928, 37025, 37142,
  6814. 37248, 37445, 37888, 37922, 37956, 38225, 39041, 39200, 40962, 41040, 41093, 41225, 41472, 42008, 43088, 43268,
  6815. };
  6816. static const uint16_t kgrid_512[512] = {
  6817. 0, 2, 5, 8, 10, 17, 20, 22, 25, 32, 34, 37, 40, 65, 68, 70,
  6818. 73, 80, 82, 85, 88, 97, 100, 128, 130, 133, 136, 145, 148, 153, 160, 257,
  6819. 260, 262, 265, 272, 274, 277, 280, 282, 289, 292, 320, 322, 325, 328, 337, 340,
  6820. 352, 360, 385, 388, 400, 512, 514, 517, 520, 529, 532, 544, 577, 580, 592, 597,
  6821. 640, 650, 1025, 1028, 1030, 1033, 1040, 1042, 1045, 1048, 1057, 1060, 1088, 1090, 1093, 1096,
  6822. 1105, 1108, 1110, 1120, 1153, 1156, 1168, 1280, 1282, 1285, 1288, 1297, 1300, 1312, 1345, 1348,
  6823. 1360, 1377, 1408, 1537, 1540, 1552, 1574, 1600, 1602, 1668, 2048, 2050, 2053, 2056, 2058, 2065,
  6824. 2068, 2080, 2085, 2113, 2116, 2128, 2136, 2176, 2208, 2218, 2305, 2308, 2320, 2368, 2433, 2441,
  6825. 2560, 2592, 2600, 2710, 2720, 4097, 4100, 4102, 4105, 4112, 4114, 4117, 4120, 4129, 4132, 4160,
  6826. 4162, 4165, 4168, 4177, 4180, 4192, 4202, 4225, 4228, 4240, 4352, 4354, 4357, 4360, 4369, 4372,
  6827. 4384, 4417, 4420, 4432, 4480, 4500, 4502, 4609, 4612, 4614, 4624, 4672, 4704, 5120, 5122, 5125,
  6828. 5128, 5137, 5140, 5152, 5185, 5188, 5193, 5200, 5220, 5248, 5377, 5380, 5392, 5440, 5632, 5652,
  6829. 5705, 6145, 6148, 6160, 6162, 6208, 6228, 6278, 6400, 6405, 6502, 6737, 6825, 8192, 8194, 8197,
  6830. 8200, 8202, 8209, 8212, 8224, 8257, 8260, 8272, 8320, 8352, 8449, 8452, 8464, 8512, 8520, 8549,
  6831. 8704, 8738, 8832, 8872, 9217, 9220, 9232, 9257, 9280, 9472, 9537, 9554, 9625, 9729, 9754, 9894,
  6832. 10240, 10248, 10250, 10272, 10325, 10376, 10402, 10600, 10640, 10760, 10784, 10882, 10888, 10890, 16385, 16388,
  6833. 16390, 16393, 16400, 16402, 16405, 16408, 16417, 16420, 16448, 16450, 16453, 16456, 16458, 16465, 16468, 16480,
  6834. 16485, 16513, 16516, 16528, 16640, 16642, 16645, 16648, 16657, 16660, 16672, 16705, 16708, 16720, 16768, 16773,
  6835. 16802, 16897, 16900, 16912, 16914, 16937, 16960, 17408, 17410, 17413, 17416, 17425, 17428, 17433, 17440, 17473,
  6836. 17476, 17488, 17536, 17556, 17665, 17668, 17680, 17700, 17728, 17818, 17920, 17930, 17988, 18000, 18433, 18436,
  6837. 18448, 18496, 18501, 18516, 18530, 18688, 18705, 18756, 18768, 18793, 18948, 20480, 20482, 20485, 20488, 20497,
  6838. 20500, 20512, 20520, 20545, 20548, 20560, 20608, 20737, 20740, 20752, 20757, 20800, 20802, 20992, 21060, 21162,
  6839. 21505, 21508, 21520, 21537, 21568, 21600, 21633, 21665, 21760, 21768, 21888, 21896, 22049, 22120, 22177, 22528,
  6840. 22548, 22593, 22608, 22681, 22810, 22848, 22850, 23173, 24577, 24580, 24592, 24640, 24660, 24674, 24710, 24745,
  6841. 24832, 25124, 25162, 25234, 25600, 25622, 25872, 25920, 25925, 26020, 26625, 26730, 26917, 27142, 27220, 27234,
  6842. 32768, 32770, 32773, 32776, 32785, 32788, 32800, 32810, 32833, 32836, 32848, 32896, 32898, 32936, 32938, 33025,
  6843. 33028, 33030, 33040, 33088, 33105, 33113, 33280, 33312, 33408, 33410, 33440, 33448, 33793, 33796, 33808, 33810,
  6844. 33813, 33856, 33888, 33929, 34048, 34116, 34213, 34328, 34410, 34816, 34824, 34853, 34906, 34944, 34946, 34984,
  6845. 35078, 35362, 35456, 35464, 35478, 35496, 36865, 36868, 36880, 36928, 36950, 36996, 37120, 37154, 37220, 37462,
  6846. 37513, 37888, 37893, 37956, 37968, 37976, 38185, 38288, 38290, 38465, 38993, 39078, 39241, 39445, 39520, 40960,
  6847. 40962, 40968, 40970, 40992, 41002, 41120, 41297, 41305, 41382, 41472, 41474, 41480, 41514, 41600, 41632, 42048,
  6848. 42133, 42597, 42648, 43018, 43040, 43042, 43048, 43168, 43176, 43268, 43396, 43398, 43560, 43562, 43665, 43690,
  6849. };
  6850. const int kmap_size = 43692;
  6851. const int nwant = 2;
  6852. const uint16_t * kgrid = grid_size == 256 ? kgrid_256 : kgrid_512;
  6853. uint64_t * kgrid_q2xs;
  6854. int * kmap_q2xs;
  6855. uint16_t * kneighbors_q2xs;
  6856. printf("================================================================= %s(grid_size = %d)\n", __func__, grid_size);
  6857. uint64_t * the_grid = (uint64_t *)malloc(grid_size*sizeof(uint64_t));
  6858. for (int k = 0; k < grid_size; ++k) {
  6859. int8_t * pos = (int8_t *)(the_grid + k);
  6860. for (int i = 0; i < 8; ++i) {
  6861. int l = (kgrid[k] >> 2*i) & 0x3;
  6862. pos[i] = 2*l + 1;
  6863. }
  6864. }
  6865. kgrid_q2xs = the_grid;
  6866. iq2_data[gindex].grid = the_grid;
  6867. kmap_q2xs = (int *)malloc(kmap_size*sizeof(int));
  6868. iq2_data[gindex].map = kmap_q2xs;
  6869. for (int i = 0; i < kmap_size; ++i) kmap_q2xs[i] = -1;
  6870. uint64_t aux64;
  6871. uint8_t * aux8 = (uint8_t *)&aux64;
  6872. for (int i = 0; i < grid_size; ++i) {
  6873. aux64 = kgrid_q2xs[i];
  6874. uint16_t index = 0;
  6875. for (int k=0; k<8; ++k) {
  6876. uint16_t q = (aux8[k] - 1)/2;
  6877. index |= (q << 2*k);
  6878. }
  6879. kmap_q2xs[index] = i;
  6880. }
  6881. int8_t pos[8];
  6882. int * dist2 = (int *)malloc(2*grid_size*sizeof(int));
  6883. int num_neighbors = 0, num_not_in_map = 0;
  6884. for (int i = 0; i < kmap_size; ++i) {
  6885. if (kmap_q2xs[i] >= 0) continue;
  6886. ++num_not_in_map;
  6887. for (int k = 0; k < 8; ++k) {
  6888. int l = (i >> 2*k) & 0x3;
  6889. pos[k] = 2*l + 1;
  6890. }
  6891. for (int j = 0; j < grid_size; ++j) {
  6892. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  6893. int d2 = 0;
  6894. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  6895. dist2[2*j+0] = d2;
  6896. dist2[2*j+1] = j;
  6897. }
  6898. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  6899. int n = 0; int d2 = dist2[0];
  6900. int nhave = 1;
  6901. for (int j = 0; j < grid_size; ++j) {
  6902. if (dist2[2*j] > d2) {
  6903. if (nhave == nwant) break;
  6904. d2 = dist2[2*j];
  6905. ++nhave;
  6906. }
  6907. ++n;
  6908. }
  6909. num_neighbors += n;
  6910. }
  6911. printf("%s: %d neighbours in total\n", __func__, num_neighbors);
  6912. kneighbors_q2xs = (uint16_t *)malloc((num_neighbors + num_not_in_map)*sizeof(uint16_t));
  6913. iq2_data[gindex].neighbours = kneighbors_q2xs;
  6914. int counter = 0;
  6915. for (int i = 0; i < kmap_size; ++i) {
  6916. if (kmap_q2xs[i] >= 0) continue;
  6917. for (int k = 0; k < 8; ++k) {
  6918. int l = (i >> 2*k) & 0x3;
  6919. pos[k] = 2*l + 1;
  6920. }
  6921. for (int j = 0; j < grid_size; ++j) {
  6922. const int8_t * pg = (const int8_t *)(kgrid_q2xs + j);
  6923. int d2 = 0;
  6924. for (int k = 0; k < 8; ++k) d2 += (pg[k] - pos[k])*(pg[k] - pos[k]);
  6925. dist2[2*j+0] = d2;
  6926. dist2[2*j+1] = j;
  6927. }
  6928. qsort(dist2, grid_size, 2*sizeof(int), iq2_compare_func);
  6929. kmap_q2xs[i] = -(counter + 1);
  6930. int d2 = dist2[0];
  6931. uint16_t * start = &kneighbors_q2xs[counter++];
  6932. int n = 0, nhave = 1;
  6933. for (int j = 0; j < grid_size; ++j) {
  6934. if (dist2[2*j] > d2) {
  6935. if (nhave == nwant) break;
  6936. d2 = dist2[2*j];
  6937. ++nhave;
  6938. }
  6939. kneighbors_q2xs[counter++] = dist2[2*j+1];
  6940. ++n;
  6941. }
  6942. *start = n;
  6943. }
  6944. free(dist2);
  6945. }
  6946. void iq2xs_free_impl(int grid_size) {
  6947. GGML_ASSERT(grid_size == 256 || grid_size == 512 || grid_size == 1024);
  6948. const int gindex = iq2_data_index(grid_size);
  6949. if (iq2_data[gindex].grid) {
  6950. free(iq2_data[gindex].grid); iq2_data[gindex].grid = NULL;
  6951. free(iq2_data[gindex].map); iq2_data[gindex].map = NULL;
  6952. free(iq2_data[gindex].neighbours); iq2_data[gindex].neighbours = NULL;
  6953. }
  6954. }
  6955. static int iq2_find_best_neighbour(const uint16_t * restrict neighbours, const uint64_t * restrict grid,
  6956. const float * restrict xval, const float * restrict weight, float scale, int8_t * restrict L) {
  6957. int num_neighbors = neighbours[0];
  6958. GGML_ASSERT(num_neighbors > 0);
  6959. float best_d2 = FLT_MAX;
  6960. int grid_index = -1;
  6961. for (int j = 1; j <= num_neighbors; ++j) {
  6962. const int8_t * pg = (const int8_t *)(grid + neighbours[j]);
  6963. float d2 = 0;
  6964. for (int i = 0; i < 8; ++i) {
  6965. float q = pg[i];
  6966. float diff = scale*q - xval[i];
  6967. d2 += weight[i]*diff*diff;
  6968. }
  6969. if (d2 < best_d2) {
  6970. best_d2 = d2; grid_index = neighbours[j];
  6971. }
  6972. }
  6973. GGML_ASSERT(grid_index >= 0);
  6974. const int8_t * pg = (const int8_t *)(grid + grid_index);
  6975. for (int i = 0; i < 8; ++i) L[i] = (pg[i] - 1)/2;
  6976. return grid_index;
  6977. }
  6978. static void quantize_row_iq2_xxs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  6979. const int gindex = iq2_data_index(256);
  6980. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  6981. const int * kmap_q2xs = iq2_data[gindex].map;
  6982. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  6983. GGML_ASSERT(quant_weights && "missing quantization weights");
  6984. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  6985. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  6986. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  6987. GGML_ASSERT(n%QK_K == 0);
  6988. const int kMaxQ = 3;
  6989. const int nbl = n/256;
  6990. block_iq2_xxs * y = vy;
  6991. float scales[QK_K/32];
  6992. float weight[32];
  6993. float xval[32];
  6994. int8_t L[32];
  6995. int8_t Laux[32];
  6996. float waux[32];
  6997. bool is_on_grid[4];
  6998. bool is_on_grid_aux[4];
  6999. uint8_t block_signs[4];
  7000. uint32_t q2[2*(QK_K/32)];
  7001. for (int ibl = 0; ibl < nbl; ++ibl) {
  7002. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7003. memset(q2, 0, QK_K/4);
  7004. float max_scale = 0;
  7005. const float * xbl = x + QK_K*ibl;
  7006. float sumx2 = 0;
  7007. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7008. float sigma2 = sumx2/QK_K;
  7009. for (int ib = 0; ib < QK_K/32; ++ib) {
  7010. const float * xb = xbl + 32*ib;
  7011. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7012. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7013. for (int i = 0; i < 32; ++i) waux[i] = sqrtf(weight[i]);
  7014. for (int k = 0; k < 4; ++k) {
  7015. int nflip = 0;
  7016. uint8_t s = 0;
  7017. for (int i = 0; i < 8; ++i) {
  7018. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7019. else {
  7020. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7021. }
  7022. }
  7023. if (nflip%2) {
  7024. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7025. for (int i = 1; i < 8; ++i) {
  7026. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7027. if (ax < min) {
  7028. min = ax; imin = i;
  7029. }
  7030. }
  7031. xval[8*k+imin] = -xval[8*k+imin];
  7032. s ^= (1 << imin);
  7033. }
  7034. block_signs[k] = s & 127;
  7035. }
  7036. float max = xval[0];
  7037. for (int i = 1; i < 32; ++i) max = MAX(max, xval[i]);
  7038. if (!max) {
  7039. scales[ib] = 0;
  7040. memset(L, 0, 32);
  7041. continue;
  7042. }
  7043. float best = 0;
  7044. float scale = max/(2*kMaxQ-1);
  7045. for (int is = -9; is <= 9; ++is) {
  7046. float id = (2*kMaxQ-1+is*0.1f)/max;
  7047. float this_scale = 1/id;
  7048. for (int k = 0; k < 4; ++k) {
  7049. for (int i = 0; i < 8; ++i) {
  7050. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7051. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7052. }
  7053. uint16_t u = 0;
  7054. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7055. int grid_index = kmap_q2xs[u];
  7056. is_on_grid_aux[k] = true;
  7057. if (grid_index < 0) {
  7058. is_on_grid_aux[k] = false;
  7059. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7060. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7061. }
  7062. }
  7063. float sumqx = 0, sumq2 = 0;
  7064. for (int i = 0; i < 32; ++i) {
  7065. float w = weight[i];
  7066. float q = 2*Laux[i] + 1;
  7067. sumqx += w*xval[i]*q;
  7068. sumq2 += w*q*q;
  7069. }
  7070. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7071. scale = sumqx/sumq2; best = scale*sumqx;
  7072. for (int i = 0; i < 32; ++i) L[i] = Laux[i];
  7073. for (int k = 0; k < 4; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7074. }
  7075. }
  7076. int n_not_ongrid = 0;
  7077. for (int k = 0; k < 4; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7078. if (n_not_ongrid > 0 && scale > 0) {
  7079. float id = 1/scale;
  7080. for (int k = 0; k < 4; ++k) {
  7081. if (is_on_grid[k]) continue;
  7082. uint16_t u = 0;
  7083. for (int i = 0; i < 8; ++i) {
  7084. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7085. l = MAX(0, MIN(kMaxQ-1, l));
  7086. u |= (l << 2*i);
  7087. }
  7088. int grid_index = kmap_q2xs[u];
  7089. if (grid_index < 0) {
  7090. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7091. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7092. }
  7093. const int8_t * pg = (const int8_t *)(kgrid_q2xs + grid_index);
  7094. for (int i = 0; i < 8; ++i) L[8*k+i] = (pg[i] - 1)/2;
  7095. }
  7096. float sumqx = 0, sumq2 = 0;
  7097. for (int i = 0; i < 32; ++i) {
  7098. float w = weight[i];
  7099. float q = 2*L[i] + 1;
  7100. sumqx += w*xval[i]*q;
  7101. sumq2 += w*q*q;
  7102. }
  7103. if (sumq2 > 0) scale = sumqx/sumq2;
  7104. }
  7105. if (scale < 0) {
  7106. // This should never happen, but just in case, flip scale so that it is positive (we use uint's to encode the scale)
  7107. // and correspondingly flip quant signs.
  7108. scale = -scale;
  7109. for (int k = 0; k < 4; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7110. }
  7111. for (int k = 0; k < 4; ++k) {
  7112. uint16_t u = 0;
  7113. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7114. int grid_index = kmap_q2xs[u];
  7115. if (grid_index < 0) {
  7116. printf("Oops: found point %u not on grid:", u);
  7117. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7118. printf("\n");
  7119. GGML_ASSERT(false);
  7120. }
  7121. q2[2*ib+0] |= (grid_index << 8*k);
  7122. q2[2*ib+1] |= (block_signs[k] << 7*k);
  7123. }
  7124. GGML_ASSERT(scale >= 0);
  7125. scales[ib] = scale;
  7126. max_scale = MAX(max_scale, scale);
  7127. }
  7128. if (!max_scale) {
  7129. memset(y[ibl].qs, 0, QK_K/4);
  7130. continue;
  7131. }
  7132. float d = max_scale/31;
  7133. y[ibl].d = GGML_FP32_TO_FP16(d);
  7134. float id = 1/d;
  7135. float sumqx = 0, sumq2 = 0;
  7136. for (int ib = 0; ib < QK_K/32; ++ib) {
  7137. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7138. l = MAX(0, MIN(15, l));
  7139. q2[2*ib+1] |= ((uint32_t)l << 28);
  7140. const float * xb = xbl + 32*ib;
  7141. const float * qw = quant_weights + QK_K*ibl + 32*ib;
  7142. for (int i = 0; i < 32; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7143. const uint8_t * aux8 = (const uint8_t *)(q2 + 2*ib);
  7144. const float db = d * (1 + 2*l);
  7145. uint32_t u = 0;
  7146. for (int k = 0; k < 4; ++k) {
  7147. const int8_t * signs = keven_signs_q2xs + 8*((q2[2*ib+1] >> 7*k) & 127);
  7148. const float * xk = xb + 8*k;
  7149. const float * wk = weight + 8*k;
  7150. const uint8_t * grid = (const uint8_t *)(kgrid_q2xs + aux8[k]);
  7151. float best_mse = 0; int best_index = aux8[k];
  7152. for (int j = 0; j < 8; ++j) {
  7153. float diff = db * grid[j] * signs[j] - xk[j];
  7154. best_mse += wk[j] * diff * diff;
  7155. }
  7156. for (int idx = 0; idx < 256; ++idx) {
  7157. grid = (const uint8_t *)(kgrid_q2xs + idx);
  7158. float mse = 0;
  7159. for (int j = 0; j < 8; ++j) {
  7160. float diff = db * grid[j] * signs[j] - xk[j];
  7161. mse += wk[j] * diff * diff;
  7162. }
  7163. if (mse < best_mse) {
  7164. best_mse = mse; best_index = idx;
  7165. }
  7166. }
  7167. u |= (best_index << 8*k);
  7168. grid = (const uint8_t *)(kgrid_q2xs + best_index);
  7169. //grid = (const uint8_t *)(kgrid_q2xs + aux8[k]);
  7170. for (int j = 0; j < 8; ++j) {
  7171. float q = db * grid[j] * signs[j];
  7172. sumqx += wk[j] * q * xk[j];
  7173. sumq2 += wk[j] * q * q;
  7174. }
  7175. }
  7176. q2[2*ib] = u;
  7177. if (sumq2 > 0) y[ibl].d = GGML_FP32_TO_FP16(d*sumqx/sumq2);
  7178. }
  7179. memcpy(y[ibl].qs, q2, QK_K/4);
  7180. }
  7181. }
  7182. static void quantize_row_iq2_xs_impl(const float * restrict x, void * restrict vy, int n, const float * restrict quant_weights) {
  7183. const int gindex = iq2_data_index(512);
  7184. const uint64_t * kgrid_q2xs = iq2_data[gindex].grid;
  7185. const int * kmap_q2xs = iq2_data[gindex].map;
  7186. const uint16_t * kneighbors_q2xs = iq2_data[gindex].neighbours;
  7187. GGML_ASSERT(quant_weights && "missing quantization weights");
  7188. GGML_ASSERT(kmap_q2xs && "forgot to call ggml_quantize_init()?");
  7189. GGML_ASSERT(kgrid_q2xs && "forgot to call ggml_quantize_init()?");
  7190. GGML_ASSERT(kneighbors_q2xs && "forgot to call ggml_quantize_init()?");
  7191. GGML_ASSERT(n%QK_K == 0);
  7192. const int kMaxQ = 3;
  7193. const int nbl = n/256;
  7194. block_iq2_xs * y = vy;
  7195. float scales[QK_K/16];
  7196. float weight[16];
  7197. float xval[16];
  7198. int8_t L[16];
  7199. int8_t Laux[16];
  7200. float waux[16];
  7201. bool is_on_grid[2];
  7202. bool is_on_grid_aux[2];
  7203. uint8_t block_signs[2];
  7204. uint16_t q2[2*(QK_K/16)];
  7205. for (int ibl = 0; ibl < nbl; ++ibl) {
  7206. y[ibl].d = GGML_FP32_TO_FP16(0.f);
  7207. memset(q2, 0, QK_K/4);
  7208. memset(y[ibl].scales, 0, QK_K/32);
  7209. float max_scale = 0;
  7210. const float * xbl = x + QK_K*ibl;
  7211. float sumx2 = 0;
  7212. for (int i = 0; i < QK_K; ++i) sumx2 += xbl[i]*xbl[i];
  7213. float sigma2 = sumx2/QK_K;
  7214. for (int ib = 0; ib < QK_K/16; ++ib) {
  7215. const float * xb = xbl + 16*ib;
  7216. const float * qw = quant_weights + QK_K*ibl + 16*ib;
  7217. for (int i = 0; i < 16; ++i) weight[i] = qw[i] * sqrtf(sigma2 + xb[i]*xb[i]);
  7218. for (int i = 0; i < 16; ++i) waux[i] = sqrtf(weight[i]);
  7219. for (int k = 0; k < 2; ++k) {
  7220. int nflip = 0;
  7221. uint8_t s = 0;
  7222. for (int i = 0; i < 8; ++i) {
  7223. if (xb[8*k + i] >= 0) xval[8*k + i] = xb[8*k + i];
  7224. else {
  7225. xval[8*k + i] = -xb[8*k + i]; ++nflip; s |= (1 << i);
  7226. }
  7227. }
  7228. if (nflip%2) {
  7229. int imin = 0; float min = weight[8*k+imin]*xb[8*k+imin]*xb[8*k+imin];
  7230. for (int i = 1; i < 8; ++i) {
  7231. float ax = weight[8*k+i]*xb[8*k+i]*xb[8*k+i];
  7232. if (ax < min) {
  7233. min = ax; imin = i;
  7234. }
  7235. }
  7236. xval[8*k+imin] = -xval[8*k+imin];
  7237. s ^= (1 << imin);
  7238. }
  7239. block_signs[k] = s & 127;
  7240. }
  7241. float max = xval[0];
  7242. for (int i = 1; i < 16; ++i) max = MAX(max, xval[i]);
  7243. if (!max) {
  7244. scales[ib] = 0;
  7245. memset(L, 0, 16);
  7246. continue;
  7247. }
  7248. float best = 0;
  7249. float scale = max/(2*kMaxQ-1);
  7250. is_on_grid[0] = is_on_grid[1] = true;
  7251. for (int is = -9; is <= 9; ++is) {
  7252. float id = (2*kMaxQ-1+is*0.1f)/max;
  7253. float this_scale = 1/id;
  7254. for (int k = 0; k < 2; ++k) {
  7255. for (int i = 0; i < 8; ++i) {
  7256. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7257. Laux[8*k+i] = MAX(0, MIN(kMaxQ-1, l));
  7258. }
  7259. uint16_t u = 0;
  7260. for (int i = 0; i < 8; ++i) u |= (Laux[8*k+i] << 2*i);
  7261. int grid_index = kmap_q2xs[u];
  7262. is_on_grid_aux[k] = true;
  7263. if (grid_index < 0) {
  7264. is_on_grid_aux[k] = false;
  7265. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7266. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, this_scale, Laux + 8*k);
  7267. }
  7268. }
  7269. float sumqx = 0, sumq2 = 0;
  7270. for (int i = 0; i < 16; ++i) {
  7271. float w = weight[i];
  7272. float q = 2*Laux[i] + 1;
  7273. sumqx += w*xval[i]*q;
  7274. sumq2 += w*q*q;
  7275. }
  7276. if (sumq2 > 0 && sumqx*sumqx > best*sumq2) {
  7277. scale = sumqx/sumq2; best = scale*sumqx;
  7278. for (int i = 0; i < 16; ++i) L[i] = Laux[i];
  7279. for (int k = 0; k < 2; ++k) is_on_grid[k] = is_on_grid_aux[k];
  7280. }
  7281. }
  7282. int n_not_ongrid = 0;
  7283. for (int k = 0; k < 2; ++k) if (!is_on_grid[k]) ++n_not_ongrid;
  7284. if (n_not_ongrid > 0 && scale > 0) {
  7285. float id = 1/scale;
  7286. for (int k = 0; k < 2; ++k) {
  7287. if (is_on_grid[k]) continue;
  7288. uint16_t u = 0;
  7289. for (int i = 0; i < 8; ++i) {
  7290. int l = nearest_int(0.5f*(id*xval[8*k+i]-1));
  7291. l = MAX(0, MIN(kMaxQ-1, l));
  7292. u |= (l << 2*i);
  7293. L[8*k + i] = l;
  7294. }
  7295. int grid_index = kmap_q2xs[u];
  7296. if (grid_index < 0) {
  7297. const uint16_t * neighbours = kneighbors_q2xs - kmap_q2xs[u] - 1;
  7298. grid_index = iq2_find_best_neighbour(neighbours, kgrid_q2xs, xval + 8*k, waux + 8*k, scale, L + 8*k);
  7299. }
  7300. }
  7301. float sumqx = 0, sumq2 = 0;
  7302. for (int i = 0; i < 16; ++i) {
  7303. float w = weight[i];
  7304. float q = 2*L[i] + 1;
  7305. sumqx += w*xval[i]*q;
  7306. sumq2 += w*q*q;
  7307. }
  7308. if (sumq2 > 0) scale = sumqx/sumq2;
  7309. }
  7310. if (scale < 0) {
  7311. scale = -scale;
  7312. for (int k = 0; k < 2; ++k) block_signs[k] = (~block_signs[k]) & 127;
  7313. }
  7314. for (int k = 0; k < 2; ++k) {
  7315. uint16_t u = 0;
  7316. for (int i = 0; i < 8; ++i) u |= (L[8*k+i] << 2*i);
  7317. int grid_index = kmap_q2xs[u];
  7318. if (grid_index < 0) {
  7319. printf("Oops: found point %u not on grid:", u);
  7320. for (int i = 0; i < 8; ++i) printf(" %d", L[8*k+i]);
  7321. printf("\n");
  7322. GGML_ASSERT(false);
  7323. }
  7324. q2[2*ib+k] = grid_index | (block_signs[k] << 9);
  7325. }
  7326. GGML_ASSERT(scale >= 0);
  7327. scales[ib] = scale;
  7328. max_scale = MAX(max_scale, scale);
  7329. }
  7330. if (!max_scale) {
  7331. memset(y[ibl].qs, 0, QK_K/4);
  7332. continue;
  7333. }
  7334. float d = max_scale/31;
  7335. y[ibl].d = GGML_FP32_TO_FP16(d);
  7336. float id = 1/d;
  7337. for (int ib = 0; ib < QK_K/16; ++ib) {
  7338. int l = nearest_int(0.5f*(id*scales[ib]-1));
  7339. l = MAX(0, MIN(15, l));
  7340. if (ib%2 == 0) y[ibl].scales[ib/2] = l;
  7341. else y[ibl].scales[ib/2] |= (l << 4);
  7342. }
  7343. memcpy(y[ibl].qs, q2, QK_K/4);
  7344. }
  7345. }
  7346. size_t quantize_iq2_xxs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7347. (void)hist;
  7348. GGML_ASSERT(n_per_row%QK_K == 0);
  7349. int nblock = n_per_row/QK_K;
  7350. char * qrow = (char *)dst;
  7351. for (int row = 0; row < nrow; ++row) {
  7352. quantize_row_iq2_xxs_impl(src, qrow, n_per_row, quant_weights);
  7353. src += n_per_row;
  7354. qrow += nblock*sizeof(block_iq2_xxs);
  7355. }
  7356. return nrow * nblock * sizeof(block_iq2_xxs);
  7357. }
  7358. size_t quantize_iq2_xs(const float * src, void * dst, int nrow, int n_per_row, int64_t * hist, const float * quant_weights) {
  7359. (void)hist;
  7360. GGML_ASSERT(n_per_row%QK_K == 0);
  7361. int nblock = n_per_row/QK_K;
  7362. char * qrow = (char *)dst;
  7363. for (int row = 0; row < nrow; ++row) {
  7364. quantize_row_iq2_xs_impl(src, qrow, n_per_row, quant_weights);
  7365. src += n_per_row;
  7366. qrow += nblock*sizeof(block_iq2_xs);
  7367. }
  7368. return nrow * nblock * sizeof(block_iq2_xs);
  7369. }