ggml-quants.c 284 KB

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