ggml-quants.c 266 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067
  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. #ifdef __ARM_NEON
  8. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  9. //
  10. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  11. //
  12. #include <arm_neon.h>
  13. #else
  14. #ifdef __wasm_simd128__
  15. #include <wasm_simd128.h>
  16. #else
  17. #if defined(__POWER9_VECTOR__) || defined(__powerpc64__)
  18. #include <altivec.h>
  19. #undef bool
  20. #define bool _Bool
  21. #else
  22. #if defined(_MSC_VER) || defined(__MINGW32__)
  23. #include <intrin.h>
  24. #else
  25. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__) || defined(__SSE3__)
  26. #if !defined(__riscv)
  27. #include <immintrin.h>
  28. #endif
  29. #endif
  30. #endif
  31. #endif
  32. #endif
  33. #endif
  34. #ifdef __riscv_v_intrinsic
  35. #include <riscv_vector.h>
  36. #endif
  37. #undef MIN
  38. #undef MAX
  39. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  40. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  41. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  42. #if defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  43. // multiply int8_t, add results pairwise twice
  44. static inline __m128i mul_sum_i8_pairs(const __m128i x, const __m128i y) {
  45. // Get absolute values of x vectors
  46. const __m128i ax = _mm_sign_epi8(x, x);
  47. // Sign the values of the y vectors
  48. const __m128i sy = _mm_sign_epi8(y, x);
  49. // Perform multiplication and create 16-bit values
  50. const __m128i dot = _mm_maddubs_epi16(ax, sy);
  51. const __m128i ones = _mm_set1_epi16(1);
  52. return _mm_madd_epi16(ones, dot);
  53. }
  54. #if __AVX__ || __AVX2__ || __AVX512F__
  55. // horizontally add 8 floats
  56. static inline float hsum_float_8(const __m256 x) {
  57. __m128 res = _mm256_extractf128_ps(x, 1);
  58. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  59. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  60. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  61. return _mm_cvtss_f32(res);
  62. }
  63. // horizontally add 8 int32_t
  64. static inline int hsum_i32_8(const __m256i a) {
  65. const __m128i sum128 = _mm_add_epi32(_mm256_castsi256_si128(a), _mm256_extractf128_si256(a, 1));
  66. const __m128i hi64 = _mm_unpackhi_epi64(sum128, sum128);
  67. const __m128i sum64 = _mm_add_epi32(hi64, sum128);
  68. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  69. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  70. }
  71. // horizontally add 4 int32_t
  72. static inline int hsum_i32_4(const __m128i a) {
  73. const __m128i hi64 = _mm_unpackhi_epi64(a, a);
  74. const __m128i sum64 = _mm_add_epi32(hi64, a);
  75. const __m128i hi32 = _mm_shuffle_epi32(sum64, _MM_SHUFFLE(2, 3, 0, 1));
  76. return _mm_cvtsi128_si32(_mm_add_epi32(sum64, hi32));
  77. }
  78. #if defined(__AVX2__) || defined(__AVX512F__)
  79. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  80. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  81. uint32_t x32;
  82. memcpy(&x32, x, sizeof(uint32_t));
  83. const __m256i shuf_mask = _mm256_set_epi64x(
  84. 0x0303030303030303, 0x0202020202020202,
  85. 0x0101010101010101, 0x0000000000000000);
  86. __m256i bytes = _mm256_shuffle_epi8(_mm256_set1_epi32(x32), shuf_mask);
  87. const __m256i bit_mask = _mm256_set1_epi64x(0x7fbfdfeff7fbfdfe);
  88. bytes = _mm256_or_si256(bytes, bit_mask);
  89. return _mm256_cmpeq_epi8(bytes, _mm256_set1_epi64x(-1));
  90. }
  91. // Unpack 32 4-bit fields into 32 bytes
  92. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  93. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  94. {
  95. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  96. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  97. const __m256i lowMask = _mm256_set1_epi8( 0xF );
  98. return _mm256_and_si256(lowMask, bytes);
  99. }
  100. // add int16_t pairwise and return as float vector
  101. static inline __m256 sum_i16_pairs_float(const __m256i x) {
  102. const __m256i ones = _mm256_set1_epi16(1);
  103. const __m256i summed_pairs = _mm256_madd_epi16(ones, x);
  104. return _mm256_cvtepi32_ps(summed_pairs);
  105. }
  106. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  107. #if __AVXVNNI__
  108. const __m256i zero = _mm256_setzero_si256();
  109. const __m256i summed_pairs = _mm256_dpbusd_epi32(zero, ax, sy);
  110. return _mm256_cvtepi32_ps(summed_pairs);
  111. #else
  112. // Perform multiplication and create 16-bit values
  113. const __m256i dot = _mm256_maddubs_epi16(ax, sy);
  114. return sum_i16_pairs_float(dot);
  115. #endif
  116. }
  117. // multiply int8_t, add results pairwise twice and return as float vector
  118. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  119. #if __AVXVNNIINT8__
  120. const __m256i zero = _mm256_setzero_si256();
  121. const __m256i summed_pairs = _mm256_dpbssd_epi32(zero, x, y);
  122. return _mm256_cvtepi32_ps(summed_pairs);
  123. #else
  124. // Get absolute values of x vectors
  125. const __m256i ax = _mm256_sign_epi8(x, x);
  126. // Sign the values of the y vectors
  127. const __m256i sy = _mm256_sign_epi8(y, x);
  128. return mul_sum_us8_pairs_float(ax, sy);
  129. #endif
  130. }
  131. static inline __m128i packNibbles( __m256i bytes )
  132. {
  133. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  134. #if __AVX512F__
  135. const __m256i bytes_srli_4 = _mm256_srli_epi16(bytes, 4); // 0000_0000_abcd_0000
  136. bytes = _mm256_or_si256(bytes, bytes_srli_4); // 0000_abcd_abcd_efgh
  137. return _mm256_cvtepi16_epi8(bytes); // abcd_efgh
  138. #else
  139. const __m256i lowByte = _mm256_set1_epi16( 0xFF );
  140. __m256i high = _mm256_andnot_si256( lowByte, bytes );
  141. __m256i low = _mm256_and_si256( lowByte, bytes );
  142. high = _mm256_srli_epi16( high, 4 );
  143. bytes = _mm256_or_si256( low, high );
  144. // Compress uint16_t lanes into bytes
  145. __m128i r0 = _mm256_castsi256_si128( bytes );
  146. __m128i r1 = _mm256_extracti128_si256( bytes, 1 );
  147. return _mm_packus_epi16( r0, r1 );
  148. #endif
  149. }
  150. #elif defined(__AVX__)
  151. // spread 32 bits to 32 bytes { 0x00, 0xFF }
  152. static inline __m256i bytes_from_bits_32(const uint8_t * x) {
  153. uint32_t x32;
  154. memcpy(&x32, x, sizeof(uint32_t));
  155. const __m128i shuf_maskl = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  156. const __m128i shuf_maskh = _mm_set_epi64x(0x0303030303030303, 0x0202020202020202);
  157. __m128i bytesl = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskl);
  158. __m128i bytesh = _mm_shuffle_epi8(_mm_set1_epi32(x32), shuf_maskh);
  159. const __m128i bit_mask = _mm_set1_epi64x(0x7fbfdfeff7fbfdfe);
  160. bytesl = _mm_or_si128(bytesl, bit_mask);
  161. bytesh = _mm_or_si128(bytesh, bit_mask);
  162. bytesl = _mm_cmpeq_epi8(bytesl, _mm_set1_epi64x(-1));
  163. bytesh = _mm_cmpeq_epi8(bytesh, _mm_set1_epi64x(-1));
  164. return MM256_SET_M128I(bytesh, bytesl);
  165. }
  166. // Unpack 32 4-bit fields into 32 bytes
  167. // The output vector contains 32 bytes, each one in [ 0 .. 15 ] interval
  168. static inline __m256i bytes_from_nibbles_32(const uint8_t * rsi)
  169. {
  170. // Load 16 bytes from memory
  171. __m128i tmpl = _mm_loadu_si128((const __m128i *)rsi);
  172. __m128i tmph = _mm_srli_epi16(tmpl, 4);
  173. const __m128i lowMask = _mm_set1_epi8(0xF);
  174. tmpl = _mm_and_si128(lowMask, tmpl);
  175. tmph = _mm_and_si128(lowMask, tmph);
  176. return MM256_SET_M128I(tmph, tmpl);
  177. }
  178. // add int16_t pairwise and return as float vector
  179. static inline __m256 sum_i16_pairs_float(const __m128i xh, const __m128i xl) {
  180. const __m128i ones = _mm_set1_epi16(1);
  181. const __m128i summed_pairsl = _mm_madd_epi16(ones, xl);
  182. const __m128i summed_pairsh = _mm_madd_epi16(ones, xh);
  183. const __m256i summed_pairs = MM256_SET_M128I(summed_pairsh, summed_pairsl);
  184. return _mm256_cvtepi32_ps(summed_pairs);
  185. }
  186. static inline __m256 mul_sum_us8_pairs_float(const __m256i ax, const __m256i sy) {
  187. const __m128i axl = _mm256_castsi256_si128(ax);
  188. const __m128i axh = _mm256_extractf128_si256(ax, 1);
  189. const __m128i syl = _mm256_castsi256_si128(sy);
  190. const __m128i syh = _mm256_extractf128_si256(sy, 1);
  191. // Perform multiplication and create 16-bit values
  192. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  193. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  194. return sum_i16_pairs_float(doth, dotl);
  195. }
  196. // multiply int8_t, add results pairwise twice and return as float vector
  197. static inline __m256 mul_sum_i8_pairs_float(const __m256i x, const __m256i y) {
  198. const __m128i xl = _mm256_castsi256_si128(x);
  199. const __m128i xh = _mm256_extractf128_si256(x, 1);
  200. const __m128i yl = _mm256_castsi256_si128(y);
  201. const __m128i yh = _mm256_extractf128_si256(y, 1);
  202. // Get absolute values of x vectors
  203. const __m128i axl = _mm_sign_epi8(xl, xl);
  204. const __m128i axh = _mm_sign_epi8(xh, xh);
  205. // Sign the values of the y vectors
  206. const __m128i syl = _mm_sign_epi8(yl, xl);
  207. const __m128i syh = _mm_sign_epi8(yh, xh);
  208. // Perform multiplication and create 16-bit values
  209. const __m128i dotl = _mm_maddubs_epi16(axl, syl);
  210. const __m128i doth = _mm_maddubs_epi16(axh, syh);
  211. return sum_i16_pairs_float(doth, dotl);
  212. }
  213. static inline __m128i packNibbles( __m128i bytes1, __m128i bytes2 )
  214. {
  215. // Move bits within 16-bit lanes from 0000_abcd_0000_efgh into 0000_0000_abcd_efgh
  216. const __m128i lowByte = _mm_set1_epi16( 0xFF );
  217. __m128i high = _mm_andnot_si128( lowByte, bytes1 );
  218. __m128i low = _mm_and_si128( lowByte, bytes1 );
  219. high = _mm_srli_epi16( high, 4 );
  220. bytes1 = _mm_or_si128( low, high );
  221. high = _mm_andnot_si128( lowByte, bytes2 );
  222. low = _mm_and_si128( lowByte, bytes2 );
  223. high = _mm_srli_epi16( high, 4 );
  224. bytes2 = _mm_or_si128( low, high );
  225. return _mm_packus_epi16( bytes1, bytes2);
  226. }
  227. #endif
  228. #elif defined(__SSSE3__)
  229. // horizontally add 4x4 floats
  230. static inline float hsum_float_4x4(const __m128 a, const __m128 b, const __m128 c, const __m128 d) {
  231. __m128 res_0 =_mm_hadd_ps(a, b);
  232. __m128 res_1 =_mm_hadd_ps(c, d);
  233. __m128 res =_mm_hadd_ps(res_0, res_1);
  234. res =_mm_hadd_ps(res, res);
  235. res =_mm_hadd_ps(res, res);
  236. return _mm_cvtss_f32(res);
  237. }
  238. #endif // __AVX__ || __AVX2__ || __AVX512F__
  239. #endif // defined(__AVX__) || defined(__AVX2__) || defined(__AVX512F__) || defined(__SSSE3__)
  240. #if defined(__ARM_NEON)
  241. #if !defined(__aarch64__)
  242. // 64-bit compatibility
  243. // vaddvq_s16
  244. // vpaddq_s16
  245. // vaddvq_s32
  246. // vaddvq_f32
  247. // vmaxvq_f32
  248. // vcvtnq_s32_f32
  249. inline static int32_t vaddvq_s16(int16x8_t v) {
  250. return
  251. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  252. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  253. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  254. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  255. }
  256. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  257. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  258. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  259. return vcombine_s16(a0, b0);
  260. }
  261. inline static int32_t vaddvq_s32(int32x4_t v) {
  262. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  263. }
  264. inline static float vaddvq_f32(float32x4_t v) {
  265. return vgetq_lane_f32(v, 0) + vgetq_lane_f32(v, 1) + vgetq_lane_f32(v, 2) + vgetq_lane_f32(v, 3);
  266. }
  267. inline static float vmaxvq_f32(float32x4_t v) {
  268. return
  269. MAX(MAX(vgetq_lane_f32(v, 0), vgetq_lane_f32(v, 1)),
  270. MAX(vgetq_lane_f32(v, 2), vgetq_lane_f32(v, 3)));
  271. }
  272. inline static int32x4_t vcvtnq_s32_f32(float32x4_t v) {
  273. int32x4_t res;
  274. res[0] = roundf(vgetq_lane_f32(v, 0));
  275. res[1] = roundf(vgetq_lane_f32(v, 1));
  276. res[2] = roundf(vgetq_lane_f32(v, 2));
  277. res[3] = roundf(vgetq_lane_f32(v, 3));
  278. return res;
  279. }
  280. // vld1q_s16_x2
  281. // vld1q_u8_x2
  282. // vld1q_u8_x4
  283. // vld1q_s8_x2
  284. // vld1q_s8_x4
  285. // TODO: double-check these work correctly
  286. typedef struct ggml_int16x8x2_t {
  287. int16x8_t val[2];
  288. } ggml_int16x8x2_t;
  289. inline static ggml_int16x8x2_t ggml_vld1q_s16_x2(const int16_t * ptr) {
  290. ggml_int16x8x2_t res;
  291. res.val[0] = vld1q_s16(ptr + 0);
  292. res.val[1] = vld1q_s16(ptr + 8);
  293. return res;
  294. }
  295. typedef struct ggml_uint8x16x2_t {
  296. uint8x16_t val[2];
  297. } ggml_uint8x16x2_t;
  298. inline static ggml_uint8x16x2_t ggml_vld1q_u8_x2(const uint8_t * ptr) {
  299. ggml_uint8x16x2_t res;
  300. res.val[0] = vld1q_u8(ptr + 0);
  301. res.val[1] = vld1q_u8(ptr + 16);
  302. return res;
  303. }
  304. typedef struct ggml_uint8x16x4_t {
  305. uint8x16_t val[4];
  306. } ggml_uint8x16x4_t;
  307. inline static ggml_uint8x16x4_t ggml_vld1q_u8_x4(const uint8_t * ptr) {
  308. ggml_uint8x16x4_t res;
  309. res.val[0] = vld1q_u8(ptr + 0);
  310. res.val[1] = vld1q_u8(ptr + 16);
  311. res.val[2] = vld1q_u8(ptr + 32);
  312. res.val[3] = vld1q_u8(ptr + 48);
  313. return res;
  314. }
  315. typedef struct ggml_int8x16x2_t {
  316. int8x16_t val[2];
  317. } ggml_int8x16x2_t;
  318. inline static ggml_int8x16x2_t ggml_vld1q_s8_x2(const int8_t * ptr) {
  319. ggml_int8x16x2_t res;
  320. res.val[0] = vld1q_s8(ptr + 0);
  321. res.val[1] = vld1q_s8(ptr + 16);
  322. return res;
  323. }
  324. typedef struct ggml_int8x16x4_t {
  325. int8x16_t val[4];
  326. } ggml_int8x16x4_t;
  327. inline static ggml_int8x16x4_t ggml_vld1q_s8_x4(const int8_t * ptr) {
  328. ggml_int8x16x4_t res;
  329. res.val[0] = vld1q_s8(ptr + 0);
  330. res.val[1] = vld1q_s8(ptr + 16);
  331. res.val[2] = vld1q_s8(ptr + 32);
  332. res.val[3] = vld1q_s8(ptr + 48);
  333. return res;
  334. }
  335. #else
  336. #define ggml_int16x8x2_t int16x8x2_t
  337. #define ggml_uint8x16x2_t uint8x16x2_t
  338. #define ggml_uint8x16x4_t uint8x16x4_t
  339. #define ggml_int8x16x2_t int8x16x2_t
  340. #define ggml_int8x16x4_t int8x16x4_t
  341. #define ggml_vld1q_s16_x2 vld1q_s16_x2
  342. #define ggml_vld1q_u8_x2 vld1q_u8_x2
  343. #define ggml_vld1q_u8_x4 vld1q_u8_x4
  344. #define ggml_vld1q_s8_x2 vld1q_s8_x2
  345. #define ggml_vld1q_s8_x4 vld1q_s8_x4
  346. #endif
  347. #if !defined(__ARM_FEATURE_DOTPROD)
  348. inline static int32x4_t ggml_vdotq_s32(int32x4_t acc, int8x16_t a, int8x16_t b) {
  349. const int16x8_t p0 = vmull_s8(vget_low_s8 (a), vget_low_s8 (b));
  350. const int16x8_t p1 = vmull_s8(vget_high_s8(a), vget_high_s8(b));
  351. return vaddq_s32(acc, vaddq_s32(vpaddlq_s16(p0), vpaddlq_s16(p1)));
  352. }
  353. #else
  354. #define ggml_vdotq_s32(a, b, c) vdotq_s32(a, b, c)
  355. #endif
  356. #endif
  357. #if defined(__ARM_NEON) || defined(__wasm_simd128__)
  358. #define B1(c,s,n) 0x ## n ## c , 0x ## n ## s
  359. #define B2(c,s,n) B1(c,s,n ## c), B1(c,s,n ## s)
  360. #define B3(c,s,n) B2(c,s,n ## c), B2(c,s,n ## s)
  361. #define B4(c,s,n) B3(c,s,n ## c), B3(c,s,n ## s)
  362. #define B5(c,s,n) B4(c,s,n ## c), B4(c,s,n ## s)
  363. #define B6(c,s,n) B5(c,s,n ## c), B5(c,s,n ## s)
  364. #define B7(c,s,n) B6(c,s,n ## c), B6(c,s,n ## s)
  365. #define B8(c,s ) B7(c,s, c), B7(c,s, s)
  366. // precomputed tables for expanding 8bits to 8 bytes:
  367. static const uint64_t table_b2b_0[1 << 8] = { B8(00, 10) }; // ( b) << 4
  368. static const uint64_t table_b2b_1[1 << 8] = { B8(10, 00) }; // (!b) << 4
  369. #endif
  370. // reference implementation for deterministic creation of model files
  371. void quantize_row_q4_0_reference(const float * restrict x, block_q4_0 * restrict y, int k) {
  372. static const int qk = QK4_0;
  373. assert(k % qk == 0);
  374. const int nb = k / qk;
  375. for (int i = 0; i < nb; i++) {
  376. float amax = 0.0f; // absolute max
  377. float max = 0.0f;
  378. for (int j = 0; j < qk; j++) {
  379. const float v = x[i*qk + j];
  380. if (amax < fabsf(v)) {
  381. amax = fabsf(v);
  382. max = v;
  383. }
  384. }
  385. const float d = max / -8;
  386. const float id = d ? 1.0f/d : 0.0f;
  387. y[i].d = GGML_FP32_TO_FP16(d);
  388. for (int j = 0; j < qk/2; ++j) {
  389. const float x0 = x[i*qk + 0 + j]*id;
  390. const float x1 = x[i*qk + qk/2 + j]*id;
  391. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 8.5f));
  392. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 8.5f));
  393. y[i].qs[j] = xi0;
  394. y[i].qs[j] |= xi1 << 4;
  395. }
  396. }
  397. }
  398. void quantize_row_q4_0(const float * restrict x, void * restrict y, int k) {
  399. quantize_row_q4_0_reference(x, y, k);
  400. }
  401. void quantize_row_q4_1_reference(const float * restrict x, block_q4_1 * restrict y, int k) {
  402. const int qk = QK4_1;
  403. assert(k % qk == 0);
  404. const int nb = k / qk;
  405. for (int i = 0; i < nb; i++) {
  406. float min = FLT_MAX;
  407. float max = -FLT_MAX;
  408. for (int j = 0; j < qk; j++) {
  409. const float v = x[i*qk + j];
  410. if (v < min) min = v;
  411. if (v > max) max = v;
  412. }
  413. const float d = (max - min) / ((1 << 4) - 1);
  414. const float id = d ? 1.0f/d : 0.0f;
  415. y[i].d = GGML_FP32_TO_FP16(d);
  416. y[i].m = GGML_FP32_TO_FP16(min);
  417. for (int j = 0; j < qk/2; ++j) {
  418. const float x0 = (x[i*qk + 0 + j] - min)*id;
  419. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  420. const uint8_t xi0 = MIN(15, (int8_t)(x0 + 0.5f));
  421. const uint8_t xi1 = MIN(15, (int8_t)(x1 + 0.5f));
  422. y[i].qs[j] = xi0;
  423. y[i].qs[j] |= xi1 << 4;
  424. }
  425. }
  426. }
  427. void quantize_row_q4_1(const float * restrict x, void * restrict y, int k) {
  428. quantize_row_q4_1_reference(x, y, k);
  429. }
  430. void quantize_row_q5_0_reference(const float * restrict x, block_q5_0 * restrict y, int k) {
  431. static const int qk = QK5_0;
  432. assert(k % qk == 0);
  433. const int nb = k / qk;
  434. for (int i = 0; i < nb; i++) {
  435. float amax = 0.0f; // absolute max
  436. float max = 0.0f;
  437. for (int j = 0; j < qk; j++) {
  438. const float v = x[i*qk + j];
  439. if (amax < fabsf(v)) {
  440. amax = fabsf(v);
  441. max = v;
  442. }
  443. }
  444. const float d = max / -16;
  445. const float id = d ? 1.0f/d : 0.0f;
  446. y[i].d = GGML_FP32_TO_FP16(d);
  447. uint32_t qh = 0;
  448. for (int j = 0; j < qk/2; ++j) {
  449. const float x0 = x[i*qk + 0 + j]*id;
  450. const float x1 = x[i*qk + qk/2 + j]*id;
  451. const uint8_t xi0 = MIN(31, (int8_t)(x0 + 16.5f));
  452. const uint8_t xi1 = MIN(31, (int8_t)(x1 + 16.5f));
  453. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  454. // get the 5-th bit and store it in qh at the right position
  455. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  456. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  457. }
  458. memcpy(&y[i].qh, &qh, sizeof(qh));
  459. }
  460. }
  461. void quantize_row_q5_0(const float * restrict x, void * restrict y, int k) {
  462. quantize_row_q5_0_reference(x, y, k);
  463. }
  464. void quantize_row_q5_1_reference(const float * restrict x, block_q5_1 * restrict y, int k) {
  465. const int qk = QK5_1;
  466. assert(k % qk == 0);
  467. const int nb = k / qk;
  468. for (int i = 0; i < nb; i++) {
  469. float min = FLT_MAX;
  470. float max = -FLT_MAX;
  471. for (int j = 0; j < qk; j++) {
  472. const float v = x[i*qk + j];
  473. if (v < min) min = v;
  474. if (v > max) max = v;
  475. }
  476. const float d = (max - min) / ((1 << 5) - 1);
  477. const float id = d ? 1.0f/d : 0.0f;
  478. y[i].d = GGML_FP32_TO_FP16(d);
  479. y[i].m = GGML_FP32_TO_FP16(min);
  480. uint32_t qh = 0;
  481. for (int j = 0; j < qk/2; ++j) {
  482. const float x0 = (x[i*qk + 0 + j] - min)*id;
  483. const float x1 = (x[i*qk + qk/2 + j] - min)*id;
  484. const uint8_t xi0 = (uint8_t)(x0 + 0.5f);
  485. const uint8_t xi1 = (uint8_t)(x1 + 0.5f);
  486. y[i].qs[j] = (xi0 & 0x0F) | ((xi1 & 0x0F) << 4);
  487. // get the 5-th bit and store it in qh at the right position
  488. qh |= ((xi0 & 0x10u) >> 4) << (j + 0);
  489. qh |= ((xi1 & 0x10u) >> 4) << (j + qk/2);
  490. }
  491. memcpy(&y[i].qh, &qh, sizeof(y[i].qh));
  492. }
  493. }
  494. void quantize_row_q5_1(const float * restrict x, void * restrict y, int k) {
  495. quantize_row_q5_1_reference(x, y, k);
  496. }
  497. // reference implementation for deterministic creation of model files
  498. void quantize_row_q8_0_reference(const float * restrict x, block_q8_0 * restrict y, int k) {
  499. assert(k % QK8_0 == 0);
  500. const int nb = k / QK8_0;
  501. for (int i = 0; i < nb; i++) {
  502. float amax = 0.0f; // absolute max
  503. for (int j = 0; j < QK8_0; j++) {
  504. const float v = x[i*QK8_0 + j];
  505. amax = MAX(amax, fabsf(v));
  506. }
  507. const float d = amax / ((1 << 7) - 1);
  508. const float id = d ? 1.0f/d : 0.0f;
  509. y[i].d = GGML_FP32_TO_FP16(d);
  510. for (int j = 0; j < QK8_0; ++j) {
  511. const float x0 = x[i*QK8_0 + j]*id;
  512. y[i].qs[j] = roundf(x0);
  513. }
  514. }
  515. }
  516. void quantize_row_q8_0(const float * restrict x, void * restrict vy, int k) {
  517. assert(QK8_0 == 32);
  518. assert(k % QK8_0 == 0);
  519. const int nb = k / QK8_0;
  520. block_q8_0 * restrict y = vy;
  521. #if defined(__ARM_NEON)
  522. for (int i = 0; i < nb; i++) {
  523. float32x4_t srcv [8];
  524. float32x4_t asrcv[8];
  525. float32x4_t amaxv[8];
  526. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  527. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  528. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  529. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  530. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  531. const float amax = vmaxvq_f32(amaxv[0]);
  532. const float d = amax / ((1 << 7) - 1);
  533. const float id = d ? 1.0f/d : 0.0f;
  534. y[i].d = GGML_FP32_TO_FP16(d);
  535. for (int j = 0; j < 8; j++) {
  536. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  537. const int32x4_t vi = vcvtnq_s32_f32(v);
  538. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  539. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  540. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  541. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  542. }
  543. }
  544. #elif defined(__wasm_simd128__)
  545. for (int i = 0; i < nb; i++) {
  546. v128_t srcv [8];
  547. v128_t asrcv[8];
  548. v128_t amaxv[8];
  549. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  550. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  551. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  552. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  553. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  554. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  555. wasm_f32x4_extract_lane(amaxv[0], 1)),
  556. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  557. wasm_f32x4_extract_lane(amaxv[0], 3)));
  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 v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  563. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  564. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  565. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  566. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  567. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  568. }
  569. }
  570. #elif defined(__AVX2__) || defined(__AVX__)
  571. for (int i = 0; i < nb; i++) {
  572. // Load elements into 4 AVX vectors
  573. __m256 v0 = _mm256_loadu_ps( x );
  574. __m256 v1 = _mm256_loadu_ps( x + 8 );
  575. __m256 v2 = _mm256_loadu_ps( x + 16 );
  576. __m256 v3 = _mm256_loadu_ps( x + 24 );
  577. x += 32;
  578. // Compute max(abs(e)) for the block
  579. const __m256 signBit = _mm256_set1_ps( -0.0f );
  580. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  581. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  582. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  583. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  584. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  585. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  586. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  587. const float maxScalar = _mm_cvtss_f32( max4 );
  588. // Quantize these floats
  589. const float d = maxScalar / 127.f;
  590. y[i].d = GGML_FP32_TO_FP16(d);
  591. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  592. const __m256 mul = _mm256_set1_ps( id );
  593. // Apply the multiplier
  594. v0 = _mm256_mul_ps( v0, mul );
  595. v1 = _mm256_mul_ps( v1, mul );
  596. v2 = _mm256_mul_ps( v2, mul );
  597. v3 = _mm256_mul_ps( v3, mul );
  598. // Round to nearest integer
  599. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  600. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  601. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  602. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  603. // Convert floats to integers
  604. __m256i i0 = _mm256_cvtps_epi32( v0 );
  605. __m256i i1 = _mm256_cvtps_epi32( v1 );
  606. __m256i i2 = _mm256_cvtps_epi32( v2 );
  607. __m256i i3 = _mm256_cvtps_epi32( v3 );
  608. #if defined(__AVX2__)
  609. // Convert int32 to int16
  610. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  611. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  612. // Convert int16 to int8
  613. 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
  614. // We got our precious signed bytes, but the order is now wrong
  615. // These AVX2 pack instructions process 16-byte pieces independently
  616. // The following instruction is fixing the order
  617. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  618. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  619. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  620. #else
  621. // Since we don't have in AVX some necessary functions,
  622. // we split the registers in half and call AVX2 analogs from SSE
  623. __m128i ni0 = _mm256_castsi256_si128( i0 );
  624. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  625. __m128i ni2 = _mm256_castsi256_si128( i1 );
  626. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  627. __m128i ni4 = _mm256_castsi256_si128( i2 );
  628. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  629. __m128i ni6 = _mm256_castsi256_si128( i3 );
  630. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  631. // Convert int32 to int16
  632. ni0 = _mm_packs_epi32( ni0, ni1 );
  633. ni2 = _mm_packs_epi32( ni2, ni3 );
  634. ni4 = _mm_packs_epi32( ni4, ni5 );
  635. ni6 = _mm_packs_epi32( ni6, ni7 );
  636. // Convert int16 to int8
  637. ni0 = _mm_packs_epi16( ni0, ni2 );
  638. ni4 = _mm_packs_epi16( ni4, ni6 );
  639. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  640. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  641. #endif
  642. }
  643. #elif defined(__riscv_v_intrinsic)
  644. size_t vl = __riscv_vsetvl_e32m4(QK8_0);
  645. for (int i = 0; i < nb; i++) {
  646. // load elements
  647. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_0, vl);
  648. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  649. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0f, vl);
  650. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  651. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  652. const float d = amax / ((1 << 7) - 1);
  653. const float id = d ? 1.0f/d : 0.0f;
  654. y[i].d = GGML_FP32_TO_FP16(d);
  655. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  656. // convert to integer
  657. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  658. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  659. // store result
  660. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  661. }
  662. #else
  663. GGML_UNUSED(nb);
  664. // scalar
  665. quantize_row_q8_0_reference(x, y, k);
  666. #endif
  667. }
  668. // reference implementation for deterministic creation of model files
  669. void quantize_row_q8_1_reference(const float * restrict x, block_q8_1 * restrict y, int k) {
  670. assert(QK8_1 == 32);
  671. assert(k % QK8_1 == 0);
  672. const int nb = k / QK8_1;
  673. for (int i = 0; i < nb; i++) {
  674. float amax = 0.0f; // absolute max
  675. for (int j = 0; j < QK8_1; j++) {
  676. const float v = x[i*QK8_1 + j];
  677. amax = MAX(amax, fabsf(v));
  678. }
  679. const float d = amax / ((1 << 7) - 1);
  680. const float id = d ? 1.0f/d : 0.0f;
  681. y[i].d = d;
  682. int sum = 0;
  683. for (int j = 0; j < QK8_1/2; ++j) {
  684. const float v0 = x[i*QK8_1 + j]*id;
  685. const float v1 = x[i*QK8_1 + QK8_1/2 + j]*id;
  686. y[i].qs[ j] = roundf(v0);
  687. y[i].qs[QK8_1/2 + j] = roundf(v1);
  688. sum += y[i].qs[ j];
  689. sum += y[i].qs[QK8_1/2 + j];
  690. }
  691. y[i].s = sum*d;
  692. }
  693. }
  694. void quantize_row_q8_1(const float * restrict x, void * restrict vy, int k) {
  695. assert(k % QK8_1 == 0);
  696. const int nb = k / QK8_1;
  697. block_q8_1 * restrict y = vy;
  698. #if defined(__ARM_NEON)
  699. for (int i = 0; i < nb; i++) {
  700. float32x4_t srcv [8];
  701. float32x4_t asrcv[8];
  702. float32x4_t amaxv[8];
  703. for (int j = 0; j < 8; j++) srcv[j] = vld1q_f32(x + i*32 + 4*j);
  704. for (int j = 0; j < 8; j++) asrcv[j] = vabsq_f32(srcv[j]);
  705. for (int j = 0; j < 4; j++) amaxv[2*j] = vmaxq_f32(asrcv[2*j], asrcv[2*j+1]);
  706. for (int j = 0; j < 2; j++) amaxv[4*j] = vmaxq_f32(amaxv[4*j], amaxv[4*j+2]);
  707. for (int j = 0; j < 1; j++) amaxv[8*j] = vmaxq_f32(amaxv[8*j], amaxv[8*j+4]);
  708. const float amax = vmaxvq_f32(amaxv[0]);
  709. const float d = amax / ((1 << 7) - 1);
  710. const float id = d ? 1.0f/d : 0.0f;
  711. y[i].d = d;
  712. int32x4_t accv = vdupq_n_s32(0);
  713. for (int j = 0; j < 8; j++) {
  714. const float32x4_t v = vmulq_n_f32(srcv[j], id);
  715. const int32x4_t vi = vcvtnq_s32_f32(v);
  716. y[i].qs[4*j + 0] = vgetq_lane_s32(vi, 0);
  717. y[i].qs[4*j + 1] = vgetq_lane_s32(vi, 1);
  718. y[i].qs[4*j + 2] = vgetq_lane_s32(vi, 2);
  719. y[i].qs[4*j + 3] = vgetq_lane_s32(vi, 3);
  720. accv = vaddq_s32(accv, vi);
  721. }
  722. y[i].s = d * vaddvq_s32(accv);
  723. }
  724. #elif defined(__wasm_simd128__)
  725. for (int i = 0; i < nb; i++) {
  726. v128_t srcv [8];
  727. v128_t asrcv[8];
  728. v128_t amaxv[8];
  729. for (int j = 0; j < 8; j++) srcv[j] = wasm_v128_load(x + i*32 + 4*j);
  730. for (int j = 0; j < 8; j++) asrcv[j] = wasm_f32x4_abs(srcv[j]);
  731. for (int j = 0; j < 4; j++) amaxv[2*j] = wasm_f32x4_max(asrcv[2*j], asrcv[2*j+1]);
  732. for (int j = 0; j < 2; j++) amaxv[4*j] = wasm_f32x4_max(amaxv[4*j], amaxv[4*j+2]);
  733. for (int j = 0; j < 1; j++) amaxv[8*j] = wasm_f32x4_max(amaxv[8*j], amaxv[8*j+4]);
  734. const float amax = MAX(MAX(wasm_f32x4_extract_lane(amaxv[0], 0),
  735. wasm_f32x4_extract_lane(amaxv[0], 1)),
  736. MAX(wasm_f32x4_extract_lane(amaxv[0], 2),
  737. wasm_f32x4_extract_lane(amaxv[0], 3)));
  738. const float d = amax / ((1 << 7) - 1);
  739. const float id = d ? 1.0f/d : 0.0f;
  740. y[i].d = d;
  741. v128_t accv = wasm_i32x4_splat(0);
  742. for (int j = 0; j < 8; j++) {
  743. const v128_t v = wasm_f32x4_mul(srcv[j], wasm_f32x4_splat(id));
  744. const v128_t vi = wasm_i32x4_trunc_sat_f32x4(v);
  745. y[i].qs[4*j + 0] = wasm_i32x4_extract_lane(vi, 0);
  746. y[i].qs[4*j + 1] = wasm_i32x4_extract_lane(vi, 1);
  747. y[i].qs[4*j + 2] = wasm_i32x4_extract_lane(vi, 2);
  748. y[i].qs[4*j + 3] = wasm_i32x4_extract_lane(vi, 3);
  749. accv = wasm_i32x4_add(accv, vi);
  750. }
  751. y[i].s = d * (wasm_i32x4_extract_lane(accv, 0) +
  752. wasm_i32x4_extract_lane(accv, 1) +
  753. wasm_i32x4_extract_lane(accv, 2) +
  754. wasm_i32x4_extract_lane(accv, 3));
  755. }
  756. #elif defined(__AVX2__) || defined(__AVX__)
  757. for (int i = 0; i < nb; i++) {
  758. // Load elements into 4 AVX vectors
  759. __m256 v0 = _mm256_loadu_ps( x );
  760. __m256 v1 = _mm256_loadu_ps( x + 8 );
  761. __m256 v2 = _mm256_loadu_ps( x + 16 );
  762. __m256 v3 = _mm256_loadu_ps( x + 24 );
  763. x += 32;
  764. // Compute max(abs(e)) for the block
  765. const __m256 signBit = _mm256_set1_ps( -0.0f );
  766. __m256 maxAbs = _mm256_andnot_ps( signBit, v0 );
  767. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v1 ) );
  768. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v2 ) );
  769. maxAbs = _mm256_max_ps( maxAbs, _mm256_andnot_ps( signBit, v3 ) );
  770. __m128 max4 = _mm_max_ps( _mm256_extractf128_ps( maxAbs, 1 ), _mm256_castps256_ps128( maxAbs ) );
  771. max4 = _mm_max_ps( max4, _mm_movehl_ps( max4, max4 ) );
  772. max4 = _mm_max_ss( max4, _mm_movehdup_ps( max4 ) );
  773. const float maxScalar = _mm_cvtss_f32( max4 );
  774. // Quantize these floats
  775. const float d = maxScalar / 127.f;
  776. y[i].d = d;
  777. const float id = ( maxScalar != 0.0f ) ? 127.f / maxScalar : 0.0f;
  778. const __m256 mul = _mm256_set1_ps( id );
  779. // Apply the multiplier
  780. v0 = _mm256_mul_ps( v0, mul );
  781. v1 = _mm256_mul_ps( v1, mul );
  782. v2 = _mm256_mul_ps( v2, mul );
  783. v3 = _mm256_mul_ps( v3, mul );
  784. // Round to nearest integer
  785. v0 = _mm256_round_ps( v0, _MM_ROUND_NEAREST );
  786. v1 = _mm256_round_ps( v1, _MM_ROUND_NEAREST );
  787. v2 = _mm256_round_ps( v2, _MM_ROUND_NEAREST );
  788. v3 = _mm256_round_ps( v3, _MM_ROUND_NEAREST );
  789. // Convert floats to integers
  790. __m256i i0 = _mm256_cvtps_epi32( v0 );
  791. __m256i i1 = _mm256_cvtps_epi32( v1 );
  792. __m256i i2 = _mm256_cvtps_epi32( v2 );
  793. __m256i i3 = _mm256_cvtps_epi32( v3 );
  794. #if defined(__AVX2__)
  795. // Compute the sum of the quants and set y[i].s
  796. y[i].s = d * hsum_i32_8(_mm256_add_epi32(_mm256_add_epi32(i0, i1), _mm256_add_epi32(i2, i3)));
  797. // Convert int32 to int16
  798. i0 = _mm256_packs_epi32( i0, i1 ); // 0, 1, 2, 3, 8, 9, 10, 11, 4, 5, 6, 7, 12, 13, 14, 15
  799. i2 = _mm256_packs_epi32( i2, i3 ); // 16, 17, 18, 19, 24, 25, 26, 27, 20, 21, 22, 23, 28, 29, 30, 31
  800. // Convert int16 to int8
  801. 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
  802. // We got our precious signed bytes, but the order is now wrong
  803. // These AVX2 pack instructions process 16-byte pieces independently
  804. // The following instruction is fixing the order
  805. const __m256i perm = _mm256_setr_epi32( 0, 4, 1, 5, 2, 6, 3, 7 );
  806. i0 = _mm256_permutevar8x32_epi32( i0, perm );
  807. _mm256_storeu_si256((__m256i *)y[i].qs, i0);
  808. #else
  809. // Since we don't have in AVX some necessary functions,
  810. // we split the registers in half and call AVX2 analogs from SSE
  811. __m128i ni0 = _mm256_castsi256_si128( i0 );
  812. __m128i ni1 = _mm256_extractf128_si256( i0, 1);
  813. __m128i ni2 = _mm256_castsi256_si128( i1 );
  814. __m128i ni3 = _mm256_extractf128_si256( i1, 1);
  815. __m128i ni4 = _mm256_castsi256_si128( i2 );
  816. __m128i ni5 = _mm256_extractf128_si256( i2, 1);
  817. __m128i ni6 = _mm256_castsi256_si128( i3 );
  818. __m128i ni7 = _mm256_extractf128_si256( i3, 1);
  819. // Compute the sum of the quants and set y[i].s
  820. const __m128i s0 = _mm_add_epi32(_mm_add_epi32(ni0, ni1), _mm_add_epi32(ni2, ni3));
  821. const __m128i s1 = _mm_add_epi32(_mm_add_epi32(ni4, ni5), _mm_add_epi32(ni6, ni7));
  822. y[i].s = d * hsum_i32_4(_mm_add_epi32(s0, s1));
  823. // Convert int32 to int16
  824. ni0 = _mm_packs_epi32( ni0, ni1 );
  825. ni2 = _mm_packs_epi32( ni2, ni3 );
  826. ni4 = _mm_packs_epi32( ni4, ni5 );
  827. ni6 = _mm_packs_epi32( ni6, ni7 );
  828. // Convert int16 to int8
  829. ni0 = _mm_packs_epi16( ni0, ni2 );
  830. ni4 = _mm_packs_epi16( ni4, ni6 );
  831. _mm_storeu_si128((__m128i *)(y[i].qs + 0), ni0);
  832. _mm_storeu_si128((__m128i *)(y[i].qs + 16), ni4);
  833. #endif
  834. }
  835. #elif defined(__riscv_v_intrinsic)
  836. size_t vl = __riscv_vsetvl_e32m4(QK8_1);
  837. for (int i = 0; i < nb; i++) {
  838. // load elements
  839. vfloat32m4_t v_x = __riscv_vle32_v_f32m4(x+i*QK8_1, vl);
  840. vfloat32m4_t vfabs = __riscv_vfabs_v_f32m4(v_x, vl);
  841. vfloat32m1_t tmp = __riscv_vfmv_v_f_f32m1(0.0, vl);
  842. vfloat32m1_t vmax = __riscv_vfredmax_vs_f32m4_f32m1(vfabs, tmp, vl);
  843. float amax = __riscv_vfmv_f_s_f32m1_f32(vmax);
  844. const float d = amax / ((1 << 7) - 1);
  845. const float id = d ? 1.0f/d : 0.0f;
  846. y[i].d = d;
  847. vfloat32m4_t x0 = __riscv_vfmul_vf_f32m4(v_x, id, vl);
  848. // convert to integer
  849. vint16m2_t vi = __riscv_vfncvt_x_f_w_i16m2(x0, vl);
  850. vint8m1_t vs = __riscv_vncvt_x_x_w_i8m1(vi, vl);
  851. // store result
  852. __riscv_vse8_v_i8m1(y[i].qs , vs, vl);
  853. // compute sum for y[i].s
  854. vint16m1_t tmp2 = __riscv_vmv_v_x_i16m1(0, vl);
  855. vint16m1_t vwrs = __riscv_vwredsum_vs_i8m1_i16m1(vs, tmp2, vl);
  856. // set y[i].s
  857. int sum = __riscv_vmv_x_s_i16m1_i16(vwrs);
  858. y[i].s = sum*d;
  859. }
  860. #else
  861. GGML_UNUSED(nb);
  862. // scalar
  863. quantize_row_q8_1_reference(x, y, k);
  864. #endif
  865. }
  866. void dequantize_row_q4_0(const block_q4_0 * restrict x, float * restrict y, int k) {
  867. static const int qk = QK4_0;
  868. assert(k % qk == 0);
  869. const int nb = k / qk;
  870. for (int i = 0; i < nb; i++) {
  871. const float d = GGML_FP16_TO_FP32(x[i].d);
  872. for (int j = 0; j < qk/2; ++j) {
  873. const int x0 = (x[i].qs[j] & 0x0F) - 8;
  874. const int x1 = (x[i].qs[j] >> 4) - 8;
  875. y[i*qk + j + 0 ] = x0*d;
  876. y[i*qk + j + qk/2] = x1*d;
  877. }
  878. }
  879. }
  880. void dequantize_row_q4_1(const block_q4_1 * restrict x, float * restrict y, int k) {
  881. static const int qk = QK4_1;
  882. assert(k % qk == 0);
  883. const int nb = k / qk;
  884. for (int i = 0; i < nb; i++) {
  885. const float d = GGML_FP16_TO_FP32(x[i].d);
  886. const float m = GGML_FP16_TO_FP32(x[i].m);
  887. for (int j = 0; j < qk/2; ++j) {
  888. const int x0 = (x[i].qs[j] & 0x0F);
  889. const int x1 = (x[i].qs[j] >> 4);
  890. y[i*qk + j + 0 ] = x0*d + m;
  891. y[i*qk + j + qk/2] = x1*d + m;
  892. }
  893. }
  894. }
  895. void dequantize_row_q5_0(const block_q5_0 * restrict x, float * restrict y, int k) {
  896. static const int qk = QK5_0;
  897. assert(k % qk == 0);
  898. const int nb = k / qk;
  899. for (int i = 0; i < nb; i++) {
  900. const float d = GGML_FP16_TO_FP32(x[i].d);
  901. uint32_t qh;
  902. memcpy(&qh, x[i].qh, sizeof(qh));
  903. for (int j = 0; j < qk/2; ++j) {
  904. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  905. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  906. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  907. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  908. y[i*qk + j + 0 ] = x0*d;
  909. y[i*qk + j + qk/2] = x1*d;
  910. }
  911. }
  912. }
  913. void dequantize_row_q5_1(const block_q5_1 * restrict x, float * restrict y, int k) {
  914. static const int qk = QK5_1;
  915. assert(k % qk == 0);
  916. const int nb = k / qk;
  917. for (int i = 0; i < nb; i++) {
  918. const float d = GGML_FP16_TO_FP32(x[i].d);
  919. const float m = GGML_FP16_TO_FP32(x[i].m);
  920. uint32_t qh;
  921. memcpy(&qh, x[i].qh, sizeof(qh));
  922. for (int j = 0; j < qk/2; ++j) {
  923. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  924. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  925. const int x0 = (x[i].qs[j] & 0x0F) | xh_0;
  926. const int x1 = (x[i].qs[j] >> 4) | xh_1;
  927. y[i*qk + j + 0 ] = x0*d + m;
  928. y[i*qk + j + qk/2] = x1*d + m;
  929. }
  930. }
  931. }
  932. void dequantize_row_q8_0(const block_q8_0 * restrict x, float * restrict y, int k) {
  933. static const int qk = QK8_0;
  934. assert(k % qk == 0);
  935. const int nb = k / qk;
  936. for (int i = 0; i < nb; i++) {
  937. const float d = GGML_FP16_TO_FP32(x[i].d);
  938. for (int j = 0; j < qk; ++j) {
  939. y[i*qk + j] = x[i].qs[j]*d;
  940. }
  941. }
  942. }
  943. //
  944. // 2-6 bit quantization in super-blocks
  945. //
  946. //
  947. // ===================== Helper functions
  948. //
  949. static inline int nearest_int(float fval) {
  950. assert(fval <= 4194303.f);
  951. float val = fval + 12582912.f;
  952. int i; memcpy(&i, &val, sizeof(int));
  953. return (i & 0x007fffff) - 0x00400000;
  954. }
  955. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type) {
  956. float max = 0;
  957. float amax = 0;
  958. for (int i = 0; i < n; ++i) {
  959. float ax = fabsf(x[i]);
  960. if (ax > amax) { amax = ax; max = x[i]; }
  961. }
  962. if (amax < 1e-30f) { // all zero
  963. for (int i = 0; i < n; ++i) {
  964. L[i] = 0;
  965. }
  966. return 0.f;
  967. }
  968. float iscale = -nmax / max;
  969. if (rmse_type == 0) {
  970. for (int i = 0; i < n; ++i) {
  971. int l = nearest_int(iscale * x[i]);
  972. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  973. }
  974. return 1/iscale;
  975. }
  976. bool return_early = false;
  977. if (rmse_type < 0) {
  978. rmse_type = -rmse_type;
  979. return_early = true;
  980. }
  981. int weight_type = rmse_type%2;
  982. float sumlx = 0;
  983. float suml2 = 0;
  984. for (int i = 0; i < n; ++i) {
  985. int l = nearest_int(iscale * x[i]);
  986. l = MAX(-nmax, MIN(nmax-1, l));
  987. L[i] = l + nmax;
  988. float w = weight_type == 1 ? x[i] * x[i] : 1;
  989. sumlx += w*x[i]*l;
  990. suml2 += w*l*l;
  991. }
  992. float scale = sumlx/suml2;
  993. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  994. float best = scale * sumlx;
  995. for (int is = -9; is <= 9; ++is) {
  996. if (is == 0) {
  997. continue;
  998. }
  999. iscale = -(nmax + 0.1f*is) / max;
  1000. sumlx = suml2 = 0;
  1001. for (int i = 0; i < n; ++i) {
  1002. int l = nearest_int(iscale * x[i]);
  1003. l = MAX(-nmax, MIN(nmax-1, l));
  1004. float w = weight_type == 1 ? x[i] * x[i] : 1;
  1005. sumlx += w*x[i]*l;
  1006. suml2 += w*l*l;
  1007. }
  1008. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  1009. for (int i = 0; i < n; ++i) {
  1010. int l = nearest_int(iscale * x[i]);
  1011. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  1012. }
  1013. scale = sumlx/suml2; best = scale*sumlx;
  1014. }
  1015. }
  1016. return scale;
  1017. }
  1018. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  1019. float max = 0;
  1020. float amax = 0;
  1021. for (int i = 0; i < n; ++i) {
  1022. float ax = fabsf(x[i]);
  1023. if (ax > amax) { amax = ax; max = x[i]; }
  1024. }
  1025. if (!amax) { // all zero
  1026. for (int i = 0; i < n; ++i) { L[i] = 0; }
  1027. return 0.f;
  1028. }
  1029. float iscale = -nmax / max;
  1030. if (do_rmse) {
  1031. float sumlx = 0;
  1032. float suml2 = 0;
  1033. for (int i = 0; i < n; ++i) {
  1034. int l = nearest_int(iscale * x[i]);
  1035. l = MAX(-nmax, MIN(nmax-1, l));
  1036. L[i] = l;
  1037. float w = x[i]*x[i];
  1038. sumlx += w*x[i]*l;
  1039. suml2 += w*l*l;
  1040. }
  1041. for (int itry = 0; itry < 5; ++itry) {
  1042. int n_changed = 0;
  1043. for (int i = 0; i < n; ++i) {
  1044. float w = x[i]*x[i];
  1045. float slx = sumlx - w*x[i]*L[i];
  1046. if (slx > 0) {
  1047. float sl2 = suml2 - w*L[i]*L[i];
  1048. int new_l = nearest_int(x[i] * sl2 / slx);
  1049. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  1050. if (new_l != L[i]) {
  1051. slx += w*x[i]*new_l;
  1052. sl2 += w*new_l*new_l;
  1053. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  1054. L[i] = new_l; sumlx = slx; suml2 = sl2;
  1055. ++n_changed;
  1056. }
  1057. }
  1058. }
  1059. }
  1060. if (!n_changed) {
  1061. break;
  1062. }
  1063. }
  1064. for (int i = 0; i < n; ++i) {
  1065. L[i] += nmax;
  1066. }
  1067. return sumlx / suml2;
  1068. }
  1069. for (int i = 0; i < n; ++i) {
  1070. int l = nearest_int(iscale * x[i]);
  1071. l = MAX(-nmax, MIN(nmax-1, l));
  1072. L[i] = l + nmax;
  1073. }
  1074. return 1/iscale;
  1075. }
  1076. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  1077. int ntry, float alpha) {
  1078. float min = x[0];
  1079. float max = x[0];
  1080. for (int i = 1; i < n; ++i) {
  1081. if (x[i] < min) min = x[i];
  1082. if (x[i] > max) max = x[i];
  1083. }
  1084. if (max == min) {
  1085. for (int i = 0; i < n; ++i) L[i] = 0;
  1086. *the_min = 0;
  1087. return 0.f;
  1088. }
  1089. if (min > 0) min = 0;
  1090. float iscale = nmax/(max - min);
  1091. float scale = 1/iscale;
  1092. for (int itry = 0; itry < ntry; ++itry) {
  1093. float sumlx = 0; int suml2 = 0;
  1094. bool did_change = false;
  1095. for (int i = 0; i < n; ++i) {
  1096. int l = nearest_int(iscale*(x[i] - min));
  1097. l = MAX(0, MIN(nmax, l));
  1098. if (l != L[i]) {
  1099. L[i] = l;
  1100. did_change = true;
  1101. }
  1102. sumlx += (x[i] - min)*l;
  1103. suml2 += l*l;
  1104. }
  1105. scale = sumlx/suml2;
  1106. float sum = 0;
  1107. for (int i = 0; i < n; ++i) {
  1108. sum += x[i] - scale*L[i];
  1109. }
  1110. min = alpha*min + (1 - alpha)*sum/n;
  1111. if (min > 0) min = 0;
  1112. iscale = 1/scale;
  1113. if (!did_change) break;
  1114. }
  1115. *the_min = -min;
  1116. return scale;
  1117. }
  1118. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  1119. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  1120. float rmin, float rdelta, int nstep, bool use_mad) {
  1121. float min = x[0];
  1122. float max = x[0];
  1123. float sum_w = weights[0];
  1124. float sum_x = sum_w * x[0];
  1125. #ifdef HAVE_BUGGY_APPLE_LINKER
  1126. // use 'volatile' to prevent unroll and work around a bug in Apple ld64 1015.7
  1127. for (volatile int i = 1; i < n; ++i) {
  1128. #else
  1129. for (int i = 1; i < n; ++i) {
  1130. #endif
  1131. if (x[i] < min) min = x[i];
  1132. if (x[i] > max) max = x[i];
  1133. float w = weights[i];
  1134. sum_w += w;
  1135. sum_x += w * x[i];
  1136. }
  1137. if (min > 0) min = 0;
  1138. if (max == min) {
  1139. for (int i = 0; i < n; ++i) L[i] = 0;
  1140. *the_min = -min;
  1141. return 0.f;
  1142. }
  1143. float iscale = nmax/(max - min);
  1144. float scale = 1/iscale;
  1145. float best_mad = 0;
  1146. for (int i = 0; i < n; ++i) {
  1147. int l = nearest_int(iscale*(x[i] - min));
  1148. L[i] = MAX(0, MIN(nmax, l));
  1149. float diff = scale * L[i] + min - x[i];
  1150. diff = use_mad ? fabsf(diff) : diff * diff;
  1151. float w = weights[i];
  1152. best_mad += w * diff;
  1153. }
  1154. if (nstep < 1) {
  1155. *the_min = -min;
  1156. return scale;
  1157. }
  1158. for (int is = 0; is <= nstep; ++is) {
  1159. iscale = (rmin + rdelta*is + nmax)/(max - min);
  1160. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  1161. for (int i = 0; i < n; ++i) {
  1162. int l = nearest_int(iscale*(x[i] - min));
  1163. l = MAX(0, MIN(nmax, l));
  1164. Laux[i] = l;
  1165. float w = weights[i];
  1166. sum_l += w*l;
  1167. sum_l2 += w*l*l;
  1168. sum_xl += w*l*x[i];
  1169. }
  1170. float D = sum_w * sum_l2 - sum_l * sum_l;
  1171. if (D > 0) {
  1172. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  1173. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  1174. if (this_min > 0) {
  1175. this_min = 0;
  1176. this_scale = sum_xl / sum_l2;
  1177. }
  1178. float mad = 0;
  1179. for (int i = 0; i < n; ++i) {
  1180. float diff = this_scale * Laux[i] + this_min - x[i];
  1181. diff = use_mad ? fabsf(diff) : diff * diff;
  1182. float w = weights[i];
  1183. mad += w * diff;
  1184. }
  1185. if (mad < best_mad) {
  1186. for (int i = 0; i < n; ++i) {
  1187. L[i] = Laux[i];
  1188. }
  1189. best_mad = mad;
  1190. scale = this_scale;
  1191. min = this_min;
  1192. }
  1193. }
  1194. }
  1195. *the_min = -min;
  1196. return scale;
  1197. }
  1198. #if QK_K == 256
  1199. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  1200. if (j < 4) {
  1201. *d = q[j] & 63; *m = q[j + 4] & 63;
  1202. } else {
  1203. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  1204. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  1205. }
  1206. }
  1207. #endif
  1208. //========================- 2-bit (de)-quantization
  1209. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  1210. assert(k % QK_K == 0);
  1211. const int nb = k / QK_K;
  1212. uint8_t L[QK_K];
  1213. uint8_t Laux[16];
  1214. float weights[16];
  1215. float mins[QK_K/16];
  1216. float scales[QK_K/16];
  1217. const float q4scale = 15.f;
  1218. for (int i = 0; i < nb; i++) {
  1219. float max_scale = 0; // as we are deducting the min, scales are always positive
  1220. float max_min = 0;
  1221. for (int j = 0; j < QK_K/16; ++j) {
  1222. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  1223. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  1224. float scale = scales[j];
  1225. if (scale > max_scale) {
  1226. max_scale = scale;
  1227. }
  1228. float min = mins[j];
  1229. if (min > max_min) {
  1230. max_min = min;
  1231. }
  1232. }
  1233. if (max_scale > 0) {
  1234. float iscale = q4scale/max_scale;
  1235. for (int j = 0; j < QK_K/16; ++j) {
  1236. int l = nearest_int(iscale*scales[j]);
  1237. y[i].scales[j] = l;
  1238. }
  1239. y[i].d = GGML_FP32_TO_FP16(max_scale/q4scale);
  1240. } else {
  1241. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  1242. y[i].d = GGML_FP32_TO_FP16(0.f);
  1243. }
  1244. if (max_min > 0) {
  1245. float iscale = q4scale/max_min;
  1246. for (int j = 0; j < QK_K/16; ++j) {
  1247. int l = nearest_int(iscale*mins[j]);
  1248. y[i].scales[j] |= (l << 4);
  1249. }
  1250. y[i].dmin = GGML_FP32_TO_FP16(max_min/q4scale);
  1251. } else {
  1252. y[i].dmin = GGML_FP32_TO_FP16(0.f);
  1253. }
  1254. for (int j = 0; j < QK_K/16; ++j) {
  1255. const float d = GGML_FP16_TO_FP32(y[i].d) * (y[i].scales[j] & 0xF);
  1256. if (!d) continue;
  1257. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * (y[i].scales[j] >> 4);
  1258. for (int ii = 0; ii < 16; ++ii) {
  1259. int l = nearest_int((x[16*j + ii] + dm)/d);
  1260. l = MAX(0, MIN(3, l));
  1261. L[16*j + ii] = l;
  1262. }
  1263. }
  1264. #if QK_K == 256
  1265. for (int j = 0; j < QK_K; j += 128) {
  1266. for (int l = 0; l < 32; ++l) {
  1267. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1268. }
  1269. }
  1270. #else
  1271. for (int l = 0; l < 16; ++l) {
  1272. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1273. }
  1274. #endif
  1275. x += QK_K;
  1276. }
  1277. }
  1278. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  1279. assert(k % QK_K == 0);
  1280. const int nb = k / QK_K;
  1281. for (int i = 0; i < nb; i++) {
  1282. const float d = GGML_FP16_TO_FP32(x[i].d);
  1283. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1284. const uint8_t * q = x[i].qs;
  1285. #if QK_K == 256
  1286. int is = 0;
  1287. float dl, ml;
  1288. for (int n = 0; n < QK_K; n += 128) {
  1289. int shift = 0;
  1290. for (int j = 0; j < 4; ++j) {
  1291. uint8_t sc = x[i].scales[is++];
  1292. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1293. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  1294. sc = x[i].scales[is++];
  1295. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  1296. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  1297. shift += 2;
  1298. }
  1299. q += 32;
  1300. }
  1301. #else
  1302. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  1303. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  1304. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  1305. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  1306. for (int l = 0; l < 16; ++l) {
  1307. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  1308. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  1309. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  1310. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  1311. }
  1312. y += QK_K;
  1313. #endif
  1314. }
  1315. }
  1316. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  1317. quantize_row_q2_K_reference(x, vy, k);
  1318. }
  1319. size_t ggml_quantize_q2_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1320. (void)hist; // TODO: collect histograms
  1321. for (int j = 0; j < n; j += k) {
  1322. block_q2_K * restrict y = (block_q2_K *)dst + j/QK_K;
  1323. quantize_row_q2_K_reference(src + j, y, k);
  1324. }
  1325. return (n/QK_K*sizeof(block_q2_K));
  1326. }
  1327. //========================= 3-bit (de)-quantization
  1328. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  1329. assert(k % QK_K == 0);
  1330. const int nb = k / QK_K;
  1331. int8_t L[QK_K];
  1332. float scales[QK_K / 16];
  1333. for (int i = 0; i < nb; i++) {
  1334. float max_scale = 0;
  1335. float amax = 0;
  1336. for (int j = 0; j < QK_K/16; ++j) {
  1337. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  1338. float scale = fabsf(scales[j]);
  1339. if (scale > amax) {
  1340. amax = scale; max_scale = scales[j];
  1341. }
  1342. }
  1343. #if QK_K == 256
  1344. memset(y[i].scales, 0, 12);
  1345. if (max_scale) {
  1346. float iscale = -32.f/max_scale;
  1347. for (int j = 0; j < QK_K/16; ++j) {
  1348. int8_t l = nearest_int(iscale*scales[j]);
  1349. l = MAX(-32, MIN(31, l)) + 32;
  1350. if (j < 8) {
  1351. y[i].scales[j] = l & 0xF;
  1352. } else {
  1353. y[i].scales[j-8] |= ((l & 0xF) << 4);
  1354. }
  1355. l >>= 4;
  1356. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  1357. }
  1358. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1359. } else {
  1360. y[i].d = GGML_FP32_TO_FP16(0.f);
  1361. }
  1362. int8_t sc;
  1363. for (int j = 0; j < QK_K/16; ++j) {
  1364. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  1365. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  1366. float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1367. if (!d) {
  1368. continue;
  1369. }
  1370. for (int ii = 0; ii < 16; ++ii) {
  1371. int l = nearest_int(x[16*j + ii]/d);
  1372. l = MAX(-4, MIN(3, l));
  1373. L[16*j + ii] = l + 4;
  1374. }
  1375. }
  1376. #else
  1377. if (max_scale) {
  1378. float iscale = -8.f/max_scale;
  1379. for (int j = 0; j < QK_K/16; j+=2) {
  1380. int l1 = nearest_int(iscale*scales[j]);
  1381. l1 = 8 + MAX(-8, MIN(7, l1));
  1382. int l2 = nearest_int(iscale*scales[j+1]);
  1383. l2 = 8 + MAX(-8, MIN(7, l2));
  1384. y[i].scales[j/2] = l1 | (l2 << 4);
  1385. }
  1386. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1387. } else {
  1388. for (int j = 0; j < QK_K/16; j+=2) {
  1389. y[i].scales[j/2] = 0;
  1390. }
  1391. y[i].d = GGML_FP32_TO_FP16(0.f);
  1392. }
  1393. for (int j = 0; j < QK_K/16; ++j) {
  1394. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  1395. float d = GGML_FP16_TO_FP32(y[i].d) * (s - 8);
  1396. if (!d) {
  1397. continue;
  1398. }
  1399. for (int ii = 0; ii < 16; ++ii) {
  1400. int l = nearest_int(x[16*j + ii]/d);
  1401. l = MAX(-4, MIN(3, l));
  1402. L[16*j + ii] = l + 4;
  1403. }
  1404. }
  1405. #endif
  1406. memset(y[i].hmask, 0, QK_K/8);
  1407. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  1408. int m = 0;
  1409. uint8_t hm = 1;
  1410. for (int j = 0; j < QK_K; ++j) {
  1411. if (L[j] > 3) {
  1412. y[i].hmask[m] |= hm;
  1413. L[j] -= 4;
  1414. }
  1415. if (++m == QK_K/8) {
  1416. m = 0; hm <<= 1;
  1417. }
  1418. }
  1419. #if QK_K == 256
  1420. for (int j = 0; j < QK_K; j += 128) {
  1421. for (int l = 0; l < 32; ++l) {
  1422. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  1423. }
  1424. }
  1425. #else
  1426. for (int l = 0; l < 16; ++l) {
  1427. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  1428. }
  1429. #endif
  1430. x += QK_K;
  1431. }
  1432. }
  1433. #if QK_K == 256
  1434. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1435. assert(k % QK_K == 0);
  1436. const int nb = k / QK_K;
  1437. const uint32_t kmask1 = 0x03030303;
  1438. const uint32_t kmask2 = 0x0f0f0f0f;
  1439. uint32_t aux[4];
  1440. const int8_t * scales = (const int8_t*)aux;
  1441. for (int i = 0; i < nb; i++) {
  1442. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1443. const uint8_t * restrict q = x[i].qs;
  1444. const uint8_t * restrict hm = x[i].hmask;
  1445. uint8_t m = 1;
  1446. memcpy(aux, x[i].scales, 12);
  1447. uint32_t tmp = aux[2];
  1448. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  1449. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  1450. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  1451. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  1452. int is = 0;
  1453. float dl;
  1454. for (int n = 0; n < QK_K; n += 128) {
  1455. int shift = 0;
  1456. for (int j = 0; j < 4; ++j) {
  1457. dl = d_all * (scales[is++] - 32);
  1458. for (int l = 0; l < 16; ++l) {
  1459. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  1460. }
  1461. dl = d_all * (scales[is++] - 32);
  1462. for (int l = 0; l < 16; ++l) {
  1463. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  1464. }
  1465. shift += 2;
  1466. m <<= 1;
  1467. }
  1468. q += 32;
  1469. }
  1470. }
  1471. }
  1472. #else
  1473. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  1474. assert(k % QK_K == 0);
  1475. assert(QK_K == 64);
  1476. const int nb = k / QK_K;
  1477. for (int i = 0; i < nb; i++) {
  1478. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  1479. const uint8_t * restrict q = x[i].qs;
  1480. const uint8_t * restrict hm = x[i].hmask;
  1481. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  1482. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  1483. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  1484. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  1485. for (int l=0; l<8; ++l) {
  1486. uint8_t h = hm[l];
  1487. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  1488. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  1489. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  1490. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  1491. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  1492. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  1493. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  1494. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  1495. }
  1496. y += QK_K;
  1497. }
  1498. }
  1499. #endif
  1500. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  1501. quantize_row_q3_K_reference(x, vy, k);
  1502. }
  1503. size_t ggml_quantize_q3_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1504. (void)hist; // TODO: collect histograms
  1505. for (int j = 0; j < n; j += k) {
  1506. block_q3_K * restrict y = (block_q3_K *)dst + j/QK_K;
  1507. quantize_row_q3_K_reference(src + j, y, k);
  1508. }
  1509. return (n/QK_K*sizeof(block_q3_K));
  1510. }
  1511. // ====================== 4-bit (de)-quantization
  1512. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  1513. assert(k % QK_K == 0);
  1514. const int nb = k / QK_K;
  1515. uint8_t L[QK_K];
  1516. uint8_t Laux[32];
  1517. float weights[32];
  1518. float mins[QK_K/32];
  1519. float scales[QK_K/32];
  1520. for (int i = 0; i < nb; i++) {
  1521. float max_scale = 0; // as we are deducting the min, scales are always positive
  1522. float max_min = 0;
  1523. for (int j = 0; j < QK_K/32; ++j) {
  1524. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1525. float sum_x2 = 0;
  1526. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1527. float av_x = sqrtf(sum_x2/32);
  1528. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1529. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  1530. float scale = scales[j];
  1531. if (scale > max_scale) {
  1532. max_scale = scale;
  1533. }
  1534. float min = mins[j];
  1535. if (min > max_min) {
  1536. max_min = min;
  1537. }
  1538. }
  1539. #if QK_K == 256
  1540. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1541. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1542. for (int j = 0; j < QK_K/32; ++j) {
  1543. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1544. uint8_t lm = nearest_int(inv_min*mins[j]);
  1545. ls = MIN(63, ls);
  1546. lm = MIN(63, lm);
  1547. if (j < 4) {
  1548. y[i].scales[j] = ls;
  1549. y[i].scales[j+4] = lm;
  1550. } else {
  1551. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1552. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1553. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1554. }
  1555. }
  1556. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1557. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1558. uint8_t sc, m;
  1559. for (int j = 0; j < QK_K/32; ++j) {
  1560. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1561. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1562. if (!d) continue;
  1563. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1564. for (int ii = 0; ii < 32; ++ii) {
  1565. int l = nearest_int((x[32*j + ii] + dm)/d);
  1566. l = MAX(0, MIN(15, l));
  1567. L[32*j + ii] = l;
  1568. }
  1569. }
  1570. #else
  1571. const float s_factor = 15.f;
  1572. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  1573. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  1574. int d1 = nearest_int(inv_scale*scales[0]);
  1575. int m1 = nearest_int(inv_min*mins[0]);
  1576. int d2 = nearest_int(inv_scale*scales[1]);
  1577. int m2 = nearest_int(inv_min*mins[1]);
  1578. y[i].scales[0] = d1 | (m1 << 4);
  1579. y[i].scales[1] = d2 | (m2 << 4);
  1580. y[i].d[0] = GGML_FP32_TO_FP16(max_scale/s_factor);
  1581. y[i].d[1] = GGML_FP32_TO_FP16(max_min/s_factor);
  1582. float sumlx = 0;
  1583. int suml2 = 0;
  1584. for (int j = 0; j < QK_K/32; ++j) {
  1585. const uint8_t sd = y[i].scales[j] & 0xF;
  1586. const uint8_t sm = y[i].scales[j] >> 4;
  1587. const float d = GGML_FP16_TO_FP32(y[i].d[0]) * sd;
  1588. if (!d) continue;
  1589. const float m = GGML_FP16_TO_FP32(y[i].d[1]) * sm;
  1590. for (int ii = 0; ii < 32; ++ii) {
  1591. int l = nearest_int((x[32*j + ii] + m)/d);
  1592. l = MAX(0, MIN(15, l));
  1593. L[32*j + ii] = l;
  1594. sumlx += (x[32*j + ii] + m)*l*sd;
  1595. suml2 += l*l*sd*sd;
  1596. }
  1597. }
  1598. if (suml2) {
  1599. y[i].d[0] = GGML_FP32_TO_FP16(sumlx/suml2);
  1600. }
  1601. #endif
  1602. uint8_t * q = y[i].qs;
  1603. for (int j = 0; j < QK_K; j += 64) {
  1604. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  1605. q += 32;
  1606. }
  1607. x += QK_K;
  1608. }
  1609. }
  1610. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  1611. assert(k % QK_K == 0);
  1612. const int nb = k / QK_K;
  1613. for (int i = 0; i < nb; i++) {
  1614. const uint8_t * q = x[i].qs;
  1615. #if QK_K == 256
  1616. const float d = GGML_FP16_TO_FP32(x[i].d);
  1617. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1618. int is = 0;
  1619. uint8_t sc, m;
  1620. for (int j = 0; j < QK_K; j += 64) {
  1621. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1622. const float d1 = d * sc; const float m1 = min * m;
  1623. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1624. const float d2 = d * sc; const float m2 = min * m;
  1625. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  1626. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  1627. q += 32; is += 2;
  1628. }
  1629. #else
  1630. const float dall = GGML_FP16_TO_FP32(x[i].d[0]);
  1631. const float mall = GGML_FP16_TO_FP32(x[i].d[1]);
  1632. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  1633. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  1634. for (int l = 0; l < 32; ++l) {
  1635. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  1636. y[l+32] = d2 * (q[l] >> 4) - m2;
  1637. }
  1638. y += QK_K;
  1639. #endif
  1640. }
  1641. }
  1642. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  1643. assert(k % QK_K == 0);
  1644. block_q4_K * restrict y = vy;
  1645. quantize_row_q4_K_reference(x, y, k);
  1646. }
  1647. size_t ggml_quantize_q4_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1648. assert(k % QK_K == 0);
  1649. (void)hist; // TODO: collect histograms
  1650. for (int j = 0; j < n; j += k) {
  1651. block_q4_K * restrict y = (block_q4_K *)dst + j/QK_K;
  1652. quantize_row_q4_K_reference(src + j, y, k);
  1653. }
  1654. return (n/QK_K*sizeof(block_q4_K));
  1655. }
  1656. // ====================== 5-bit (de)-quantization
  1657. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  1658. assert(k % QK_K == 0);
  1659. const int nb = k / QK_K;
  1660. #if QK_K == 256
  1661. uint8_t L[QK_K];
  1662. float mins[QK_K/32];
  1663. float scales[QK_K/32];
  1664. float weights[32];
  1665. uint8_t Laux[32];
  1666. #else
  1667. int8_t L[QK_K];
  1668. float scales[QK_K/16];
  1669. #endif
  1670. for (int i = 0; i < nb; i++) {
  1671. #if QK_K == 256
  1672. float max_scale = 0; // as we are deducting the min, scales are always positive
  1673. float max_min = 0;
  1674. for (int j = 0; j < QK_K/32; ++j) {
  1675. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  1676. float sum_x2 = 0;
  1677. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  1678. float av_x = sqrtf(sum_x2/32);
  1679. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  1680. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  1681. float scale = scales[j];
  1682. if (scale > max_scale) {
  1683. max_scale = scale;
  1684. }
  1685. float min = mins[j];
  1686. if (min > max_min) {
  1687. max_min = min;
  1688. }
  1689. }
  1690. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  1691. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  1692. for (int j = 0; j < QK_K/32; ++j) {
  1693. uint8_t ls = nearest_int(inv_scale*scales[j]);
  1694. uint8_t lm = nearest_int(inv_min*mins[j]);
  1695. ls = MIN(63, ls);
  1696. lm = MIN(63, lm);
  1697. if (j < 4) {
  1698. y[i].scales[j] = ls;
  1699. y[i].scales[j+4] = lm;
  1700. } else {
  1701. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  1702. y[i].scales[j-4] |= ((ls >> 4) << 6);
  1703. y[i].scales[j-0] |= ((lm >> 4) << 6);
  1704. }
  1705. }
  1706. y[i].d = GGML_FP32_TO_FP16(max_scale/63.f);
  1707. y[i].dmin = GGML_FP32_TO_FP16(max_min/63.f);
  1708. uint8_t sc, m;
  1709. for (int j = 0; j < QK_K/32; ++j) {
  1710. get_scale_min_k4(j, y[i].scales, &sc, &m);
  1711. const float d = GGML_FP16_TO_FP32(y[i].d) * sc;
  1712. if (!d) continue;
  1713. const float dm = GGML_FP16_TO_FP32(y[i].dmin) * m;
  1714. for (int ii = 0; ii < 32; ++ii) {
  1715. int l = nearest_int((x[32*j + ii] + dm)/d);
  1716. l = MAX(0, MIN(31, l));
  1717. L[32*j + ii] = l;
  1718. }
  1719. }
  1720. uint8_t * restrict qh = y[i].qh;
  1721. uint8_t * restrict ql = y[i].qs;
  1722. memset(qh, 0, QK_K/8);
  1723. uint8_t m1 = 1, m2 = 2;
  1724. for (int n = 0; n < QK_K; n += 64) {
  1725. for (int j = 0; j < 32; ++j) {
  1726. int l1 = L[n + j];
  1727. if (l1 > 15) {
  1728. l1 -= 16; qh[j] |= m1;
  1729. }
  1730. int l2 = L[n + j + 32];
  1731. if (l2 > 15) {
  1732. l2 -= 16; qh[j] |= m2;
  1733. }
  1734. ql[j] = l1 | (l2 << 4);
  1735. }
  1736. m1 <<= 2; m2 <<= 2;
  1737. ql += 32;
  1738. }
  1739. #else
  1740. float max_scale = 0, amax = 0;
  1741. for (int j = 0; j < QK_K/16; ++j) {
  1742. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1);
  1743. float abs_scale = fabsf(scales[j]);
  1744. if (abs_scale > amax) {
  1745. amax = abs_scale;
  1746. max_scale = scales[j];
  1747. }
  1748. }
  1749. float iscale = -128.f/max_scale;
  1750. for (int j = 0; j < QK_K/16; ++j) {
  1751. int l = nearest_int(iscale*scales[j]);
  1752. y[i].scales[j] = MAX(-128, MIN(127, l));
  1753. }
  1754. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1755. for (int j = 0; j < QK_K/16; ++j) {
  1756. const float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  1757. if (!d) continue;
  1758. for (int ii = 0; ii < 16; ++ii) {
  1759. int l = nearest_int(x[16*j + ii]/d);
  1760. l = MAX(-16, MIN(15, l));
  1761. L[16*j + ii] = l + 16;
  1762. }
  1763. }
  1764. uint8_t * restrict qh = y[i].qh;
  1765. uint8_t * restrict ql = y[i].qs;
  1766. memset(qh, 0, QK_K/8);
  1767. for (int j = 0; j < 32; ++j) {
  1768. int jm = j%8;
  1769. int is = j/8;
  1770. int l1 = L[j];
  1771. if (l1 > 15) {
  1772. l1 -= 16; qh[jm] |= (1 << is);
  1773. }
  1774. int l2 = L[j + 32];
  1775. if (l2 > 15) {
  1776. l2 -= 16; qh[jm] |= (1 << (4 + is));
  1777. }
  1778. ql[j] = l1 | (l2 << 4);
  1779. }
  1780. #endif
  1781. x += QK_K;
  1782. }
  1783. }
  1784. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  1785. assert(k % QK_K == 0);
  1786. const int nb = k / QK_K;
  1787. for (int i = 0; i < nb; i++) {
  1788. const uint8_t * ql = x[i].qs;
  1789. const uint8_t * qh = x[i].qh;
  1790. #if QK_K == 256
  1791. const float d = GGML_FP16_TO_FP32(x[i].d);
  1792. const float min = GGML_FP16_TO_FP32(x[i].dmin);
  1793. int is = 0;
  1794. uint8_t sc, m;
  1795. uint8_t u1 = 1, u2 = 2;
  1796. for (int j = 0; j < QK_K; j += 64) {
  1797. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  1798. const float d1 = d * sc; const float m1 = min * m;
  1799. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  1800. const float d2 = d * sc; const float m2 = min * m;
  1801. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  1802. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  1803. ql += 32; is += 2;
  1804. u1 <<= 2; u2 <<= 2;
  1805. }
  1806. #else
  1807. float d = GGML_FP16_TO_FP32(x[i].d);
  1808. const int8_t * restrict s = x[i].scales;
  1809. for (int l = 0; l < 8; ++l) {
  1810. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  1811. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  1812. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  1813. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  1814. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  1815. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  1816. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  1817. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  1818. }
  1819. y += QK_K;
  1820. #endif
  1821. }
  1822. }
  1823. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  1824. assert(k % QK_K == 0);
  1825. block_q5_K * restrict y = vy;
  1826. quantize_row_q5_K_reference(x, y, k);
  1827. }
  1828. size_t ggml_quantize_q5_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  1829. assert(k % QK_K == 0);
  1830. (void)hist; // TODO: collect histograms
  1831. for (int j = 0; j < n; j += k) {
  1832. block_q5_K * restrict y = (block_q5_K *)dst + j/QK_K;
  1833. quantize_row_q5_K_reference(src + j, y, k);
  1834. }
  1835. return (n/QK_K*sizeof(block_q5_K));
  1836. }
  1837. // ====================== 6-bit (de)-quantization
  1838. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  1839. assert(k % QK_K == 0);
  1840. const int nb = k / QK_K;
  1841. int8_t L[QK_K];
  1842. float scales[QK_K/16];
  1843. for (int i = 0; i < nb; i++) {
  1844. float max_scale = 0;
  1845. float max_abs_scale = 0;
  1846. for (int ib = 0; ib < QK_K/16; ++ib) {
  1847. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1);
  1848. scales[ib] = scale;
  1849. const float abs_scale = fabsf(scale);
  1850. if (abs_scale > max_abs_scale) {
  1851. max_abs_scale = abs_scale;
  1852. max_scale = scale;
  1853. }
  1854. }
  1855. if (!max_abs_scale) {
  1856. memset(&y[i], 0, sizeof(block_q6_K));
  1857. y[i].d = GGML_FP32_TO_FP16(0.f);
  1858. x += QK_K;
  1859. continue;
  1860. }
  1861. float iscale = -128.f/max_scale;
  1862. y[i].d = GGML_FP32_TO_FP16(1/iscale);
  1863. for (int ib = 0; ib < QK_K/16; ++ib) {
  1864. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  1865. }
  1866. for (int j = 0; j < QK_K/16; ++j) {
  1867. float d = GGML_FP16_TO_FP32(y[i].d) * y[i].scales[j];
  1868. if (!d) {
  1869. continue;
  1870. }
  1871. for (int ii = 0; ii < 16; ++ii) {
  1872. int l = nearest_int(x[16*j + ii]/d);
  1873. l = MAX(-32, MIN(31, l));
  1874. L[16*j + ii] = l + 32;
  1875. }
  1876. }
  1877. uint8_t * restrict ql = y[i].ql;
  1878. uint8_t * restrict qh = y[i].qh;
  1879. #if QK_K == 256
  1880. for (int j = 0; j < QK_K; j += 128) {
  1881. for (int l = 0; l < 32; ++l) {
  1882. const uint8_t q1 = L[j + l + 0] & 0xF;
  1883. const uint8_t q2 = L[j + l + 32] & 0xF;
  1884. const uint8_t q3 = L[j + l + 64] & 0xF;
  1885. const uint8_t q4 = L[j + l + 96] & 0xF;
  1886. ql[l+ 0] = q1 | (q3 << 4);
  1887. ql[l+32] = q2 | (q4 << 4);
  1888. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  1889. }
  1890. ql += 64;
  1891. qh += 32;
  1892. }
  1893. #else
  1894. for (int l = 0; l < 32; ++l) {
  1895. const uint8_t q1 = L[l + 0] & 0xF;
  1896. const uint8_t q2 = L[l + 32] & 0xF;
  1897. ql[l] = q1 | (q2 << 4);
  1898. }
  1899. for (int l = 0; l < 16; ++l) {
  1900. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  1901. }
  1902. #endif
  1903. x += QK_K;
  1904. }
  1905. }
  1906. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  1907. assert(k % QK_K == 0);
  1908. const int nb = k / QK_K;
  1909. for (int i = 0; i < nb; i++) {
  1910. const float d = GGML_FP16_TO_FP32(x[i].d);
  1911. const uint8_t * restrict ql = x[i].ql;
  1912. const uint8_t * restrict qh = x[i].qh;
  1913. const int8_t * restrict sc = x[i].scales;
  1914. #if QK_K == 256
  1915. for (int n = 0; n < QK_K; n += 128) {
  1916. for (int l = 0; l < 32; ++l) {
  1917. int is = l/16;
  1918. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  1919. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  1920. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  1921. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  1922. y[l + 0] = d * sc[is + 0] * q1;
  1923. y[l + 32] = d * sc[is + 2] * q2;
  1924. y[l + 64] = d * sc[is + 4] * q3;
  1925. y[l + 96] = d * sc[is + 6] * q4;
  1926. }
  1927. y += 128;
  1928. ql += 64;
  1929. qh += 32;
  1930. sc += 8;
  1931. }
  1932. #else
  1933. for (int l = 0; l < 16; ++l) {
  1934. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  1935. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  1936. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  1937. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  1938. y[l+ 0] = d * sc[0] * q1;
  1939. y[l+16] = d * sc[1] * q2;
  1940. y[l+32] = d * sc[2] * q3;
  1941. y[l+48] = d * sc[3] * q4;
  1942. }
  1943. y += 64;
  1944. #endif
  1945. }
  1946. }
  1947. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  1948. assert(k % QK_K == 0);
  1949. block_q6_K * restrict y = vy;
  1950. quantize_row_q6_K_reference(x, y, k);
  1951. }
  1952. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist) {
  1953. assert(k % QK_K == 0);
  1954. (void)hist; // TODO: collect histograms
  1955. for (int j = 0; j < n; j += k) {
  1956. block_q6_K * restrict y = (block_q6_K *)dst + j/QK_K;
  1957. quantize_row_q6_K_reference(src + j, y, k);
  1958. }
  1959. return (n/QK_K*sizeof(block_q6_K));
  1960. }
  1961. //===================================== Q8_K ==============================================
  1962. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * 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. float max = 0;
  1967. float amax = 0;
  1968. for (int j = 0; j < QK_K; ++j) {
  1969. float ax = fabsf(x[j]);
  1970. if (ax > amax) {
  1971. amax = ax; max = x[j];
  1972. }
  1973. }
  1974. if (!amax) {
  1975. y[i].d = 0;
  1976. memset(y[i].qs, 0, QK_K);
  1977. x += QK_K;
  1978. continue;
  1979. }
  1980. const float iscale = -128.f/max;
  1981. for (int j = 0; j < QK_K; ++j) {
  1982. int v = nearest_int(iscale*x[j]);
  1983. y[i].qs[j] = MIN(127, v);
  1984. }
  1985. for (int j = 0; j < QK_K/16; ++j) {
  1986. int sum = 0;
  1987. for (int ii = 0; ii < 16; ++ii) {
  1988. sum += y[i].qs[j*16 + ii];
  1989. }
  1990. y[i].bsums[j] = sum;
  1991. }
  1992. y[i].d = 1/iscale;
  1993. x += QK_K;
  1994. }
  1995. }
  1996. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  1997. assert(k % QK_K == 0);
  1998. const int nb = k / QK_K;
  1999. for (int i = 0; i < nb; i++) {
  2000. for (int j = 0; j < QK_K; ++j) {
  2001. *y++ = x[i].d * x[i].qs[j];
  2002. }
  2003. }
  2004. }
  2005. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  2006. quantize_row_q8_K_reference(x, y, k);
  2007. }
  2008. //===================================== Dot ptoducts =================================
  2009. //
  2010. // Helper functions
  2011. //
  2012. #if __AVX__ || __AVX2__ || __AVX512F__
  2013. // shuffles to pick the required scales in dot products
  2014. static inline __m256i get_scale_shuffle_q3k(int i) {
  2015. static const uint8_t k_shuffle[128] = {
  2016. 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,
  2017. 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,
  2018. 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,
  2019. 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,
  2020. };
  2021. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2022. }
  2023. static inline __m256i get_scale_shuffle_k4(int i) {
  2024. static const uint8_t k_shuffle[256] = {
  2025. 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,
  2026. 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,
  2027. 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,
  2028. 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,
  2029. 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,
  2030. 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,
  2031. 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,
  2032. 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
  2033. };
  2034. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  2035. }
  2036. static inline __m128i get_scale_shuffle(int i) {
  2037. static const uint8_t k_shuffle[128] = {
  2038. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  2039. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  2040. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  2041. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  2042. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  2043. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  2044. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  2045. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  2046. };
  2047. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  2048. }
  2049. #endif
  2050. void ggml_vec_dot_q4_0_q8_0(int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2051. const int qk = QK8_0;
  2052. const int nb = n / qk;
  2053. assert(n % qk == 0);
  2054. const block_q4_0 * restrict x = vx;
  2055. const block_q8_0 * restrict y = vy;
  2056. #if defined(__ARM_NEON)
  2057. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2058. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2059. assert(nb % 2 == 0); // TODO: handle odd nb
  2060. for (int i = 0; i < nb; i += 2) {
  2061. const block_q4_0 * restrict x0 = &x[i + 0];
  2062. const block_q4_0 * restrict x1 = &x[i + 1];
  2063. const block_q8_0 * restrict y0 = &y[i + 0];
  2064. const block_q8_0 * restrict y1 = &y[i + 1];
  2065. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2066. const int8x16_t s8b = vdupq_n_s8(0x8);
  2067. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2068. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2069. // 4-bit -> 8-bit
  2070. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2071. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2072. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2073. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2074. // sub 8
  2075. const int8x16_t v0_0ls = vsubq_s8(v0_0l, s8b);
  2076. const int8x16_t v0_0hs = vsubq_s8(v0_0h, s8b);
  2077. const int8x16_t v0_1ls = vsubq_s8(v0_1l, s8b);
  2078. const int8x16_t v0_1hs = vsubq_s8(v0_1h, s8b);
  2079. // load y
  2080. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2081. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2082. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2083. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2084. // dot product into int32x4_t
  2085. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0ls, v1_0l), v0_0hs, v1_0h);
  2086. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1ls, v1_1l), v0_1hs, v1_1h);
  2087. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2088. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2089. }
  2090. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2091. #elif defined(__AVX2__)
  2092. // Initialize accumulator with zeros
  2093. __m256 acc = _mm256_setzero_ps();
  2094. // Main loop
  2095. for (int i = 0; i < nb; ++i) {
  2096. /* Compute combined scale for the block */
  2097. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2098. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2099. // Now we have a vector with bytes in [ 0 .. 15 ] interval. Offset them into [ -8 .. +7 ] interval.
  2100. const __m256i off = _mm256_set1_epi8( 8 );
  2101. bx = _mm256_sub_epi8( bx, off );
  2102. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2103. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2104. /* Multiply q with scale and accumulate */
  2105. acc = _mm256_fmadd_ps( d, q, acc );
  2106. }
  2107. *s = hsum_float_8(acc);
  2108. #elif defined(__AVX__)
  2109. // Initialize accumulator with zeros
  2110. __m256 acc = _mm256_setzero_ps();
  2111. // Main loop
  2112. for (int i = 0; i < nb; ++i) {
  2113. // Compute combined scale for the block
  2114. const __m256 d = _mm256_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2115. const __m128i lowMask = _mm_set1_epi8(0xF);
  2116. const __m128i off = _mm_set1_epi8(8);
  2117. const __m128i tmp = _mm_loadu_si128((const __m128i *)x[i].qs);
  2118. __m128i bx = _mm_and_si128(lowMask, tmp);
  2119. __m128i by = _mm_loadu_si128((const __m128i *)y[i].qs);
  2120. bx = _mm_sub_epi8(bx, off);
  2121. const __m128i i32_0 = mul_sum_i8_pairs(bx, by);
  2122. bx = _mm_and_si128(lowMask, _mm_srli_epi64(tmp, 4));
  2123. by = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  2124. bx = _mm_sub_epi8(bx, off);
  2125. const __m128i i32_1 = mul_sum_i8_pairs(bx, by);
  2126. // Convert int32_t to float
  2127. __m256 p = _mm256_cvtepi32_ps(MM256_SET_M128I(i32_0, i32_1));
  2128. // Apply the scale, and accumulate
  2129. acc = _mm256_add_ps(_mm256_mul_ps( d, p ), acc);
  2130. }
  2131. *s = hsum_float_8(acc);
  2132. #elif defined(__SSSE3__)
  2133. // set constants
  2134. const __m128i lowMask = _mm_set1_epi8(0xF);
  2135. const __m128i off = _mm_set1_epi8(8);
  2136. // Initialize accumulator with zeros
  2137. __m128 acc_0 = _mm_setzero_ps();
  2138. __m128 acc_1 = _mm_setzero_ps();
  2139. __m128 acc_2 = _mm_setzero_ps();
  2140. __m128 acc_3 = _mm_setzero_ps();
  2141. // First round without accumulation
  2142. {
  2143. _mm_prefetch(&x[0] + sizeof(block_q4_0), _MM_HINT_T0);
  2144. _mm_prefetch(&y[0] + sizeof(block_q8_0), _MM_HINT_T0);
  2145. // Compute combined scale for the block 0 and 1
  2146. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[0].d) * GGML_FP16_TO_FP32(y[0].d) );
  2147. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[0].qs);
  2148. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  2149. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[0].qs);
  2150. bx_0 = _mm_sub_epi8(bx_0, off);
  2151. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  2152. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  2153. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[0].qs + 16));
  2154. bx_1 = _mm_sub_epi8(bx_1, off);
  2155. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  2156. _mm_prefetch(&x[1] + sizeof(block_q4_0), _MM_HINT_T0);
  2157. _mm_prefetch(&y[1] + sizeof(block_q8_0), _MM_HINT_T0);
  2158. // Compute combined scale for the block 2 and 3
  2159. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[1].d) * GGML_FP16_TO_FP32(y[1].d) );
  2160. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[1].qs);
  2161. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  2162. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[1].qs);
  2163. bx_2 = _mm_sub_epi8(bx_2, off);
  2164. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  2165. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  2166. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[1].qs + 16));
  2167. bx_3 = _mm_sub_epi8(bx_3, off);
  2168. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  2169. // Convert int32_t to float
  2170. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  2171. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  2172. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  2173. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  2174. // Apply the scale
  2175. acc_0 = _mm_mul_ps( d_0_1, p0 );
  2176. acc_1 = _mm_mul_ps( d_0_1, p1 );
  2177. acc_2 = _mm_mul_ps( d_2_3, p2 );
  2178. acc_3 = _mm_mul_ps( d_2_3, p3 );
  2179. }
  2180. assert(nb % 2 == 0); // TODO: handle odd nb
  2181. // Main loop
  2182. for (int i = 2; i < nb; i+=2) {
  2183. _mm_prefetch(&x[i] + sizeof(block_q4_0), _MM_HINT_T0);
  2184. _mm_prefetch(&y[i] + sizeof(block_q8_0), _MM_HINT_T0);
  2185. // Compute combined scale for the block 0 and 1
  2186. const __m128 d_0_1 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d) );
  2187. const __m128i tmp_0_1 = _mm_loadu_si128((const __m128i *)x[i].qs);
  2188. __m128i bx_0 = _mm_and_si128(lowMask, tmp_0_1);
  2189. __m128i by_0 = _mm_loadu_si128((const __m128i *)y[i].qs);
  2190. bx_0 = _mm_sub_epi8(bx_0, off);
  2191. const __m128i i32_0 = mul_sum_i8_pairs(bx_0, by_0);
  2192. __m128i bx_1 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_0_1, 4));
  2193. __m128i by_1 = _mm_loadu_si128((const __m128i *)(y[i].qs + 16));
  2194. bx_1 = _mm_sub_epi8(bx_1, off);
  2195. const __m128i i32_1 = mul_sum_i8_pairs(bx_1, by_1);
  2196. _mm_prefetch(&x[i] + 2 * sizeof(block_q4_0), _MM_HINT_T0);
  2197. _mm_prefetch(&y[i] + 2 * sizeof(block_q8_0), _MM_HINT_T0);
  2198. // Compute combined scale for the block 2 and 3
  2199. const __m128 d_2_3 = _mm_set1_ps( GGML_FP16_TO_FP32(x[i + 1].d) * GGML_FP16_TO_FP32(y[i + 1].d) );
  2200. const __m128i tmp_2_3 = _mm_loadu_si128((const __m128i *)x[i + 1].qs);
  2201. __m128i bx_2 = _mm_and_si128(lowMask, tmp_2_3);
  2202. __m128i by_2 = _mm_loadu_si128((const __m128i *)y[i + 1].qs);
  2203. bx_2 = _mm_sub_epi8(bx_2, off);
  2204. const __m128i i32_2 = mul_sum_i8_pairs(bx_2, by_2);
  2205. __m128i bx_3 = _mm_and_si128(lowMask, _mm_srli_epi64(tmp_2_3, 4));
  2206. __m128i by_3 = _mm_loadu_si128((const __m128i *)(y[i + 1].qs + 16));
  2207. bx_3 = _mm_sub_epi8(bx_3, off);
  2208. const __m128i i32_3 = mul_sum_i8_pairs(bx_3, by_3);
  2209. // Convert int32_t to float
  2210. __m128 p0 = _mm_cvtepi32_ps(i32_0);
  2211. __m128 p1 = _mm_cvtepi32_ps(i32_1);
  2212. __m128 p2 = _mm_cvtepi32_ps(i32_2);
  2213. __m128 p3 = _mm_cvtepi32_ps(i32_3);
  2214. // Apply the scale
  2215. __m128 p0_d = _mm_mul_ps( d_0_1, p0 );
  2216. __m128 p1_d = _mm_mul_ps( d_0_1, p1 );
  2217. __m128 p2_d = _mm_mul_ps( d_2_3, p2 );
  2218. __m128 p3_d = _mm_mul_ps( d_2_3, p3 );
  2219. // Acummulate
  2220. acc_0 = _mm_add_ps(p0_d, acc_0);
  2221. acc_1 = _mm_add_ps(p1_d, acc_1);
  2222. acc_2 = _mm_add_ps(p2_d, acc_2);
  2223. acc_3 = _mm_add_ps(p3_d, acc_3);
  2224. }
  2225. *s = hsum_float_4x4(acc_0, acc_1, acc_2, acc_3);
  2226. #elif defined(__riscv_v_intrinsic)
  2227. float sumf = 0.0;
  2228. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2229. for (int i = 0; i < nb; i++) {
  2230. // load elements
  2231. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2232. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2233. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2234. // mask and store lower part of x, and then upper part
  2235. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2236. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2237. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2238. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2239. // subtract offset
  2240. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 8, vl);
  2241. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 8, vl);
  2242. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2243. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2244. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2245. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2246. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2247. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2248. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  2249. }
  2250. *s = sumf;
  2251. #else
  2252. // scalar
  2253. float sumf = 0.0;
  2254. for (int i = 0; i < nb; i++) {
  2255. int sumi = 0;
  2256. for (int j = 0; j < qk/2; ++j) {
  2257. const int v0 = (x[i].qs[j] & 0x0F) - 8;
  2258. const int v1 = (x[i].qs[j] >> 4) - 8;
  2259. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  2260. }
  2261. sumf += sumi*GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d);
  2262. }
  2263. *s = sumf;
  2264. #endif
  2265. }
  2266. void ggml_vec_dot_q4_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2267. const int qk = QK8_1;
  2268. const int nb = n / qk;
  2269. assert(n % qk == 0);
  2270. const block_q4_1 * restrict x = vx;
  2271. const block_q8_1 * restrict y = vy;
  2272. // TODO: add WASM SIMD
  2273. #if defined(__ARM_NEON)
  2274. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2275. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2276. float summs = 0;
  2277. assert(nb % 2 == 0); // TODO: handle odd nb
  2278. for (int i = 0; i < nb; i += 2) {
  2279. const block_q4_1 * restrict x0 = &x[i + 0];
  2280. const block_q4_1 * restrict x1 = &x[i + 1];
  2281. const block_q8_1 * restrict y0 = &y[i + 0];
  2282. const block_q8_1 * restrict y1 = &y[i + 1];
  2283. summs += GGML_FP16_TO_FP32(x0->m) * y0->s + GGML_FP16_TO_FP32(x1->m) * y1->s;
  2284. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2285. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2286. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2287. // 4-bit -> 8-bit
  2288. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2289. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2290. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2291. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2292. // load y
  2293. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2294. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2295. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2296. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2297. // dot product into int32x4_t
  2298. const int32x4_t p_0 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_0l, v1_0l), v0_0h, v1_0h);
  2299. const int32x4_t p_1 = ggml_vdotq_s32(ggml_vdotq_s32(vdupq_n_s32(0), v0_1l, v1_1l), v0_1h, v1_1h);
  2300. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(p_0), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2301. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(p_1), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2302. }
  2303. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs;
  2304. #elif defined(__AVX2__) || defined(__AVX__)
  2305. // Initialize accumulator with zeros
  2306. __m256 acc = _mm256_setzero_ps();
  2307. float summs = 0;
  2308. // Main loop
  2309. for (int i = 0; i < nb; ++i) {
  2310. const float d0 = GGML_FP16_TO_FP32(x[i].d);
  2311. const float d1 = y[i].d;
  2312. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2313. const __m256 d0v = _mm256_set1_ps( d0 );
  2314. const __m256 d1v = _mm256_set1_ps( d1 );
  2315. // Compute combined scales
  2316. const __m256 d0d1 = _mm256_mul_ps( d0v, d1v );
  2317. // Load 16 bytes, and unpack 4 bit fields into bytes, making 32 bytes
  2318. const __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2319. const __m256i by = _mm256_loadu_si256( (const __m256i *)y[i].qs );
  2320. const __m256 xy = mul_sum_us8_pairs_float(bx, by);
  2321. // Accumulate d0*d1*x*y
  2322. #if defined(__AVX2__)
  2323. acc = _mm256_fmadd_ps( d0d1, xy, acc );
  2324. #else
  2325. acc = _mm256_add_ps( _mm256_mul_ps( d0d1, xy ), acc );
  2326. #endif
  2327. }
  2328. *s = hsum_float_8(acc) + summs;
  2329. #elif defined(__riscv_v_intrinsic)
  2330. float sumf = 0.0;
  2331. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2332. for (int i = 0; i < nb; i++) {
  2333. // load elements
  2334. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2335. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2336. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2337. // mask and store lower part of x, and then upper part
  2338. vuint8mf2_t x_a = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2339. vuint8mf2_t x_l = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2340. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2341. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2342. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2343. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2344. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2345. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2346. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2347. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2348. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2349. }
  2350. *s = sumf;
  2351. #else
  2352. // scalar
  2353. float sumf = 0.0;
  2354. for (int i = 0; i < nb; i++) {
  2355. int sumi = 0;
  2356. for (int j = 0; j < qk/2; ++j) {
  2357. const int v0 = (x[i].qs[j] & 0x0F);
  2358. const int v1 = (x[i].qs[j] >> 4);
  2359. sumi += (v0 * y[i].qs[j]) + (v1 * y[i].qs[j + qk/2]);
  2360. }
  2361. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2362. }
  2363. *s = sumf;
  2364. #endif
  2365. }
  2366. void ggml_vec_dot_q5_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2367. const int qk = QK8_0;
  2368. const int nb = n / qk;
  2369. assert(n % qk == 0);
  2370. assert(qk == QK5_0);
  2371. const block_q5_0 * restrict x = vx;
  2372. const block_q8_0 * restrict y = vy;
  2373. #if defined(__ARM_NEON)
  2374. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2375. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2376. uint32_t qh0;
  2377. uint32_t qh1;
  2378. uint64_t tmp0[4];
  2379. uint64_t tmp1[4];
  2380. assert(nb % 2 == 0); // TODO: handle odd nb
  2381. for (int i = 0; i < nb; i += 2) {
  2382. const block_q5_0 * restrict x0 = &x[i];
  2383. const block_q5_0 * restrict x1 = &x[i + 1];
  2384. const block_q8_0 * restrict y0 = &y[i];
  2385. const block_q8_0 * restrict y1 = &y[i + 1];
  2386. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2387. // extract the 5th bit via lookup table ((!b) << 4)
  2388. memcpy(&qh0, x0->qh, sizeof(qh0));
  2389. memcpy(&qh1, x1->qh, sizeof(qh1));
  2390. tmp0[0] = table_b2b_1[(qh0 >> 0) & 0xFF];
  2391. tmp0[1] = table_b2b_1[(qh0 >> 8) & 0xFF];
  2392. tmp0[2] = table_b2b_1[(qh0 >> 16) & 0xFF];
  2393. tmp0[3] = table_b2b_1[(qh0 >> 24) ];
  2394. tmp1[0] = table_b2b_1[(qh1 >> 0) & 0xFF];
  2395. tmp1[1] = table_b2b_1[(qh1 >> 8) & 0xFF];
  2396. tmp1[2] = table_b2b_1[(qh1 >> 16) & 0xFF];
  2397. tmp1[3] = table_b2b_1[(qh1 >> 24) ];
  2398. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  2399. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  2400. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  2401. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  2402. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2403. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2404. // 4-bit -> 8-bit
  2405. int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2406. int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2407. int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2408. int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2409. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  2410. const int8x16_t v0_0lf = vsubq_s8(v0_0l, qhl0);
  2411. const int8x16_t v0_0hf = vsubq_s8(v0_0h, qhh0);
  2412. const int8x16_t v0_1lf = vsubq_s8(v0_1l, qhl1);
  2413. const int8x16_t v0_1hf = vsubq_s8(v0_1h, qhh1);
  2414. // load y
  2415. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2416. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2417. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2418. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2419. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2420. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  2421. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2422. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2423. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  2424. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2425. }
  2426. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2427. #elif defined(__wasm_simd128__)
  2428. v128_t sumv = wasm_f32x4_splat(0.0f);
  2429. uint32_t qh;
  2430. uint64_t tmp[4];
  2431. // TODO: check if unrolling this is better
  2432. for (int i = 0; i < nb; ++i) {
  2433. const block_q5_0 * restrict x0 = &x[i];
  2434. const block_q8_0 * restrict y0 = &y[i];
  2435. const v128_t m4b = wasm_i8x16_splat(0x0F);
  2436. // extract the 5th bit
  2437. memcpy(&qh, x0->qh, sizeof(qh));
  2438. tmp[0] = table_b2b_1[(qh >> 0) & 0xFF];
  2439. tmp[1] = table_b2b_1[(qh >> 8) & 0xFF];
  2440. tmp[2] = table_b2b_1[(qh >> 16) & 0xFF];
  2441. tmp[3] = table_b2b_1[(qh >> 24) ];
  2442. const v128_t qhl = wasm_v128_load(tmp + 0);
  2443. const v128_t qhh = wasm_v128_load(tmp + 2);
  2444. const v128_t v0 = wasm_v128_load(x0->qs);
  2445. // 4-bit -> 8-bit
  2446. const v128_t v0l = wasm_v128_and (v0, m4b);
  2447. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  2448. // add high bit and sub 16 (equivalent to sub 0x10 when bit is zero)
  2449. const v128_t v0lf = wasm_i8x16_sub(v0l, qhl);
  2450. const v128_t v0hf = wasm_i8x16_sub(v0h, qhh);
  2451. // load y
  2452. const v128_t v1l = wasm_v128_load(y0->qs);
  2453. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  2454. // int8x16 -> int16x8
  2455. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  2456. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  2457. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  2458. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  2459. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  2460. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  2461. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  2462. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  2463. // dot product
  2464. sumv = wasm_f32x4_add(sumv, wasm_f32x4_mul(wasm_f32x4_convert_i32x4(
  2465. wasm_i32x4_add(
  2466. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  2467. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  2468. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  2469. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  2470. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * GGML_FP16_TO_FP32(y0->d))));
  2471. }
  2472. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  2473. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3);
  2474. #elif defined(__AVX2__)
  2475. // Initialize accumulator with zeros
  2476. __m256 acc = _mm256_setzero_ps();
  2477. // Main loop
  2478. for (int i = 0; i < nb; i++) {
  2479. /* Compute combined scale for the block */
  2480. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2481. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2482. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2483. bxhi = _mm256_andnot_si256(bxhi, _mm256_set1_epi8((char)0xF0));
  2484. bx = _mm256_or_si256(bx, bxhi);
  2485. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2486. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2487. /* Multiply q with scale and accumulate */
  2488. acc = _mm256_fmadd_ps(d, q, acc);
  2489. }
  2490. *s = hsum_float_8(acc);
  2491. #elif defined(__AVX__)
  2492. // Initialize accumulator with zeros
  2493. __m256 acc = _mm256_setzero_ps();
  2494. __m128i mask = _mm_set1_epi8((char)0xF0);
  2495. // Main loop
  2496. for (int i = 0; i < nb; i++) {
  2497. /* Compute combined scale for the block */
  2498. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2499. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2500. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2501. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  2502. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  2503. bxhil = _mm_andnot_si128(bxhil, mask);
  2504. bxhih = _mm_andnot_si128(bxhih, mask);
  2505. __m128i bxl = _mm256_castsi256_si128(bx);
  2506. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  2507. bxl = _mm_or_si128(bxl, bxhil);
  2508. bxh = _mm_or_si128(bxh, bxhih);
  2509. bx = MM256_SET_M128I(bxh, bxl);
  2510. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2511. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2512. /* Multiply q with scale and accumulate */
  2513. acc = _mm256_add_ps(_mm256_mul_ps(d, q), acc);
  2514. }
  2515. *s = hsum_float_8(acc);
  2516. #elif defined(__riscv_v_intrinsic)
  2517. float sumf = 0.0;
  2518. uint32_t qh;
  2519. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2520. // These temporary registers are for masking and shift operations
  2521. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  2522. vuint32m2_t vt_2 = __riscv_vsll_vv_u32m2(__riscv_vmv_v_x_u32m2(1, vl), vt_1, vl);
  2523. vuint32m2_t vt_3 = __riscv_vsll_vx_u32m2(vt_2, 16, vl);
  2524. vuint32m2_t vt_4 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  2525. for (int i = 0; i < nb; i++) {
  2526. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  2527. // ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  2528. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(vt_2, qh, vl);
  2529. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(xha_0, vt_1, vl);
  2530. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  2531. // ((qh & (1u << (j + 16))) >> (j + 12));
  2532. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(vt_3, qh, vl);
  2533. vuint32m2_t xhl_1 = __riscv_vsrl_vv_u32m2(xha_1, vt_4, vl);
  2534. // narrowing
  2535. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xhl_0, vl);
  2536. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  2537. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xhl_1, vl);
  2538. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  2539. // load
  2540. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2541. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2542. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2543. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2544. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2545. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  2546. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  2547. vint8mf2_t x_ai = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2548. vint8mf2_t x_li = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2549. vint8mf2_t v0 = __riscv_vsub_vx_i8mf2(x_ai, 16, vl);
  2550. vint8mf2_t v1 = __riscv_vsub_vx_i8mf2(x_li, 16, vl);
  2551. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2552. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2553. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2554. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2555. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2556. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2557. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  2558. }
  2559. *s = sumf;
  2560. #else
  2561. // scalar
  2562. float sumf = 0.0;
  2563. for (int i = 0; i < nb; i++) {
  2564. uint32_t qh;
  2565. memcpy(&qh, x[i].qh, sizeof(qh));
  2566. int sumi = 0;
  2567. for (int j = 0; j < qk/2; ++j) {
  2568. const uint8_t xh_0 = ((qh & (1u << (j + 0 ))) >> (j + 0 )) << 4;
  2569. const uint8_t xh_1 = ((qh & (1u << (j + 16))) >> (j + 12));
  2570. const int32_t x0 = ((x[i].qs[j] & 0x0F) | xh_0) - 16;
  2571. const int32_t x1 = ((x[i].qs[j] >> 4) | xh_1) - 16;
  2572. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  2573. }
  2574. sumf += (GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d)) * sumi;
  2575. }
  2576. *s = sumf;
  2577. #endif
  2578. }
  2579. void ggml_vec_dot_q5_1_q8_1(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2580. const int qk = QK8_1;
  2581. const int nb = n / qk;
  2582. assert(n % qk == 0);
  2583. assert(qk == QK5_1);
  2584. const block_q5_1 * restrict x = vx;
  2585. const block_q8_1 * restrict y = vy;
  2586. #if defined(__ARM_NEON)
  2587. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2588. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2589. float summs0 = 0.0f;
  2590. float summs1 = 0.0f;
  2591. uint32_t qh0;
  2592. uint32_t qh1;
  2593. uint64_t tmp0[4];
  2594. uint64_t tmp1[4];
  2595. assert(nb % 2 == 0); // TODO: handle odd nb
  2596. for (int i = 0; i < nb; i += 2) {
  2597. const block_q5_1 * restrict x0 = &x[i];
  2598. const block_q5_1 * restrict x1 = &x[i + 1];
  2599. const block_q8_1 * restrict y0 = &y[i];
  2600. const block_q8_1 * restrict y1 = &y[i + 1];
  2601. const uint8x16_t m4b = vdupq_n_u8(0x0F);
  2602. summs0 += GGML_FP16_TO_FP32(x0->m) * y0->s;
  2603. summs1 += GGML_FP16_TO_FP32(x1->m) * y1->s;
  2604. // extract the 5th bit via lookup table ((b) << 4)
  2605. memcpy(&qh0, x0->qh, sizeof(qh0));
  2606. memcpy(&qh1, x1->qh, sizeof(qh1));
  2607. tmp0[0] = table_b2b_0[(qh0 >> 0) & 0xFF];
  2608. tmp0[1] = table_b2b_0[(qh0 >> 8) & 0xFF];
  2609. tmp0[2] = table_b2b_0[(qh0 >> 16) & 0xFF];
  2610. tmp0[3] = table_b2b_0[(qh0 >> 24) ];
  2611. tmp1[0] = table_b2b_0[(qh1 >> 0) & 0xFF];
  2612. tmp1[1] = table_b2b_0[(qh1 >> 8) & 0xFF];
  2613. tmp1[2] = table_b2b_0[(qh1 >> 16) & 0xFF];
  2614. tmp1[3] = table_b2b_0[(qh1 >> 24) ];
  2615. const int8x16_t qhl0 = vld1q_s8((const int8_t *)(tmp0 + 0));
  2616. const int8x16_t qhh0 = vld1q_s8((const int8_t *)(tmp0 + 2));
  2617. const int8x16_t qhl1 = vld1q_s8((const int8_t *)(tmp1 + 0));
  2618. const int8x16_t qhh1 = vld1q_s8((const int8_t *)(tmp1 + 2));
  2619. const uint8x16_t v0_0 = vld1q_u8(x0->qs);
  2620. const uint8x16_t v0_1 = vld1q_u8(x1->qs);
  2621. // 4-bit -> 8-bit
  2622. const int8x16_t v0_0l = vreinterpretq_s8_u8(vandq_u8 (v0_0, m4b));
  2623. const int8x16_t v0_0h = vreinterpretq_s8_u8(vshrq_n_u8(v0_0, 4));
  2624. const int8x16_t v0_1l = vreinterpretq_s8_u8(vandq_u8 (v0_1, m4b));
  2625. const int8x16_t v0_1h = vreinterpretq_s8_u8(vshrq_n_u8(v0_1, 4));
  2626. // add high bit
  2627. const int8x16_t v0_0lf = vorrq_s8(v0_0l, qhl0);
  2628. const int8x16_t v0_0hf = vorrq_s8(v0_0h, qhh0);
  2629. const int8x16_t v0_1lf = vorrq_s8(v0_1l, qhl1);
  2630. const int8x16_t v0_1hf = vorrq_s8(v0_1h, qhh1);
  2631. // load y
  2632. const int8x16_t v1_0l = vld1q_s8(y0->qs);
  2633. const int8x16_t v1_0h = vld1q_s8(y0->qs + 16);
  2634. const int8x16_t v1_1l = vld1q_s8(y1->qs);
  2635. const int8x16_t v1_1h = vld1q_s8(y1->qs + 16);
  2636. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2637. ggml_vdotq_s32(vdupq_n_s32(0), v0_0lf, v1_0l),
  2638. ggml_vdotq_s32(vdupq_n_s32(0), v0_0hf, v1_0h))), GGML_FP16_TO_FP32(x0->d)*y0->d);
  2639. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2640. ggml_vdotq_s32(vdupq_n_s32(0), v0_1lf, v1_1l),
  2641. ggml_vdotq_s32(vdupq_n_s32(0), v0_1hf, v1_1h))), GGML_FP16_TO_FP32(x1->d)*y1->d);
  2642. }
  2643. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1) + summs0 + summs1;
  2644. #elif defined(__wasm_simd128__)
  2645. v128_t sumv = wasm_f32x4_splat(0.0f);
  2646. float summs = 0.0f;
  2647. uint32_t qh;
  2648. uint64_t tmp[4];
  2649. // TODO: check if unrolling this is better
  2650. for (int i = 0; i < nb; ++i) {
  2651. const block_q5_1 * restrict x0 = &x[i];
  2652. const block_q8_1 * restrict y0 = &y[i];
  2653. summs += GGML_FP16_TO_FP32(x0->m) * y0->s;
  2654. const v128_t m4b = wasm_i8x16_splat(0x0F);
  2655. // extract the 5th bit
  2656. memcpy(&qh, x0->qh, sizeof(qh));
  2657. tmp[0] = table_b2b_0[(qh >> 0) & 0xFF];
  2658. tmp[1] = table_b2b_0[(qh >> 8) & 0xFF];
  2659. tmp[2] = table_b2b_0[(qh >> 16) & 0xFF];
  2660. tmp[3] = table_b2b_0[(qh >> 24) ];
  2661. const v128_t qhl = wasm_v128_load(tmp + 0);
  2662. const v128_t qhh = wasm_v128_load(tmp + 2);
  2663. const v128_t v0 = wasm_v128_load(x0->qs);
  2664. // 4-bit -> 8-bit
  2665. const v128_t v0l = wasm_v128_and (v0, m4b);
  2666. const v128_t v0h = wasm_u8x16_shr(v0, 4);
  2667. // add high bit
  2668. const v128_t v0lf = wasm_v128_or(v0l, qhl);
  2669. const v128_t v0hf = wasm_v128_or(v0h, qhh);
  2670. // load y
  2671. const v128_t v1l = wasm_v128_load(y0->qs);
  2672. const v128_t v1h = wasm_v128_load(y0->qs + 16);
  2673. // int8x16 -> int16x8
  2674. const v128_t v0lfl = wasm_i16x8_extend_low_i8x16 (v0lf);
  2675. const v128_t v0lfh = wasm_i16x8_extend_high_i8x16(v0lf);
  2676. const v128_t v0hfl = wasm_i16x8_extend_low_i8x16 (v0hf);
  2677. const v128_t v0hfh = wasm_i16x8_extend_high_i8x16(v0hf);
  2678. const v128_t v1ll = wasm_i16x8_extend_low_i8x16 (v1l);
  2679. const v128_t v1lh = wasm_i16x8_extend_high_i8x16(v1l);
  2680. const v128_t v1hl = wasm_i16x8_extend_low_i8x16 (v1h);
  2681. const v128_t v1hh = wasm_i16x8_extend_high_i8x16(v1h);
  2682. // dot product
  2683. sumv = wasm_f32x4_add(sumv,
  2684. wasm_f32x4_mul(wasm_f32x4_convert_i32x4(wasm_i32x4_add(
  2685. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0lfl, v1ll),
  2686. wasm_i32x4_dot_i16x8(v0lfh, v1lh)),
  2687. wasm_i32x4_add(wasm_i32x4_dot_i16x8(v0hfl, v1hl),
  2688. wasm_i32x4_dot_i16x8(v0hfh, v1hh)))),
  2689. wasm_f32x4_splat(GGML_FP16_TO_FP32(x0->d) * y0->d)));
  2690. }
  2691. *s = wasm_f32x4_extract_lane(sumv, 0) + wasm_f32x4_extract_lane(sumv, 1) +
  2692. wasm_f32x4_extract_lane(sumv, 2) + wasm_f32x4_extract_lane(sumv, 3) + summs;
  2693. #elif defined(__AVX2__)
  2694. // Initialize accumulator with zeros
  2695. __m256 acc = _mm256_setzero_ps();
  2696. float summs = 0.0f;
  2697. // Main loop
  2698. for (int i = 0; i < nb; i++) {
  2699. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  2700. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2701. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2702. __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2703. bxhi = _mm256_and_si256(bxhi, _mm256_set1_epi8(0x10));
  2704. bx = _mm256_or_si256(bx, bxhi);
  2705. const __m256 dy = _mm256_set1_ps(y[i].d);
  2706. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2707. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  2708. acc = _mm256_fmadd_ps(q, _mm256_mul_ps(dx, dy), acc);
  2709. }
  2710. *s = hsum_float_8(acc) + summs;
  2711. #elif defined(__AVX__)
  2712. // Initialize accumulator with zeros
  2713. __m256 acc = _mm256_setzero_ps();
  2714. __m128i mask = _mm_set1_epi8(0x10);
  2715. float summs = 0.0f;
  2716. // Main loop
  2717. for (int i = 0; i < nb; i++) {
  2718. const __m256 dx = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d));
  2719. summs += GGML_FP16_TO_FP32(x[i].m) * y[i].s;
  2720. __m256i bx = bytes_from_nibbles_32(x[i].qs);
  2721. const __m256i bxhi = bytes_from_bits_32(x[i].qh);
  2722. __m128i bxhil = _mm256_castsi256_si128(bxhi);
  2723. __m128i bxhih = _mm256_extractf128_si256(bxhi, 1);
  2724. bxhil = _mm_and_si128(bxhil, mask);
  2725. bxhih = _mm_and_si128(bxhih, mask);
  2726. __m128i bxl = _mm256_castsi256_si128(bx);
  2727. __m128i bxh = _mm256_extractf128_si256(bx, 1);
  2728. bxl = _mm_or_si128(bxl, bxhil);
  2729. bxh = _mm_or_si128(bxh, bxhih);
  2730. bx = MM256_SET_M128I(bxh, bxl);
  2731. const __m256 dy = _mm256_set1_ps(y[i].d);
  2732. const __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2733. const __m256 q = mul_sum_us8_pairs_float(bx, by);
  2734. acc = _mm256_add_ps(_mm256_mul_ps(q, _mm256_mul_ps(dx, dy)), acc);
  2735. }
  2736. *s = hsum_float_8(acc) + summs;
  2737. #elif defined(__riscv_v_intrinsic)
  2738. float sumf = 0.0;
  2739. uint32_t qh;
  2740. size_t vl = __riscv_vsetvl_e8m1(qk/2);
  2741. // temporary registers for shift operations
  2742. vuint32m2_t vt_1 = __riscv_vid_v_u32m2(vl);
  2743. vuint32m2_t vt_2 = __riscv_vadd_vx_u32m2(vt_1, 12, vl);
  2744. for (int i = 0; i < nb; i++) {
  2745. memcpy(&qh, x[i].qh, sizeof(uint32_t));
  2746. // load qh
  2747. vuint32m2_t vqh = __riscv_vmv_v_x_u32m2(qh, vl);
  2748. // ((qh >> (j + 0)) << 4) & 0x10;
  2749. vuint32m2_t xhr_0 = __riscv_vsrl_vv_u32m2(vqh, vt_1, vl);
  2750. vuint32m2_t xhl_0 = __riscv_vsll_vx_u32m2(xhr_0, 4, vl);
  2751. vuint32m2_t xha_0 = __riscv_vand_vx_u32m2(xhl_0, 0x10, vl);
  2752. // ((qh >> (j + 12)) ) & 0x10;
  2753. vuint32m2_t xhr_1 = __riscv_vsrl_vv_u32m2(vqh, vt_2, vl);
  2754. vuint32m2_t xha_1 = __riscv_vand_vx_u32m2(xhr_1, 0x10, vl);
  2755. // narrowing
  2756. vuint16m1_t xhc_0 = __riscv_vncvt_x_x_w_u16m1(xha_0, vl);
  2757. vuint8mf2_t xh_0 = __riscv_vncvt_x_x_w_u8mf2(xhc_0, vl);
  2758. vuint16m1_t xhc_1 = __riscv_vncvt_x_x_w_u16m1(xha_1, vl);
  2759. vuint8mf2_t xh_1 = __riscv_vncvt_x_x_w_u8mf2(xhc_1, vl);
  2760. // load
  2761. vuint8mf2_t tx = __riscv_vle8_v_u8mf2(x[i].qs, vl);
  2762. vint8mf2_t y0 = __riscv_vle8_v_i8mf2(y[i].qs, vl);
  2763. vint8mf2_t y1 = __riscv_vle8_v_i8mf2(y[i].qs+16, vl);
  2764. vuint8mf2_t x_at = __riscv_vand_vx_u8mf2(tx, 0x0F, vl);
  2765. vuint8mf2_t x_lt = __riscv_vsrl_vx_u8mf2(tx, 0x04, vl);
  2766. vuint8mf2_t x_a = __riscv_vor_vv_u8mf2(x_at, xh_0, vl);
  2767. vuint8mf2_t x_l = __riscv_vor_vv_u8mf2(x_lt, xh_1, vl);
  2768. vint8mf2_t v0 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_a);
  2769. vint8mf2_t v1 = __riscv_vreinterpret_v_u8mf2_i8mf2(x_l);
  2770. vint16m1_t vec_mul1 = __riscv_vwmul_vv_i16m1(v0, y0, vl);
  2771. vint16m1_t vec_mul2 = __riscv_vwmul_vv_i16m1(v1, y1, vl);
  2772. vint32m1_t vec_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2773. vint32m1_t vs1 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul1, vec_zero, vl);
  2774. vint32m1_t vs2 = __riscv_vwredsum_vs_i16m1_i32m1(vec_mul2, vs1, vl);
  2775. int sumi = __riscv_vmv_x_s_i32m1_i32(vs2);
  2776. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2777. }
  2778. *s = sumf;
  2779. #else
  2780. // scalar
  2781. float sumf = 0.0;
  2782. for (int i = 0; i < nb; i++) {
  2783. uint32_t qh;
  2784. memcpy(&qh, x[i].qh, sizeof(qh));
  2785. int sumi = 0;
  2786. for (int j = 0; j < qk/2; ++j) {
  2787. const uint8_t xh_0 = ((qh >> (j + 0)) << 4) & 0x10;
  2788. const uint8_t xh_1 = ((qh >> (j + 12)) ) & 0x10;
  2789. const int32_t x0 = (x[i].qs[j] & 0xF) | xh_0;
  2790. const int32_t x1 = (x[i].qs[j] >> 4) | xh_1;
  2791. sumi += (x0 * y[i].qs[j]) + (x1 * y[i].qs[j + qk/2]);
  2792. }
  2793. sumf += (GGML_FP16_TO_FP32(x[i].d)*y[i].d)*sumi + GGML_FP16_TO_FP32(x[i].m)*y[i].s;
  2794. }
  2795. *s = sumf;
  2796. #endif
  2797. }
  2798. void ggml_vec_dot_q8_0_q8_0(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2799. const int qk = QK8_0;
  2800. const int nb = n / qk;
  2801. assert(n % qk == 0);
  2802. const block_q8_0 * restrict x = vx;
  2803. const block_q8_0 * restrict y = vy;
  2804. #if defined(__ARM_NEON)
  2805. float32x4_t sumv0 = vdupq_n_f32(0.0f);
  2806. float32x4_t sumv1 = vdupq_n_f32(0.0f);
  2807. assert(nb % 2 == 0); // TODO: handle odd nb
  2808. for (int i = 0; i < nb; i += 2) {
  2809. const block_q8_0 * restrict x0 = &x[i + 0];
  2810. const block_q8_0 * restrict x1 = &x[i + 1];
  2811. const block_q8_0 * restrict y0 = &y[i + 0];
  2812. const block_q8_0 * restrict y1 = &y[i + 1];
  2813. const int8x16_t x0_0 = vld1q_s8(x0->qs);
  2814. const int8x16_t x0_1 = vld1q_s8(x0->qs + 16);
  2815. const int8x16_t x1_0 = vld1q_s8(x1->qs);
  2816. const int8x16_t x1_1 = vld1q_s8(x1->qs + 16);
  2817. // load y
  2818. const int8x16_t y0_0 = vld1q_s8(y0->qs);
  2819. const int8x16_t y0_1 = vld1q_s8(y0->qs + 16);
  2820. const int8x16_t y1_0 = vld1q_s8(y1->qs);
  2821. const int8x16_t y1_1 = vld1q_s8(y1->qs + 16);
  2822. sumv0 = vmlaq_n_f32(sumv0, vcvtq_f32_s32(vaddq_s32(
  2823. ggml_vdotq_s32(vdupq_n_s32(0), x0_0, y0_0),
  2824. ggml_vdotq_s32(vdupq_n_s32(0), x0_1, y0_1))), GGML_FP16_TO_FP32(x0->d)*GGML_FP16_TO_FP32(y0->d));
  2825. sumv1 = vmlaq_n_f32(sumv1, vcvtq_f32_s32(vaddq_s32(
  2826. ggml_vdotq_s32(vdupq_n_s32(0), x1_0, y1_0),
  2827. ggml_vdotq_s32(vdupq_n_s32(0), x1_1, y1_1))), GGML_FP16_TO_FP32(x1->d)*GGML_FP16_TO_FP32(y1->d));
  2828. }
  2829. *s = vaddvq_f32(sumv0) + vaddvq_f32(sumv1);
  2830. #elif defined(__AVX2__) || defined(__AVX__)
  2831. // Initialize accumulator with zeros
  2832. __m256 acc = _mm256_setzero_ps();
  2833. // Main loop
  2834. for (int i = 0; i < nb; ++i) {
  2835. // Compute combined scale for the block
  2836. const __m256 d = _mm256_set1_ps(GGML_FP16_TO_FP32(x[i].d) * GGML_FP16_TO_FP32(y[i].d));
  2837. __m256i bx = _mm256_loadu_si256((const __m256i *)x[i].qs);
  2838. __m256i by = _mm256_loadu_si256((const __m256i *)y[i].qs);
  2839. const __m256 q = mul_sum_i8_pairs_float(bx, by);
  2840. // Multiply q with scale and accumulate
  2841. #if defined(__AVX2__)
  2842. acc = _mm256_fmadd_ps( d, q, acc );
  2843. #else
  2844. acc = _mm256_add_ps( _mm256_mul_ps( d, q ), acc );
  2845. #endif
  2846. }
  2847. *s = hsum_float_8(acc);
  2848. #elif defined(__riscv_v_intrinsic)
  2849. float sumf = 0.0;
  2850. size_t vl = __riscv_vsetvl_e8m1(qk);
  2851. for (int i = 0; i < nb; i++) {
  2852. // load elements
  2853. vint8m1_t bx = __riscv_vle8_v_i8m1(x[i].qs, vl);
  2854. vint8m1_t by = __riscv_vle8_v_i8m1(y[i].qs, vl);
  2855. vint16m2_t vw_mul = __riscv_vwmul_vv_i16m2(bx, by, vl);
  2856. vint32m1_t v_zero = __riscv_vmv_v_x_i32m1(0, vl);
  2857. vint32m1_t v_sum = __riscv_vwredsum_vs_i16m2_i32m1(vw_mul, v_zero, vl);
  2858. int sumi = __riscv_vmv_x_s_i32m1_i32(v_sum);
  2859. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  2860. }
  2861. *s = sumf;
  2862. #else
  2863. // scalar
  2864. float sumf = 0.0;
  2865. for (int i = 0; i < nb; i++) {
  2866. int sumi = 0;
  2867. for (int j = 0; j < qk; j++) {
  2868. sumi += x[i].qs[j]*y[i].qs[j];
  2869. }
  2870. sumf += sumi*(GGML_FP16_TO_FP32(x[i].d)*GGML_FP16_TO_FP32(y[i].d));
  2871. }
  2872. *s = sumf;
  2873. #endif
  2874. }
  2875. #if QK_K == 256
  2876. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2877. const block_q2_K * restrict x = vx;
  2878. const block_q8_K * restrict y = vy;
  2879. const int nb = n / QK_K;
  2880. #ifdef __ARM_NEON
  2881. const uint8x16_t m3 = vdupq_n_u8(0x3);
  2882. const uint8x16_t m4 = vdupq_n_u8(0xF);
  2883. const int32x4_t vzero = vdupq_n_s32(0);
  2884. ggml_int8x16x2_t q2bytes;
  2885. uint8_t aux[16];
  2886. float sum = 0;
  2887. for (int i = 0; i < nb; ++i) {
  2888. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  2889. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  2890. const uint8_t * restrict q2 = x[i].qs;
  2891. const int8_t * restrict q8 = y[i].qs;
  2892. const uint8_t * restrict sc = x[i].scales;
  2893. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  2894. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  2895. vst1q_u8(aux, scales);
  2896. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  2897. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  2898. const ggml_int16x8x2_t mins16 = {{vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))}};
  2899. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  2900. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  2901. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  2902. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  2903. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  2904. int isum = 0;
  2905. int is = 0;
  2906. // We use this macro instead of a function call because for some reason
  2907. // the code runs 2-3% slower, even if the function is declared inline
  2908. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  2909. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  2910. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  2911. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  2912. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;\
  2913. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  2914. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  2915. MULTIPLY_ACCUM_WITH_SCALE((index));
  2916. for (int j = 0; j < QK_K/128; ++j) {
  2917. const ggml_uint8x16x2_t q2bits = ggml_vld1q_u8_x2(q2); q2 += 32;
  2918. ggml_int8x16x2_t q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  2919. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  2920. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  2921. MULTIPLY_ACCUM_WITH_SCALE(0);
  2922. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  2923. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  2924. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  2925. is += 8;
  2926. }
  2927. sum += d * isum;
  2928. }
  2929. *s = sum;
  2930. #elif defined __AVX2__
  2931. const __m256i m3 = _mm256_set1_epi8(3);
  2932. const __m128i m4 = _mm_set1_epi8(0xF);
  2933. __m256 acc = _mm256_setzero_ps();
  2934. for (int i = 0; i < nb; ++i) {
  2935. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  2936. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  2937. const uint8_t * restrict q2 = x[i].qs;
  2938. const int8_t * restrict q8 = y[i].qs;
  2939. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  2940. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  2941. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  2942. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  2943. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  2944. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  2945. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  2946. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  2947. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  2948. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  2949. __m256i sumi = _mm256_setzero_si256();
  2950. for (int j = 0; j < QK_K/128; ++j) {
  2951. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  2952. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2953. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2954. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2955. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2956. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  2957. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  2958. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  2959. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  2960. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  2961. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  2962. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  2963. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  2964. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  2965. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  2966. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  2967. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  2968. p0 = _mm256_add_epi32(p0, p1);
  2969. p2 = _mm256_add_epi32(p2, p3);
  2970. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  2971. }
  2972. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  2973. }
  2974. *s = hsum_float_8(acc);
  2975. #elif defined __AVX__
  2976. const __m128i m3 = _mm_set1_epi8(0x3);
  2977. const __m128i m4 = _mm_set1_epi8(0xF);
  2978. const __m128i m2 = _mm_set1_epi8(0x2);
  2979. __m256 acc = _mm256_setzero_ps();
  2980. for (int i = 0; i < nb; ++i) {
  2981. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  2982. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  2983. const uint8_t * restrict q2 = x[i].qs;
  2984. const int8_t * restrict q8 = y[i].qs;
  2985. // load mins and scales from block_q2_K.scales[QK_K/16]
  2986. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  2987. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  2988. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  2989. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  2990. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  2991. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  2992. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  2993. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  2994. // sumf += -dmin * summs in 32bits*8
  2995. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  2996. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  2997. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  2998. const __m128i scales[2] = { scales_0, scales_1 };
  2999. __m128i sumi_0 = _mm_setzero_si128();
  3000. __m128i sumi_1 = _mm_setzero_si128();
  3001. for (int j = 0; j < QK_K/128; ++j) {
  3002. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  3003. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3004. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3005. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3006. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3007. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3008. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3009. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3010. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3011. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  3012. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3013. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3014. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3015. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3016. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3017. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  3018. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  3019. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3020. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3021. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3022. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  3023. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  3024. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  3025. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  3026. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  3027. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  3028. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  3029. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  3030. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  3031. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  3032. __m128i shuffle = _mm_set1_epi16(0x0100);
  3033. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  3034. shuffle = _mm_add_epi16(shuffle, m2);
  3035. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  3036. shuffle = _mm_add_epi16(shuffle, m2);
  3037. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  3038. shuffle = _mm_add_epi16(shuffle, m2);
  3039. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  3040. shuffle = _mm_add_epi16(shuffle, m2);
  3041. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  3042. shuffle = _mm_add_epi16(shuffle, m2);
  3043. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  3044. shuffle = _mm_add_epi16(shuffle, m2);
  3045. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  3046. shuffle = _mm_add_epi16(shuffle, m2);
  3047. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  3048. p0 = _mm_add_epi32(p0, p1);
  3049. p2 = _mm_add_epi32(p2, p3);
  3050. p4 = _mm_add_epi32(p4, p5);
  3051. p6 = _mm_add_epi32(p6, p7);
  3052. // isum in 32bits*4*2
  3053. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  3054. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  3055. }
  3056. // sumf += dall * isum - dmin * summs in 32bits
  3057. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3058. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  3059. }
  3060. *s = hsum_float_8(acc);
  3061. #elif defined __riscv_v_intrinsic
  3062. float sumf = 0;
  3063. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  3064. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  3065. for (int i = 0; i < nb; ++i) {
  3066. const uint8_t * q2 = x[i].qs;
  3067. const int8_t * q8 = y[i].qs;
  3068. const uint8_t * sc = x[i].scales;
  3069. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3070. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3071. size_t vl = 16;
  3072. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  3073. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  3074. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  3075. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  3076. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  3077. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  3078. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  3079. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  3080. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  3081. vl = 32;
  3082. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3083. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  3084. uint8_t is=0;
  3085. int isum=0;
  3086. for (int j = 0; j < QK_K/128; ++j) {
  3087. // load Q2
  3088. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  3089. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  3090. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  3091. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  3092. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  3093. // duplicate scale elements for product
  3094. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  3095. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  3096. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  3097. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  3098. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  3099. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  3100. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  3101. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  3102. // load Q8
  3103. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  3104. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  3105. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  3106. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  3107. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  3108. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  3109. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  3110. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  3111. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  3112. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  3113. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  3114. q2+=32; q8+=128; is=8;
  3115. }
  3116. sumf += dall * isum;
  3117. }
  3118. *s = sumf;
  3119. #else
  3120. float sumf = 0;
  3121. for (int i = 0; i < nb; ++i) {
  3122. const uint8_t * q2 = x[i].qs;
  3123. const int8_t * q8 = y[i].qs;
  3124. const uint8_t * sc = x[i].scales;
  3125. int summs = 0;
  3126. for (int j = 0; j < 16; ++j) {
  3127. summs += y[i].bsums[j] * (sc[j] >> 4);
  3128. }
  3129. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3130. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3131. int isum = 0;
  3132. int is = 0;
  3133. int d;
  3134. for (int k = 0; k < QK_K/128; ++k) {
  3135. int shift = 0;
  3136. for (int j = 0; j < 4; ++j) {
  3137. d = sc[is++] & 0xF;
  3138. int isuml = 0;
  3139. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  3140. isum += d * isuml;
  3141. d = sc[is++] & 0xF;
  3142. isuml = 0;
  3143. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  3144. isum += d * isuml;
  3145. shift += 2;
  3146. q8 += 32;
  3147. }
  3148. q2 += 32;
  3149. }
  3150. sumf += dall * isum - dmin * summs;
  3151. }
  3152. *s = sumf;
  3153. #endif
  3154. }
  3155. #else
  3156. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3157. const block_q2_K * restrict x = vx;
  3158. const block_q8_K * restrict y = vy;
  3159. const int nb = n / QK_K;
  3160. #ifdef __ARM_NEON
  3161. const uint8x16_t m3 = vdupq_n_u8(0x3);
  3162. const int32x4_t vzero = vdupq_n_s32(0);
  3163. ggml_int8x16x4_t q2bytes;
  3164. uint32_t aux32[2];
  3165. const uint8_t * scales = (const uint8_t *)aux32;
  3166. float sum = 0;
  3167. for (int i = 0; i < nb; ++i) {
  3168. const float d = y[i].d * (float)x[i].d;
  3169. const float dmin = -y[i].d * (float)x[i].dmin;
  3170. const uint8_t * restrict q2 = x[i].qs;
  3171. const int8_t * restrict q8 = y[i].qs;
  3172. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3173. aux32[0] = sc[0] & 0x0f0f0f0f;
  3174. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  3175. 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]);
  3176. int isum1 = 0, isum2 = 0;
  3177. const uint8x16_t q2bits = vld1q_u8(q2);
  3178. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  3179. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  3180. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  3181. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  3182. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  3183. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  3184. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  3185. isum1 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  3186. isum2 += vaddvq_s32(ggml_vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  3187. sum += d * (isum1 + isum2);
  3188. }
  3189. *s = sum;
  3190. #elif defined __AVX2__
  3191. const __m256i m3 = _mm256_set1_epi8(3);
  3192. __m256 acc = _mm256_setzero_ps();
  3193. uint32_t ud, um;
  3194. const uint8_t * restrict db = (const uint8_t *)&ud;
  3195. const uint8_t * restrict mb = (const uint8_t *)&um;
  3196. float summs = 0;
  3197. // TODO: optimize this
  3198. for (int i = 0; i < nb; ++i) {
  3199. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3200. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3201. const uint8_t * restrict q2 = x[i].qs;
  3202. const int8_t * restrict q8 = y[i].qs;
  3203. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3204. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  3205. um = (sc[0] >> 4) & 0x0f0f0f0f;
  3206. 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];
  3207. summs += dmin * smin;
  3208. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  3209. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  3210. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  3211. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3212. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3213. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  3214. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  3215. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  3216. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  3217. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  3218. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  3219. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  3220. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  3221. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  3222. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  3223. }
  3224. *s = hsum_float_8(acc) + summs;
  3225. #elif defined __AVX__
  3226. const __m128i m3 = _mm_set1_epi8(3);
  3227. __m256 acc = _mm256_setzero_ps();
  3228. uint32_t ud, um;
  3229. const uint8_t * restrict db = (const uint8_t *)&ud;
  3230. const uint8_t * restrict mb = (const uint8_t *)&um;
  3231. float summs = 0;
  3232. // TODO: optimize this
  3233. for (int i = 0; i < nb; ++i) {
  3234. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3235. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3236. const uint8_t * restrict q2 = x[i].qs;
  3237. const int8_t * restrict q8 = y[i].qs;
  3238. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3239. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  3240. um = (sc[0] >> 4) & 0x0f0f0f0f;
  3241. 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];
  3242. summs += dmin * smin;
  3243. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  3244. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  3245. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  3246. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  3247. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  3248. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3249. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3250. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  3251. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  3252. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  3253. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  3254. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  3255. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  3256. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  3257. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  3258. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  3259. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  3260. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  3261. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  3262. }
  3263. *s = hsum_float_8(acc) + summs;
  3264. #elif defined __riscv_v_intrinsic
  3265. uint32_t aux32[2];
  3266. const uint8_t * scales = (const uint8_t *)aux32;
  3267. float sumf = 0;
  3268. for (int i = 0; i < nb; ++i) {
  3269. const float d = y[i].d * (float)x[i].d;
  3270. const float dmin = -y[i].d * (float)x[i].dmin;
  3271. const uint8_t * restrict q2 = x[i].qs;
  3272. const int8_t * restrict q8 = y[i].qs;
  3273. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  3274. aux32[0] = sc[0] & 0x0f0f0f0f;
  3275. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  3276. 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]);
  3277. int isum1 = 0;
  3278. int isum2 = 0;
  3279. size_t vl = 16;
  3280. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  3281. // load Q2
  3282. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  3283. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  3284. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  3285. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  3286. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  3287. // load Q8, and take product with Q2
  3288. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  3289. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  3290. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  3291. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  3292. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  3293. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  3294. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  3295. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  3296. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  3297. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  3298. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  3299. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  3300. sumf += d * (isum1 + isum2);
  3301. }
  3302. *s = sumf;
  3303. #else
  3304. float sumf = 0;
  3305. int isum[4];
  3306. for (int i = 0; i < nb; ++i) {
  3307. const uint8_t * q2 = x[i].qs;
  3308. const int8_t * q8 = y[i].qs;
  3309. const uint8_t * sc = x[i].scales;
  3310. int summs = 0;
  3311. for (int j = 0; j < QK_K/16; ++j) {
  3312. summs += y[i].bsums[j] * (sc[j] >> 4);
  3313. }
  3314. const float dall = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3315. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3316. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  3317. for (int l = 0; l < 16; ++l) {
  3318. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  3319. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  3320. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  3321. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  3322. }
  3323. for (int l = 0; l < 4; ++l) {
  3324. isum[l] *= (sc[l] & 0xF);
  3325. }
  3326. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  3327. }
  3328. *s = sumf;
  3329. #endif
  3330. }
  3331. #endif
  3332. #if QK_K == 256
  3333. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3334. assert(n % QK_K == 0);
  3335. const uint32_t kmask1 = 0x03030303;
  3336. const uint32_t kmask2 = 0x0f0f0f0f;
  3337. const block_q3_K * restrict x = vx;
  3338. const block_q8_K * restrict y = vy;
  3339. const int nb = n / QK_K;
  3340. #ifdef __ARM_NEON
  3341. uint32_t aux[3];
  3342. uint32_t utmp[4];
  3343. const uint8x16_t m3b = vdupq_n_u8(0x3);
  3344. const int32x4_t vzero = vdupq_n_s32(0);
  3345. const uint8x16_t m0 = vdupq_n_u8(1);
  3346. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  3347. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  3348. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  3349. const int8_t m32 = 32;
  3350. ggml_int8x16x4_t q3bytes;
  3351. float sum = 0;
  3352. for (int i = 0; i < nb; ++i) {
  3353. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3354. const uint8_t * restrict q3 = x[i].qs;
  3355. const uint8_t * restrict qh = x[i].hmask;
  3356. const int8_t * restrict q8 = y[i].qs;
  3357. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  3358. ggml_uint8x16x4_t q3h;
  3359. int32_t isum = 0;
  3360. // Set up scales
  3361. memcpy(aux, x[i].scales, 12);
  3362. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  3363. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  3364. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  3365. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  3366. int8_t * scale = (int8_t *)utmp;
  3367. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  3368. for (int j = 0; j < QK_K/128; ++j) {
  3369. const ggml_uint8x16x2_t q3bits = ggml_vld1q_u8_x2(q3); q3 += 32;
  3370. const ggml_int8x16x4_t q8bytes_1 = ggml_vld1q_s8_x4(q8); q8 += 64;
  3371. const ggml_int8x16x4_t q8bytes_2 = ggml_vld1q_s8_x4(q8); q8 += 64;
  3372. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  3373. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  3374. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  3375. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  3376. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  3377. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  3378. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  3379. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  3380. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  3381. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  3382. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  3383. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  3384. scale += 4;
  3385. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  3386. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  3387. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  3388. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  3389. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  3390. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  3391. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  3392. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  3393. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  3394. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  3395. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  3396. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  3397. scale += 4;
  3398. if (j == 0) {
  3399. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  3400. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  3401. }
  3402. }
  3403. sum += d * isum;
  3404. }
  3405. *s = sum;
  3406. #elif defined __AVX2__
  3407. const __m256i m3 = _mm256_set1_epi8(3);
  3408. const __m256i mone = _mm256_set1_epi8(1);
  3409. const __m128i m32 = _mm_set1_epi8(32);
  3410. __m256 acc = _mm256_setzero_ps();
  3411. uint32_t aux[3];
  3412. for (int i = 0; i < nb; ++i) {
  3413. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3414. const uint8_t * restrict q3 = x[i].qs;
  3415. const int8_t * restrict q8 = y[i].qs;
  3416. // Set up scales
  3417. memcpy(aux, x[i].scales, 12);
  3418. __m128i scales128 = _mm_set_epi32(
  3419. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  3420. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  3421. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  3422. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  3423. scales128 = _mm_sub_epi8(scales128, m32);
  3424. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  3425. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  3426. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  3427. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  3428. // high bit
  3429. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  3430. // integer accumulator
  3431. __m256i sumi = _mm256_setzero_si256();
  3432. int bit = 0;
  3433. int is = 0;
  3434. for (int j = 0; j < QK_K/128; ++j) {
  3435. // load low 2 bits
  3436. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  3437. // prepare low and high bits
  3438. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  3439. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3440. ++bit;
  3441. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  3442. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3443. ++bit;
  3444. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  3445. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3446. ++bit;
  3447. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  3448. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  3449. ++bit;
  3450. // load Q8 quants
  3451. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3452. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3453. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3454. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3455. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3456. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3457. // and 2 if the high bit was set)
  3458. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  3459. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  3460. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  3461. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  3462. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  3463. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  3464. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  3465. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  3466. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  3467. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  3468. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  3469. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  3470. // multiply with scales
  3471. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  3472. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  3473. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  3474. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  3475. // accumulate
  3476. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  3477. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  3478. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  3479. }
  3480. // multiply with block scale and accumulate
  3481. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3482. }
  3483. *s = hsum_float_8(acc);
  3484. #elif defined __AVX__
  3485. const __m128i m3 = _mm_set1_epi8(3);
  3486. const __m128i mone = _mm_set1_epi8(1);
  3487. const __m128i m32 = _mm_set1_epi8(32);
  3488. const __m128i m2 = _mm_set1_epi8(2);
  3489. __m256 acc = _mm256_setzero_ps();
  3490. const uint32_t *aux;
  3491. for (int i = 0; i < nb; ++i) {
  3492. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3493. const uint8_t * restrict q3 = x[i].qs;
  3494. const int8_t * restrict q8 = y[i].qs;
  3495. // Set up scales
  3496. aux = (const uint32_t *)x[i].scales;
  3497. __m128i scales128 = _mm_set_epi32(
  3498. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  3499. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  3500. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  3501. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  3502. scales128 = _mm_sub_epi8(scales128, m32);
  3503. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  3504. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  3505. const __m128i scales[2] = { scales_0, scales_1 };
  3506. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  3507. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  3508. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  3509. // integer accumulator
  3510. __m128i sumi_0 = _mm_setzero_si128();
  3511. __m128i sumi_1 = _mm_setzero_si128();
  3512. for (int j = 0; j < QK_K/128; ++j) {
  3513. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  3514. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  3515. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  3516. // prepare low and high bits
  3517. const int bit = j << 2;
  3518. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  3519. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  3520. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  3521. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  3522. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  3523. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  3524. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  3525. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  3526. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  3527. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  3528. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  3529. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  3530. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  3531. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  3532. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  3533. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  3534. // load Q8 quants from block_q8_K.qs[QK_K]
  3535. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3536. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3537. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3538. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3539. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3540. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3541. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3542. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3543. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3544. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3545. // and 2 if the high bit was set)
  3546. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  3547. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  3548. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  3549. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  3550. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  3551. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  3552. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  3553. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  3554. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  3555. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  3556. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  3557. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  3558. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  3559. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  3560. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  3561. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  3562. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  3563. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  3564. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  3565. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  3566. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  3567. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  3568. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  3569. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  3570. // multiply with scales
  3571. __m128i shuffle = _mm_set1_epi16(0x0100);
  3572. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  3573. shuffle = _mm_add_epi16(shuffle, m2);
  3574. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  3575. shuffle = _mm_add_epi16(shuffle, m2);
  3576. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  3577. shuffle = _mm_add_epi16(shuffle, m2);
  3578. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  3579. shuffle = _mm_add_epi16(shuffle, m2);
  3580. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  3581. shuffle = _mm_add_epi16(shuffle, m2);
  3582. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  3583. shuffle = _mm_add_epi16(shuffle, m2);
  3584. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  3585. shuffle = _mm_add_epi16(shuffle, m2);
  3586. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  3587. // accumulate
  3588. p16_0 = _mm_add_epi32(p16_0, p16_1);
  3589. p16_2 = _mm_add_epi32(p16_2, p16_3);
  3590. p16_4 = _mm_add_epi32(p16_4, p16_5);
  3591. p16_6 = _mm_add_epi32(p16_6, p16_7);
  3592. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  3593. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  3594. }
  3595. // multiply with block scale and accumulate
  3596. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3597. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  3598. }
  3599. *s = hsum_float_8(acc);
  3600. #elif defined __riscv_v_intrinsic
  3601. uint32_t aux[3];
  3602. uint32_t utmp[4];
  3603. float sumf = 0;
  3604. for (int i = 0; i < nb; ++i) {
  3605. const uint8_t * restrict q3 = x[i].qs;
  3606. const uint8_t * restrict qh = x[i].hmask;
  3607. const int8_t * restrict q8 = y[i].qs;
  3608. memcpy(aux, x[i].scales, 12);
  3609. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  3610. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  3611. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  3612. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  3613. int8_t * scale = (int8_t *)utmp;
  3614. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  3615. size_t vl = 32;
  3616. uint8_t m = 1;
  3617. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3618. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  3619. int sum_t = 0;
  3620. for (int j = 0; j < QK_K; j += 128) {
  3621. vl = 32;
  3622. // load Q3
  3623. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  3624. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  3625. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  3626. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  3627. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  3628. // compute mask for subtraction
  3629. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3630. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  3631. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  3632. m <<= 1;
  3633. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3634. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  3635. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  3636. m <<= 1;
  3637. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3638. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  3639. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  3640. m <<= 1;
  3641. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3642. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  3643. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  3644. m <<= 1;
  3645. // load Q8 and take product with Q3
  3646. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  3647. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  3648. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  3649. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  3650. vl = 16;
  3651. // retrieve lane to multiply with scale
  3652. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  3653. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  3654. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  3655. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  3656. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  3657. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  3658. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  3659. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  3660. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  3661. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  3662. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  3663. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  3664. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  3665. q3 += 32; q8 += 128; scale += 8;
  3666. }
  3667. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  3668. sumf += d*sum_t;
  3669. }
  3670. *s = sumf;
  3671. #else
  3672. // scalar version
  3673. // This function is written like this so the compiler can manage to vectorize most of it
  3674. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  3675. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  3676. // The ideal situation would be if we could just write the code once, and the compiler would
  3677. // automatically produce the best possible set of machine instructions, instead of us having to manually
  3678. // write vectorized versions for AVX, ARM_NEON, etc.
  3679. int8_t aux8[QK_K];
  3680. int16_t aux16[8];
  3681. float sums [8];
  3682. int32_t aux32[8];
  3683. memset(sums, 0, 8*sizeof(float));
  3684. uint32_t auxs[4];
  3685. const int8_t * scales = (const int8_t*)auxs;
  3686. float sumf = 0;
  3687. for (int i = 0; i < nb; ++i) {
  3688. const uint8_t * restrict q3 = x[i].qs;
  3689. const uint8_t * restrict hm = x[i].hmask;
  3690. const int8_t * restrict q8 = y[i].qs;
  3691. memset(aux32, 0, 8*sizeof(int32_t));
  3692. int8_t * restrict a = aux8;
  3693. uint8_t m = 1;
  3694. for (int j = 0; j < QK_K; j += 128) {
  3695. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  3696. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3697. a += 32; m <<= 1;
  3698. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  3699. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3700. a += 32; m <<= 1;
  3701. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  3702. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3703. a += 32; m <<= 1;
  3704. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  3705. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  3706. a += 32; m <<= 1;
  3707. q3 += 32;
  3708. }
  3709. a = aux8;
  3710. memcpy(auxs, x[i].scales, 12);
  3711. uint32_t tmp = auxs[2];
  3712. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  3713. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  3714. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  3715. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  3716. for (int j = 0; j < QK_K/16; ++j) {
  3717. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3718. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  3719. q8 += 8; a += 8;
  3720. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3721. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  3722. q8 += 8; a += 8;
  3723. }
  3724. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  3725. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  3726. }
  3727. for (int l = 0; l < 8; ++l) sumf += sums[l];
  3728. *s = sumf;
  3729. #endif
  3730. }
  3731. #else
  3732. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3733. assert(n % QK_K == 0);
  3734. const block_q3_K * restrict x = vx;
  3735. const block_q8_K * restrict y = vy;
  3736. const int nb = n / QK_K;
  3737. #ifdef __ARM_NEON
  3738. const int32x4_t vzero = vdupq_n_s32(0);
  3739. const uint8x16_t m3b = vdupq_n_u8(0x3);
  3740. const uint8x16_t mh = vdupq_n_u8(4);
  3741. ggml_int8x16x4_t q3bytes;
  3742. uint16_t aux16[2];
  3743. int8_t * scales = (int8_t *)aux16;
  3744. float sum = 0;
  3745. for (int i = 0; i < nb; ++i) {
  3746. ggml_uint8x16x4_t q3h;
  3747. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  3748. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  3749. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(y[i].qs);
  3750. const uint16_t a = *(const uint16_t *)x[i].scales;
  3751. aux16[0] = a & 0x0f0f;
  3752. aux16[1] = (a >> 4) & 0x0f0f;
  3753. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  3754. 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]);
  3755. const float d = y[i].d * (float)x[i].d;
  3756. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  3757. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  3758. q3h.val[1] = vandq_u8(mh, htmp);
  3759. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  3760. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  3761. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  3762. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  3763. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  3764. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  3765. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  3766. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  3767. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  3768. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  3769. sum += d * isum;
  3770. }
  3771. *s = sum;
  3772. #elif defined __AVX2__
  3773. const __m256i m3 = _mm256_set1_epi8(3);
  3774. const __m256i m1 = _mm256_set1_epi8(1);
  3775. __m256 acc = _mm256_setzero_ps();
  3776. uint64_t aux64;
  3777. uint16_t aux16[2];
  3778. const int8_t * aux8 = (const int8_t *)aux16;
  3779. for (int i = 0; i < nb; ++i) {
  3780. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3781. const uint8_t * restrict q3 = x[i].qs;
  3782. const int8_t * restrict q8 = y[i].qs;
  3783. const uint16_t a = *(const uint16_t *)x[i].scales;
  3784. aux16[0] = a & 0x0f0f;
  3785. aux16[1] = (a >> 4) & 0x0f0f;
  3786. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  3787. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  3788. memcpy(&aux64, x[i].hmask, 8);
  3789. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  3790. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  3791. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  3792. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  3793. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  3794. // load low 2 bits
  3795. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  3796. // prepare low and high bits
  3797. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  3798. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  3799. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  3800. // load Q8 quants
  3801. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3802. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3803. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  3804. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3805. // and 2 if the high bit was set)
  3806. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  3807. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  3808. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  3809. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  3810. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  3811. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  3812. // multiply with scales
  3813. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  3814. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  3815. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  3816. // multiply with block scale and accumulate
  3817. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  3818. }
  3819. *s = hsum_float_8(acc);
  3820. #elif defined __AVX__
  3821. const __m128i m3 = _mm_set1_epi8(3);
  3822. const __m128i m1 = _mm_set1_epi8(1);
  3823. __m256 acc = _mm256_setzero_ps();
  3824. uint64_t aux64;
  3825. uint16_t aux16[2];
  3826. const int8_t * aux8 = (const int8_t *)aux16;
  3827. for (int i = 0; i < nb; ++i) {
  3828. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3829. const uint8_t * restrict q3 = x[i].qs;
  3830. const int8_t * restrict q8 = y[i].qs;
  3831. const uint16_t a = *(const uint16_t *)x[i].scales;
  3832. aux16[0] = a & 0x0f0f;
  3833. aux16[1] = (a >> 4) & 0x0f0f;
  3834. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  3835. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  3836. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  3837. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  3838. memcpy(&aux64, x[i].hmask, 8);
  3839. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  3840. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  3841. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  3842. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  3843. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  3844. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  3845. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  3846. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  3847. // load low 2 bits
  3848. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  3849. // prepare low and high bits
  3850. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  3851. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  3852. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  3853. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  3854. // load Q8 quants
  3855. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3856. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3857. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  3858. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  3859. // and 2 if the high bit was set)
  3860. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  3861. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  3862. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  3863. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  3864. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  3865. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  3866. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  3867. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  3868. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  3869. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  3870. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  3871. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  3872. // multiply with scales
  3873. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  3874. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  3875. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  3876. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  3877. p16_0 = _mm_add_epi32(p16_0, p16_2);
  3878. p16_1 = _mm_add_epi32(p16_1, p16_3);
  3879. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  3880. // multiply with block scale and accumulate
  3881. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  3882. }
  3883. *s = hsum_float_8(acc);
  3884. #elif defined __riscv_v_intrinsic
  3885. uint16_t aux16[2];
  3886. int8_t * scales = (int8_t *)aux16;
  3887. float sumf = 0;
  3888. for (int i = 0; i < nb; ++i) {
  3889. const uint8_t * restrict q3 = x[i].qs;
  3890. const int8_t * restrict q8 = y[i].qs;
  3891. const uint16_t a = *(const uint16_t *)x[i].scales;
  3892. aux16[0] = a & 0x0f0f;
  3893. aux16[1] = (a >> 4) & 0x0f0f;
  3894. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  3895. 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]);
  3896. const float d = y[i].d * (float)x[i].d;
  3897. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3898. // load qh
  3899. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  3900. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  3901. size_t vl = 16;
  3902. // extend and combine both qh_x1 and qh_x2
  3903. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  3904. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  3905. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  3906. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  3907. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  3908. // load Q3
  3909. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  3910. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  3911. 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);
  3912. 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);
  3913. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  3914. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  3915. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  3916. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  3917. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  3918. // load Q8 and take product with Q3
  3919. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  3920. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  3921. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  3922. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  3923. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  3924. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  3925. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  3926. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  3927. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  3928. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  3929. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  3930. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  3931. sumf += d * isum;
  3932. }
  3933. *s = sumf;
  3934. #else
  3935. int8_t aux8[QK_K];
  3936. int16_t aux16[8];
  3937. float sums [8];
  3938. int32_t aux32[8];
  3939. int32_t scales[4];
  3940. memset(sums, 0, 8*sizeof(float));
  3941. float sumf = 0;
  3942. for (int i = 0; i < nb; ++i) {
  3943. const uint8_t * restrict q3 = x[i].qs;
  3944. const uint8_t * restrict hm = x[i].hmask;
  3945. const int8_t * restrict q8 = y[i].qs;
  3946. int8_t * restrict a = aux8;
  3947. for (int l = 0; l < 8; ++l) {
  3948. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  3949. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  3950. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  3951. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  3952. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  3953. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  3954. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  3955. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  3956. }
  3957. scales[0] = (x[i].scales[0] & 0xF) - 8;
  3958. scales[1] = (x[i].scales[0] >> 4) - 8;
  3959. scales[2] = (x[i].scales[1] & 0xF) - 8;
  3960. scales[3] = (x[i].scales[1] >> 4) - 8;
  3961. memset(aux32, 0, 8*sizeof(int32_t));
  3962. for (int j = 0; j < QK_K/16; ++j) {
  3963. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3964. q8 += 8; a += 8;
  3965. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  3966. q8 += 8; a += 8;
  3967. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  3968. }
  3969. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  3970. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  3971. }
  3972. for (int l = 0; l < 8; ++l) sumf += sums[l];
  3973. *s = sumf;
  3974. #endif
  3975. }
  3976. #endif
  3977. #if QK_K == 256
  3978. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3979. assert(n % QK_K == 0);
  3980. const block_q4_K * restrict x = vx;
  3981. const block_q8_K * restrict y = vy;
  3982. const int nb = n / QK_K;
  3983. static const uint32_t kmask1 = 0x3f3f3f3f;
  3984. static const uint32_t kmask2 = 0x0f0f0f0f;
  3985. static const uint32_t kmask3 = 0x03030303;
  3986. uint32_t utmp[4];
  3987. #ifdef __ARM_NEON
  3988. const uint8x16_t m4b = vdupq_n_u8(0xf);
  3989. const int32x4_t mzero = vdupq_n_s32(0);
  3990. ggml_int8x16x2_t q4bytes;
  3991. ggml_int8x16x2_t q8bytes;
  3992. float sumf = 0;
  3993. for (int i = 0; i < nb; ++i) {
  3994. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  3995. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  3996. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  3997. memcpy(utmp, x[i].scales, 12);
  3998. uint32x2_t mins8 = { 0 };
  3999. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  4000. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  4001. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4002. utmp[0] &= kmask1;
  4003. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  4004. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4005. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4006. sumf -= dmin * vaddvq_s32(prod);
  4007. const uint8_t * scales = (const uint8_t *)utmp;
  4008. const uint8_t * restrict q4 = x[i].qs;
  4009. const int8_t * restrict q8 = y[i].qs;
  4010. int32_t sumi1 = 0;
  4011. int32_t sumi2 = 0;
  4012. for (int j = 0; j < QK_K/64; ++j) {
  4013. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4); q4 += 32;
  4014. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4015. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4016. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4017. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4018. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  4019. q8bytes = ggml_vld1q_s8_x2(q8); q8 += 32;
  4020. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4021. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4022. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4023. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  4024. }
  4025. sumf += d * (sumi1 + sumi2);
  4026. }
  4027. *s = sumf;
  4028. #elif defined __AVX2__
  4029. const __m256i m4 = _mm256_set1_epi8(0xF);
  4030. __m256 acc = _mm256_setzero_ps();
  4031. __m128 acc_m = _mm_setzero_ps();
  4032. for (int i = 0; i < nb; ++i) {
  4033. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4034. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4035. memcpy(utmp, x[i].scales, 12);
  4036. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4037. const uint32_t uaux = utmp[1] & kmask1;
  4038. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4039. utmp[2] = uaux;
  4040. utmp[0] &= kmask1;
  4041. const uint8_t * restrict q4 = x[i].qs;
  4042. const int8_t * restrict q8 = y[i].qs;
  4043. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4044. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4045. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4046. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4047. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  4048. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4049. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4050. __m256i sumi = _mm256_setzero_si256();
  4051. for (int j = 0; j < QK_K/64; ++j) {
  4052. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4053. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4054. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  4055. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4056. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4057. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4058. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4059. p16l = _mm256_madd_epi16(scale_l, p16l);
  4060. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  4061. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4062. p16h = _mm256_madd_epi16(scale_h, p16h);
  4063. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  4064. sumi = _mm256_add_epi32(sumi, sumj);
  4065. }
  4066. __m256 vd = _mm256_set1_ps(d);
  4067. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  4068. }
  4069. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4070. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4071. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4072. #elif defined __AVX__
  4073. const __m128i m4 = _mm_set1_epi8(0xF);
  4074. const __m128i m2 = _mm_set1_epi8(0x2);
  4075. __m256 acc = _mm256_setzero_ps();
  4076. __m128 acc_m = _mm_setzero_ps();
  4077. for (int i = 0; i < nb; ++i) {
  4078. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4079. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4080. const uint8_t * restrict q4 = x[i].qs;
  4081. const int8_t * restrict q8 = y[i].qs;
  4082. memcpy(utmp, x[i].scales, 12);
  4083. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4084. const uint32_t uaux = utmp[1] & kmask1;
  4085. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4086. utmp[2] = uaux;
  4087. utmp[0] &= kmask1;
  4088. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  4089. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  4090. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  4091. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  4092. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  4093. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  4094. const __m128i prod = _mm_madd_epi16(mins, q8s);
  4095. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  4096. __m128i sumi_0 = _mm_setzero_si128();
  4097. __m128i sumi_1 = _mm_setzero_si128();
  4098. __m128i shuffle = _mm_set1_epi16(0x0100);
  4099. for (int j = 0; j < QK_K/64; ++j) {
  4100. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  4101. shuffle = _mm_add_epi16(shuffle, m2);
  4102. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  4103. shuffle = _mm_add_epi16(shuffle, m2);
  4104. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4105. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  4106. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4107. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  4108. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  4109. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  4110. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4111. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  4112. p16l = _mm_madd_epi16(scale_l, p16l);
  4113. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  4114. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4115. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  4116. p16l = _mm_madd_epi16(scale_l, p16l);
  4117. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  4118. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4119. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  4120. p16h = _mm_madd_epi16(scale_h, p16h);
  4121. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  4122. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4123. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  4124. p16h = _mm_madd_epi16(scale_h, p16h);
  4125. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  4126. }
  4127. __m256 vd = _mm256_set1_ps(d);
  4128. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4129. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  4130. }
  4131. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  4132. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  4133. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  4134. #elif defined __riscv_v_intrinsic
  4135. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4136. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4137. float sumf = 0;
  4138. for (int i = 0; i < nb; ++i) {
  4139. size_t vl = 8;
  4140. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4141. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4142. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  4143. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  4144. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  4145. memcpy(utmp, x[i].scales, 12);
  4146. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4147. const uint32_t uaux = utmp[1] & kmask1;
  4148. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4149. utmp[2] = uaux;
  4150. utmp[0] &= kmask1;
  4151. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  4152. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  4153. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  4154. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4155. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  4156. const uint8_t * restrict q4 = x[i].qs;
  4157. const int8_t * restrict q8 = y[i].qs;
  4158. vl = 32;
  4159. int32_t sum_1 = 0;
  4160. int32_t sum_2 = 0;
  4161. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4162. for (int j = 0; j < QK_K/64; ++j) {
  4163. // load Q4
  4164. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  4165. // load Q8 and multiply it with lower Q4 nibble
  4166. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  4167. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  4168. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  4169. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  4170. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  4171. // load Q8 and multiply it with upper Q4 nibble
  4172. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  4173. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  4174. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  4175. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  4176. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  4177. q4 += 32; q8 += 64;
  4178. }
  4179. sumf += d*(sum_1 + sum_2);
  4180. }
  4181. *s = sumf;
  4182. #else
  4183. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4184. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4185. int8_t aux8[QK_K];
  4186. int16_t aux16[8];
  4187. float sums [8];
  4188. int32_t aux32[8];
  4189. memset(sums, 0, 8*sizeof(float));
  4190. float sumf = 0;
  4191. for (int i = 0; i < nb; ++i) {
  4192. const uint8_t * restrict q4 = x[i].qs;
  4193. const int8_t * restrict q8 = y[i].qs;
  4194. memset(aux32, 0, 8*sizeof(int32_t));
  4195. int8_t * restrict a = aux8;
  4196. for (int j = 0; j < QK_K/64; ++j) {
  4197. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  4198. a += 32;
  4199. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  4200. a += 32; q4 += 32;
  4201. }
  4202. memcpy(utmp, x[i].scales, 12);
  4203. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4204. const uint32_t uaux = utmp[1] & kmask1;
  4205. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4206. utmp[2] = uaux;
  4207. utmp[0] &= kmask1;
  4208. int sumi = 0;
  4209. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  4210. a = aux8;
  4211. int is = 0;
  4212. for (int j = 0; j < QK_K/32; ++j) {
  4213. int32_t scale = scales[is++];
  4214. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4215. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4216. q8 += 8; a += 8;
  4217. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4218. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4219. q8 += 8; a += 8;
  4220. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4221. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4222. q8 += 8; a += 8;
  4223. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4224. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4225. q8 += 8; a += 8;
  4226. }
  4227. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4228. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4229. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4230. sumf -= dmin * sumi;
  4231. }
  4232. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4233. *s = sumf;
  4234. #endif
  4235. }
  4236. #else
  4237. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4238. assert(n % QK_K == 0);
  4239. const block_q4_K * restrict x = vx;
  4240. const block_q8_K * restrict y = vy;
  4241. const int nb = n / QK_K;
  4242. #ifdef __ARM_NEON
  4243. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4244. const int32x4_t mzero = vdupq_n_s32(0);
  4245. float sumf = 0;
  4246. ggml_int8x16x2_t q4bytes;
  4247. ggml_int8x16x4_t q8bytes;
  4248. float sum_mins = 0.f;
  4249. uint16_t aux16[2];
  4250. const uint8_t * restrict scales = (const uint8_t *)aux16;
  4251. for (int i = 0; i < nb; ++i) {
  4252. const uint8_t * restrict q4 = x[i].qs;
  4253. const int8_t * restrict q8 = y[i].qs;
  4254. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  4255. aux16[0] = a[0] & 0x0f0f;
  4256. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4257. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  4258. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  4259. const float d = y[i].d * (float)x[i].d[0];
  4260. const ggml_uint8x16x2_t q4bits = ggml_vld1q_u8_x2(q4);
  4261. q8bytes = ggml_vld1q_s8_x4(q8);
  4262. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  4263. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  4264. const int32x4_t p1 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  4265. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  4266. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  4267. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  4268. const int32x4_t p2 = ggml_vdotq_s32(ggml_vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  4269. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  4270. sumf += d * (sumi1 + sumi2);
  4271. }
  4272. *s = sumf - sum_mins;
  4273. #elif defined __AVX2__
  4274. const __m256i m4 = _mm256_set1_epi8(0xF);
  4275. __m256 acc = _mm256_setzero_ps();
  4276. float summs = 0;
  4277. uint16_t aux16[2];
  4278. const uint8_t * scales = (const uint8_t *)aux16;
  4279. for (int i = 0; i < nb; ++i) {
  4280. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  4281. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  4282. const __m256 vd = _mm256_set1_ps(d);
  4283. const uint16_t * a = (const uint16_t *)x[i].scales;
  4284. aux16[0] = a[0] & 0x0f0f;
  4285. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4286. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  4287. const uint8_t * restrict q4 = x[i].qs;
  4288. const int8_t * restrict q8 = y[i].qs;
  4289. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  4290. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  4291. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  4292. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4293. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  4294. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  4295. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  4296. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  4297. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  4298. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  4299. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  4300. }
  4301. *s = hsum_float_8(acc) - summs;
  4302. #elif defined __AVX__
  4303. const __m128i m4 = _mm_set1_epi8(0xF);
  4304. __m256 acc = _mm256_setzero_ps();
  4305. float summs = 0;
  4306. uint16_t aux16[2];
  4307. const uint8_t * scales = (const uint8_t *)aux16;
  4308. for (int i = 0; i < nb; ++i) {
  4309. const float d = GGML_FP16_TO_FP32(x[i].d[0]) * y[i].d;
  4310. const float m = GGML_FP16_TO_FP32(x[i].d[1]) * y[i].d;
  4311. const __m256 vd = _mm256_set1_ps(d);
  4312. const uint16_t * a = (const uint16_t *)x[i].scales;
  4313. aux16[0] = a[0] & 0x0f0f;
  4314. aux16[1] = (a[0] >> 4) & 0x0f0f;
  4315. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  4316. const uint8_t * restrict q4 = x[i].qs;
  4317. const int8_t * restrict q8 = y[i].qs;
  4318. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  4319. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  4320. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  4321. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  4322. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  4323. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  4324. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  4325. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4326. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4327. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  4328. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  4329. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  4330. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  4331. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  4332. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  4333. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  4334. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  4335. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  4336. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  4337. }
  4338. *s = hsum_float_8(acc) - summs;
  4339. #elif defined __riscv_v_intrinsic
  4340. uint16_t s16[2];
  4341. const uint8_t * restrict scales = (const uint8_t *)s16;
  4342. float sumf = 0;
  4343. for (int i = 0; i < nb; ++i) {
  4344. const uint8_t * restrict q4 = x[i].qs;
  4345. const int8_t * restrict q8 = y[i].qs;
  4346. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  4347. s16[0] = b[0] & 0x0f0f;
  4348. s16[1] = (b[0] >> 4) & 0x0f0f;
  4349. 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]));
  4350. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  4351. size_t vl = 32;
  4352. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  4353. // load Q4
  4354. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  4355. // load Q8 and multiply it with lower Q4 nibble
  4356. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  4357. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  4358. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  4359. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  4360. // load Q8 and multiply it with upper Q4 nibble
  4361. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  4362. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  4363. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  4364. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  4365. }
  4366. *s = sumf;
  4367. #else
  4368. uint8_t aux8[QK_K];
  4369. int16_t aux16[16];
  4370. float sums [8];
  4371. memset(sums, 0, 8*sizeof(float));
  4372. uint16_t s16[2];
  4373. const uint8_t * restrict scales = (const uint8_t *)s16;
  4374. float sumf = 0;
  4375. for (int i = 0; i < nb; ++i) {
  4376. const uint8_t * restrict q4 = x[i].qs;
  4377. const int8_t * restrict q8 = y[i].qs;
  4378. uint8_t * restrict a = aux8;
  4379. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  4380. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  4381. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  4382. s16[0] = b[0] & 0x0f0f;
  4383. s16[1] = (b[0] >> 4) & 0x0f0f;
  4384. 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]));
  4385. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d[0]);
  4386. for (int j = 0; j < QK_K/32; ++j) {
  4387. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  4388. q8 += 16; a += 16;
  4389. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  4390. q8 += 16; a += 16;
  4391. const float dl = d * scales[j];
  4392. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  4393. }
  4394. }
  4395. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4396. *s = sumf;
  4397. #endif
  4398. }
  4399. #endif
  4400. #if QK_K == 256
  4401. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4402. assert(n % QK_K == 0);
  4403. const block_q5_K * restrict x = vx;
  4404. const block_q8_K * restrict y = vy;
  4405. const int nb = n / QK_K;
  4406. static const uint32_t kmask1 = 0x3f3f3f3f;
  4407. static const uint32_t kmask2 = 0x0f0f0f0f;
  4408. static const uint32_t kmask3 = 0x03030303;
  4409. uint32_t utmp[4];
  4410. #ifdef __ARM_NEON
  4411. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4412. const uint8x16_t mone = vdupq_n_u8(1);
  4413. const uint8x16_t mtwo = vdupq_n_u8(2);
  4414. const int32x4_t mzero = vdupq_n_s32(0);
  4415. ggml_int8x16x4_t q5bytes;
  4416. float sumf = 0;
  4417. for (int i = 0; i < nb; ++i) {
  4418. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4419. const float dmin = y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4420. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  4421. memcpy(utmp, x[i].scales, 12);
  4422. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4423. const uint32_t uaux = utmp[1] & kmask1;
  4424. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4425. utmp[2] = uaux;
  4426. utmp[0] &= kmask1;
  4427. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  4428. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  4429. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  4430. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  4431. int32_t sumi_mins = vaddvq_s32(prod);
  4432. const uint8_t * scales = (const uint8_t *)utmp;
  4433. const uint8_t * restrict q5 = x[i].qs;
  4434. const uint8_t * restrict qh = x[i].qh;
  4435. const int8_t * restrict q8 = y[i].qs;
  4436. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh);
  4437. ggml_uint8x16x4_t q5h;
  4438. int32_t sumi = 0;
  4439. for (int j = 0; j < QK_K/64; ++j) {
  4440. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5); q5 += 32;
  4441. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  4442. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  4443. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  4444. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  4445. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  4446. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  4447. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  4448. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  4449. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  4450. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  4451. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  4452. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  4453. sumi += vaddvq_s32(ggml_vdotq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  4454. }
  4455. sumf += d * sumi - dmin * sumi_mins;
  4456. }
  4457. *s = sumf;
  4458. #elif defined __AVX2__
  4459. const __m256i m4 = _mm256_set1_epi8(0xF);
  4460. const __m128i mzero = _mm_setzero_si128();
  4461. const __m256i mone = _mm256_set1_epi8(1);
  4462. __m256 acc = _mm256_setzero_ps();
  4463. float summs = 0.f;
  4464. for (int i = 0; i < nb; ++i) {
  4465. const uint8_t * restrict q5 = x[i].qs;
  4466. const int8_t * restrict q8 = y[i].qs;
  4467. #if QK_K == 256
  4468. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4469. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4470. memcpy(utmp, x[i].scales, 12);
  4471. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4472. const uint32_t uaux = utmp[1] & kmask1;
  4473. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4474. utmp[2] = uaux;
  4475. utmp[0] &= kmask1;
  4476. #else
  4477. // TODO
  4478. const float d = 0, dmin = 0;
  4479. #endif
  4480. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  4481. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  4482. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  4483. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  4484. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  4485. summs += dmin * _mm_extract_epi32(hsum, 0);
  4486. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  4487. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  4488. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  4489. __m256i hmask = mone;
  4490. __m256i sumi = _mm256_setzero_si256();
  4491. int bit = 0;
  4492. for (int j = 0; j < QK_K/64; ++j) {
  4493. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  4494. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  4495. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  4496. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  4497. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  4498. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  4499. hmask = _mm256_slli_epi16(hmask, 1);
  4500. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  4501. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  4502. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  4503. hmask = _mm256_slli_epi16(hmask, 1);
  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. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  4507. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  4508. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  4509. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  4510. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  4511. }
  4512. __m256 vd = _mm256_set1_ps(d);
  4513. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  4514. }
  4515. *s = hsum_float_8(acc) + summs;
  4516. #elif defined __AVX__
  4517. const __m128i m4 = _mm_set1_epi8(0xF);
  4518. const __m128i mzero = _mm_setzero_si128();
  4519. const __m128i mone = _mm_set1_epi8(1);
  4520. const __m128i m2 = _mm_set1_epi8(2);
  4521. __m256 acc = _mm256_setzero_ps();
  4522. float summs = 0.f;
  4523. for (int i = 0; i < nb; ++i) {
  4524. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4525. const float dmin = -y[i].d * GGML_FP16_TO_FP32(x[i].dmin);
  4526. const uint8_t * restrict q5 = x[i].qs;
  4527. const int8_t * restrict q8 = y[i].qs;
  4528. memcpy(utmp, x[i].scales, 12);
  4529. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4530. const uint32_t uaux = utmp[1] & kmask1;
  4531. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4532. utmp[2] = uaux;
  4533. utmp[0] &= kmask1;
  4534. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  4535. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  4536. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  4537. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  4538. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  4539. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  4540. const __m128i prod = _mm_madd_epi16(mins, q8s);
  4541. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  4542. summs += dmin * _mm_extract_epi32(hsum, 0);
  4543. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  4544. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  4545. __m128i hmask = mone;
  4546. __m128i sumi_0 = _mm_setzero_si128();
  4547. __m128i sumi_1 = _mm_setzero_si128();
  4548. int bit = 0;
  4549. __m128i shuffle = _mm_set1_epi16(0x0100);
  4550. for (int j = 0; j < QK_K/64; ++j) {
  4551. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  4552. shuffle = _mm_add_epi16(shuffle, m2);
  4553. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  4554. shuffle = _mm_add_epi16(shuffle, m2);
  4555. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  4556. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  4557. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  4558. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  4559. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  4560. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  4561. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  4562. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  4563. hmask = _mm_slli_epi16(hmask, 1);
  4564. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4565. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4566. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  4567. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  4568. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  4569. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  4570. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  4571. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  4572. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  4573. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  4574. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  4575. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  4576. hmask = _mm_slli_epi16(hmask, 1);
  4577. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4578. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  4579. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  4580. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  4581. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  4582. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  4583. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  4584. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  4585. }
  4586. __m256 vd = _mm256_set1_ps(d);
  4587. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  4588. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  4589. }
  4590. *s = hsum_float_8(acc) + summs;
  4591. #elif defined __riscv_v_intrinsic
  4592. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4593. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4594. float sumf = 0;
  4595. float sums = 0.0;
  4596. size_t vl;
  4597. for (int i = 0; i < nb; ++i) {
  4598. vl = 8;
  4599. const uint8_t * restrict q5 = x[i].qs;
  4600. const uint8_t * restrict hm = x[i].qh;
  4601. const int8_t * restrict q8 = y[i].qs;
  4602. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4603. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4604. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  4605. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  4606. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  4607. memcpy(utmp, x[i].scales, 12);
  4608. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4609. const uint32_t uaux = utmp[1] & kmask1;
  4610. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4611. utmp[2] = uaux;
  4612. utmp[0] &= kmask1;
  4613. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  4614. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  4615. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  4616. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  4617. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  4618. vl = 32;
  4619. int32_t aux32 = 0;
  4620. int is = 0;
  4621. uint8_t m = 1;
  4622. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4623. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  4624. for (int j = 0; j < QK_K/64; ++j) {
  4625. // load Q5 and Q8
  4626. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  4627. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  4628. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  4629. // compute mask for addition
  4630. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  4631. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4632. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  4633. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  4634. m <<= 1;
  4635. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  4636. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  4637. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  4638. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  4639. m <<= 1;
  4640. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  4641. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  4642. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  4643. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  4644. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  4645. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  4646. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  4647. q5 += 32; q8 += 64;
  4648. }
  4649. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  4650. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  4651. }
  4652. *s = sumf+sums;
  4653. #else
  4654. const uint8_t * scales = (const uint8_t*)&utmp[0];
  4655. const uint8_t * mins = (const uint8_t*)&utmp[2];
  4656. int8_t aux8[QK_K];
  4657. int16_t aux16[8];
  4658. float sums [8];
  4659. int32_t aux32[8];
  4660. memset(sums, 0, 8*sizeof(float));
  4661. float sumf = 0;
  4662. for (int i = 0; i < nb; ++i) {
  4663. const uint8_t * restrict q4 = x[i].qs;
  4664. const uint8_t * restrict hm = x[i].qh;
  4665. const int8_t * restrict q8 = y[i].qs;
  4666. memset(aux32, 0, 8*sizeof(int32_t));
  4667. int8_t * restrict a = aux8;
  4668. uint8_t m = 1;
  4669. for (int j = 0; j < QK_K/64; ++j) {
  4670. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  4671. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  4672. a += 32; m <<= 1;
  4673. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  4674. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  4675. a += 32; m <<= 1;
  4676. q4 += 32;
  4677. }
  4678. memcpy(utmp, x[i].scales, 12);
  4679. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  4680. const uint32_t uaux = utmp[1] & kmask1;
  4681. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  4682. utmp[2] = uaux;
  4683. utmp[0] &= kmask1;
  4684. int sumi = 0;
  4685. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  4686. a = aux8;
  4687. int is = 0;
  4688. for (int j = 0; j < QK_K/32; ++j) {
  4689. int32_t scale = scales[is++];
  4690. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4691. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4692. q8 += 8; a += 8;
  4693. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4694. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4695. q8 += 8; a += 8;
  4696. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4697. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4698. q8 += 8; a += 8;
  4699. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  4700. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  4701. q8 += 8; a += 8;
  4702. }
  4703. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  4704. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  4705. const float dmin = GGML_FP16_TO_FP32(x[i].dmin) * y[i].d;
  4706. sumf -= dmin * sumi;
  4707. }
  4708. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4709. *s = sumf;
  4710. #endif
  4711. }
  4712. #else
  4713. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4714. assert(n % QK_K == 0);
  4715. const block_q5_K * restrict x = vx;
  4716. const block_q8_K * restrict y = vy;
  4717. const int nb = n / QK_K;
  4718. #ifdef __ARM_NEON
  4719. const uint8x16_t m4b = vdupq_n_u8(0xf);
  4720. const uint8x16_t mh = vdupq_n_u8(16);
  4721. const int32x4_t mzero = vdupq_n_s32(0);
  4722. ggml_int8x16x4_t q5bytes;
  4723. ggml_uint8x16x4_t q5h;
  4724. float sumf = 0;
  4725. for (int i = 0; i < nb; ++i) {
  4726. const float d = y[i].d * (float)x[i].d;
  4727. const int8_t * sc = x[i].scales;
  4728. const uint8_t * restrict q5 = x[i].qs;
  4729. const uint8_t * restrict qh = x[i].qh;
  4730. const int8_t * restrict q8 = y[i].qs;
  4731. const uint8x8_t qhbits = vld1_u8(qh);
  4732. const ggml_uint8x16x2_t q5bits = ggml_vld1q_u8_x2(q5);
  4733. const ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  4734. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  4735. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  4736. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  4737. q5h.val[2] = vbicq_u8(mh, htmp);
  4738. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  4739. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  4740. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  4741. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  4742. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  4743. int32_t sumi1 = sc[0] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  4744. int32_t sumi2 = sc[1] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  4745. int32_t sumi3 = sc[2] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  4746. int32_t sumi4 = sc[3] * vaddvq_s32(ggml_vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  4747. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  4748. }
  4749. *s = sumf;
  4750. #elif defined __AVX2__
  4751. const __m256i m4 = _mm256_set1_epi8(0xF);
  4752. const __m256i mone = _mm256_set1_epi8(1);
  4753. __m256 acc = _mm256_setzero_ps();
  4754. for (int i = 0; i < nb; ++i) {
  4755. const uint8_t * restrict q5 = x[i].qs;
  4756. const int8_t * restrict q8 = y[i].qs;
  4757. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4758. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  4759. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  4760. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  4761. int64_t aux64;
  4762. memcpy(&aux64, x[i].qh, 8);
  4763. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  4764. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  4765. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  4766. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  4767. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  4768. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  4769. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4770. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4771. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  4772. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  4773. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  4774. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  4775. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  4776. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  4777. }
  4778. *s = hsum_float_8(acc);
  4779. #elif defined __AVX__
  4780. const __m128i m4 = _mm_set1_epi8(0xF);
  4781. const __m128i mone = _mm_set1_epi8(1);
  4782. __m256 acc = _mm256_setzero_ps();
  4783. for (int i = 0; i < nb; ++i) {
  4784. const uint8_t * restrict q5 = x[i].qs;
  4785. const int8_t * restrict q8 = y[i].qs;
  4786. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4787. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  4788. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  4789. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  4790. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  4791. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  4792. int64_t aux64;
  4793. memcpy(&aux64, x[i].qh, 8);
  4794. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  4795. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  4796. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  4797. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  4798. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  4799. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  4800. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  4801. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  4802. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  4803. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  4804. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  4805. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  4806. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  4807. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  4808. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  4809. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  4810. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  4811. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  4812. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  4813. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  4814. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  4815. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  4816. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  4817. }
  4818. *s = hsum_float_8(acc);
  4819. #elif defined __riscv_v_intrinsic
  4820. float sumf = 0;
  4821. for (int i = 0; i < nb; ++i) {
  4822. const float d = y[i].d * (float)x[i].d;
  4823. const int8_t * sc = x[i].scales;
  4824. const uint8_t * restrict q5 = x[i].qs;
  4825. const uint8_t * restrict qh = x[i].qh;
  4826. const int8_t * restrict q8 = y[i].qs;
  4827. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  4828. // load qh
  4829. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  4830. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  4831. size_t vl = 16;
  4832. // combine both qh_1 and qh_2
  4833. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  4834. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  4835. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  4836. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  4837. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  4838. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  4839. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  4840. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  4841. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  4842. // load q5
  4843. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  4844. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  4845. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  4846. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  4847. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  4848. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  4849. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  4850. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  4851. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  4852. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  4853. // load Q8 and multiply it with Q5
  4854. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  4855. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  4856. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  4857. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  4858. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  4859. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  4860. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  4861. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  4862. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  4863. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  4864. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  4865. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  4866. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  4867. }
  4868. *s = sumf;
  4869. #else
  4870. int8_t aux8[QK_K];
  4871. int16_t aux16[16];
  4872. float sums [8];
  4873. memset(sums, 0, 8*sizeof(float));
  4874. float sumf = 0;
  4875. for (int i = 0; i < nb; ++i) {
  4876. const uint8_t * restrict q4 = x[i].qs;
  4877. const uint8_t * restrict hm = x[i].qh;
  4878. const int8_t * restrict q8 = y[i].qs;
  4879. int8_t * restrict a = aux8;
  4880. for (int l = 0; l < 32; ++l) {
  4881. a[l+ 0] = q4[l] & 0xF;
  4882. a[l+32] = q4[l] >> 4;
  4883. }
  4884. for (int is = 0; is < 8; ++is) {
  4885. uint8_t m = 1 << is;
  4886. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  4887. }
  4888. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4889. const int8_t * restrict sc = x[i].scales;
  4890. for (int j = 0; j < QK_K/16; ++j) {
  4891. const float dl = d * sc[j];
  4892. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  4893. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  4894. q8 += 16; a += 16;
  4895. }
  4896. }
  4897. for (int l = 0; l < 8; ++l) sumf += sums[l];
  4898. *s = sumf;
  4899. #endif
  4900. }
  4901. #endif
  4902. #if QK_K == 256
  4903. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  4904. assert(n % QK_K == 0);
  4905. const block_q6_K * restrict x = vx;
  4906. const block_q8_K * restrict y = vy;
  4907. const int nb = n / QK_K;
  4908. #ifdef __ARM_NEON
  4909. float sum = 0;
  4910. const uint8x16_t m4b = vdupq_n_u8(0xF);
  4911. const int32x4_t vzero = vdupq_n_s32(0);
  4912. //const int8x16_t m32s = vdupq_n_s8(32);
  4913. const uint8x16_t mone = vdupq_n_u8(3);
  4914. ggml_int8x16x4_t q6bytes;
  4915. ggml_uint8x16x4_t q6h;
  4916. for (int i = 0; i < nb; ++i) {
  4917. const float d_all = GGML_FP16_TO_FP32(x[i].d);
  4918. const uint8_t * restrict q6 = x[i].ql;
  4919. const uint8_t * restrict qh = x[i].qh;
  4920. const int8_t * restrict q8 = y[i].qs;
  4921. const int8_t * restrict scale = x[i].scales;
  4922. const ggml_int16x8x2_t q8sums = ggml_vld1q_s16_x2(y[i].bsums);
  4923. const int8x16_t scales = vld1q_s8(scale);
  4924. const ggml_int16x8x2_t q6scales = {{vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))}};
  4925. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  4926. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  4927. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  4928. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  4929. int32_t isum_mins = vaddvq_s32(prod);
  4930. int32_t isum = 0;
  4931. for (int j = 0; j < QK_K/128; ++j) {
  4932. ggml_uint8x16x2_t qhbits = ggml_vld1q_u8_x2(qh); qh += 32;
  4933. ggml_uint8x16x4_t q6bits = ggml_vld1q_u8_x4(q6); q6 += 64;
  4934. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  4935. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  4936. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  4937. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  4938. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  4939. shifted = vshrq_n_u8(qhbits.val[1], 2);
  4940. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  4941. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  4942. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  4943. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  4944. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  4945. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  4946. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  4947. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  4948. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  4949. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  4950. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  4951. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  4952. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  4953. scale += 4;
  4954. q8bytes = ggml_vld1q_s8_x4(q8); q8 += 64;
  4955. shifted = vshrq_n_u8(qhbits.val[0], 4);
  4956. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  4957. shifted = vshrq_n_u8(qhbits.val[1], 4);
  4958. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  4959. shifted = vshrq_n_u8(qhbits.val[0], 6);
  4960. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  4961. shifted = vshrq_n_u8(qhbits.val[1], 6);
  4962. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  4963. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  4964. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  4965. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  4966. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  4967. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  4968. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  4969. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  4970. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  4971. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  4972. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  4973. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  4974. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  4975. scale += 4;
  4976. }
  4977. //sum += isum * d_all * y[i].d;
  4978. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  4979. }
  4980. *s = sum;
  4981. #elif defined __AVX2__
  4982. const __m256i m4 = _mm256_set1_epi8(0xF);
  4983. const __m256i m2 = _mm256_set1_epi8(3);
  4984. const __m256i m32s = _mm256_set1_epi8(32);
  4985. __m256 acc = _mm256_setzero_ps();
  4986. for (int i = 0; i < nb; ++i) {
  4987. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  4988. const uint8_t * restrict q4 = x[i].ql;
  4989. const uint8_t * restrict qh = x[i].qh;
  4990. const int8_t * restrict q8 = y[i].qs;
  4991. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  4992. __m256i sumi = _mm256_setzero_si256();
  4993. int is = 0;
  4994. for (int j = 0; j < QK_K/128; ++j) {
  4995. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  4996. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  4997. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  4998. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  4999. is += 4;
  5000. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5001. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  5002. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  5003. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  5004. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  5005. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  5006. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  5007. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5008. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  5009. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  5010. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  5011. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5012. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5013. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5014. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  5015. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5016. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5017. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  5018. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  5019. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5020. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5021. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  5022. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  5023. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5024. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5025. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  5026. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  5027. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5028. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5029. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  5030. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  5031. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5032. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  5033. }
  5034. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5035. }
  5036. *s = hsum_float_8(acc);
  5037. #elif defined __AVX__
  5038. const __m128i m4 = _mm_set1_epi8(0xF);
  5039. const __m128i m3 = _mm_set1_epi8(3);
  5040. const __m128i m32s = _mm_set1_epi8(32);
  5041. const __m128i m2 = _mm_set1_epi8(2);
  5042. __m256 acc = _mm256_setzero_ps();
  5043. for (int i = 0; i < nb; ++i) {
  5044. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5045. const uint8_t * restrict q4 = x[i].ql;
  5046. const uint8_t * restrict qh = x[i].qh;
  5047. const int8_t * restrict q8 = y[i].qs;
  5048. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  5049. __m128i sumi_0 = _mm_setzero_si128();
  5050. __m128i sumi_1 = _mm_setzero_si128();
  5051. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  5052. for (int j = 0; j < QK_K/128; ++j) {
  5053. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5054. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  5055. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  5056. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  5057. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  5058. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  5059. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  5060. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  5061. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  5062. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  5063. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5064. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5065. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5066. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  5067. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  5068. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  5069. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  5070. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  5071. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  5072. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  5073. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  5074. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  5075. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5076. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5077. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5078. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5079. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5080. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5081. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5082. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  5083. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  5084. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  5085. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  5086. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  5087. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  5088. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  5089. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  5090. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  5091. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  5092. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  5093. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  5094. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  5095. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  5096. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  5097. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  5098. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  5099. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5100. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5101. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5102. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5103. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  5104. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  5105. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  5106. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  5107. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  5108. shuffle = _mm_add_epi8(shuffle, m2);
  5109. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  5110. shuffle = _mm_add_epi8(shuffle, m2);
  5111. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  5112. shuffle = _mm_add_epi8(shuffle, m2);
  5113. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  5114. shuffle = _mm_add_epi8(shuffle, m2);
  5115. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5116. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5117. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5118. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  5119. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  5120. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  5121. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  5122. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  5123. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5124. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5125. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  5126. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  5127. }
  5128. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  5129. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  5130. }
  5131. *s = hsum_float_8(acc);
  5132. #elif defined __riscv_v_intrinsic
  5133. float sumf = 0;
  5134. for (int i = 0; i < nb; ++i) {
  5135. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5136. const uint8_t * restrict q6 = x[i].ql;
  5137. const uint8_t * restrict qh = x[i].qh;
  5138. const int8_t * restrict q8 = y[i].qs;
  5139. const int8_t * restrict scale = x[i].scales;
  5140. size_t vl;
  5141. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5142. int sum_t = 0;
  5143. int is = 0;
  5144. for (int j = 0; j < QK_K/128; ++j) {
  5145. vl = 32;
  5146. // load qh
  5147. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  5148. // load Q6
  5149. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  5150. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  5151. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  5152. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  5153. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  5154. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  5155. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  5156. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  5157. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  5158. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  5159. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  5160. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  5161. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  5162. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  5163. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  5164. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  5165. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  5166. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  5167. // load Q8 and take product
  5168. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  5169. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  5170. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  5171. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  5172. vl = 16;
  5173. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  5174. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  5175. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  5176. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  5177. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  5178. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  5179. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  5180. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  5181. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  5182. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  5183. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  5184. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  5185. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  5186. q6 += 64; qh += 32; q8 += 128; is=8;
  5187. }
  5188. sumf += d * sum_t;
  5189. }
  5190. *s = sumf;
  5191. #else
  5192. int8_t aux8[QK_K];
  5193. int16_t aux16[8];
  5194. float sums [8];
  5195. int32_t aux32[8];
  5196. memset(sums, 0, 8*sizeof(float));
  5197. float sumf = 0;
  5198. for (int i = 0; i < nb; ++i) {
  5199. const uint8_t * restrict q4 = x[i].ql;
  5200. const uint8_t * restrict qh = x[i].qh;
  5201. const int8_t * restrict q8 = y[i].qs;
  5202. memset(aux32, 0, 8*sizeof(int32_t));
  5203. int8_t * restrict a = aux8;
  5204. for (int j = 0; j < QK_K; j += 128) {
  5205. for (int l = 0; l < 32; ++l) {
  5206. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  5207. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  5208. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  5209. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  5210. }
  5211. a += 128;
  5212. q4 += 64;
  5213. qh += 32;
  5214. }
  5215. a = aux8;
  5216. int is = 0;
  5217. for (int j = 0; j < QK_K/16; ++j) {
  5218. int scale = x[i].scales[is++];
  5219. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5220. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5221. q8 += 8; a += 8;
  5222. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5223. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5224. q8 += 8; a += 8;
  5225. }
  5226. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5227. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5228. }
  5229. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5230. *s = sumf;
  5231. #endif
  5232. }
  5233. #else
  5234. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  5235. assert(n % QK_K == 0);
  5236. const block_q6_K * restrict x = vx;
  5237. const block_q8_K * restrict y = vy;
  5238. const int nb = n / QK_K;
  5239. #ifdef __ARM_NEON
  5240. float sum = 0;
  5241. const uint8x16_t m4b = vdupq_n_u8(0xF);
  5242. const int8x16_t m32s = vdupq_n_s8(32);
  5243. const int32x4_t vzero = vdupq_n_s32(0);
  5244. const uint8x16_t mone = vdupq_n_u8(3);
  5245. ggml_int8x16x4_t q6bytes;
  5246. ggml_uint8x16x4_t q6h;
  5247. for (int i = 0; i < nb; ++i) {
  5248. const float d_all = (float)x[i].d;
  5249. const uint8_t * restrict q6 = x[i].ql;
  5250. const uint8_t * restrict qh = x[i].qh;
  5251. const int8_t * restrict q8 = y[i].qs;
  5252. const int8_t * restrict scale = x[i].scales;
  5253. int32_t isum = 0;
  5254. uint8x16_t qhbits = vld1q_u8(qh);
  5255. ggml_uint8x16x2_t q6bits = ggml_vld1q_u8_x2(q6);
  5256. ggml_int8x16x4_t q8bytes = ggml_vld1q_s8_x4(q8);
  5257. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  5258. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  5259. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5260. shifted = vshrq_n_u8(qhbits, 4);
  5261. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5262. shifted = vshrq_n_u8(qhbits, 6);
  5263. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  5264. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  5265. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  5266. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  5267. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  5268. isum += vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  5269. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  5270. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  5271. vaddvq_s32(ggml_vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  5272. sum += isum * d_all * y[i].d;
  5273. }
  5274. *s = sum;
  5275. #elif defined __AVX2__
  5276. const __m256i m4 = _mm256_set1_epi8(0xF);
  5277. const __m256i m2 = _mm256_set1_epi8(3);
  5278. const __m256i m32s = _mm256_set1_epi8(32);
  5279. __m256 acc = _mm256_setzero_ps();
  5280. for (int i = 0; i < nb; ++i) {
  5281. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5282. const uint8_t * restrict q4 = x[i].ql;
  5283. const uint8_t * restrict qh = x[i].qh;
  5284. const int8_t * restrict q8 = y[i].qs;
  5285. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  5286. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  5287. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  5288. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  5289. __m256i sumi = _mm256_setzero_si256();
  5290. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  5291. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  5292. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  5293. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  5294. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  5295. 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);
  5296. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  5297. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  5298. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5299. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5300. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  5301. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  5302. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  5303. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  5304. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  5305. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  5306. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  5307. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  5308. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  5309. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  5310. }
  5311. *s = hsum_float_8(acc);
  5312. #elif defined __AVX__
  5313. const __m128i m4 = _mm_set1_epi8(0xF);
  5314. const __m128i m2 = _mm_set1_epi8(3);
  5315. const __m128i m32s = _mm_set1_epi8(32);
  5316. __m256 acc = _mm256_setzero_ps();
  5317. for (int i = 0; i < nb; ++i) {
  5318. const float d = y[i].d * GGML_FP16_TO_FP32(x[i].d);
  5319. const uint8_t * restrict q4 = x[i].ql;
  5320. const uint8_t * restrict qh = x[i].qh;
  5321. const int8_t * restrict q8 = y[i].qs;
  5322. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  5323. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  5324. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  5325. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  5326. __m128i sumi_0 = _mm_setzero_si128();
  5327. __m128i sumi_1 = _mm_setzero_si128();
  5328. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  5329. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  5330. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  5331. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  5332. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  5333. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  5334. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  5335. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  5336. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  5337. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  5338. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  5339. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  5340. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  5341. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  5342. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  5343. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  5344. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  5345. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  5346. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  5347. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  5348. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  5349. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  5350. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  5351. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  5352. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  5353. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  5354. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  5355. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  5356. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  5357. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  5358. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  5359. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  5360. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  5361. }
  5362. *s = hsum_float_8(acc);
  5363. #elif defined __riscv_v_intrinsic
  5364. float sumf = 0;
  5365. for (int i = 0; i < nb; ++i) {
  5366. const float d_all = (float)x[i].d;
  5367. const uint8_t * restrict q6 = x[i].ql;
  5368. const uint8_t * restrict qh = x[i].qh;
  5369. const int8_t * restrict q8 = y[i].qs;
  5370. const int8_t * restrict scale = x[i].scales;
  5371. int32_t isum = 0;
  5372. size_t vl = 16;
  5373. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  5374. // load Q6
  5375. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  5376. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  5377. // load qh
  5378. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  5379. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5380. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5381. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5382. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5383. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5384. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  5385. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  5386. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  5387. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  5388. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  5389. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  5390. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  5391. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  5392. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  5393. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  5394. // load Q8 and take product
  5395. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  5396. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  5397. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  5398. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  5399. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  5400. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  5401. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  5402. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  5403. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  5404. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  5405. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  5406. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  5407. sumf += isum * d_all * y[i].d;
  5408. }
  5409. *s = sumf;
  5410. #else
  5411. int8_t aux8[QK_K];
  5412. int16_t aux16[8];
  5413. float sums [8];
  5414. int32_t aux32[8];
  5415. memset(sums, 0, 8*sizeof(float));
  5416. float sumf = 0;
  5417. for (int i = 0; i < nb; ++i) {
  5418. const uint8_t * restrict q4 = x[i].ql;
  5419. const uint8_t * restrict qh = x[i].qh;
  5420. const int8_t * restrict q8 = y[i].qs;
  5421. memset(aux32, 0, 8*sizeof(int32_t));
  5422. int8_t * restrict a = aux8;
  5423. for (int l = 0; l < 16; ++l) {
  5424. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  5425. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  5426. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  5427. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  5428. }
  5429. int is = 0;
  5430. for (int j = 0; j < QK_K/16; ++j) {
  5431. int scale = x[i].scales[is++];
  5432. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5433. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5434. q8 += 8; a += 8;
  5435. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  5436. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  5437. q8 += 8; a += 8;
  5438. }
  5439. const float d = GGML_FP16_TO_FP32(x[i].d) * y[i].d;
  5440. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  5441. }
  5442. for (int l = 0; l < 8; ++l) sumf += sums[l];
  5443. *s = sumf;
  5444. #endif
  5445. }
  5446. #endif