ggml-quants.c 282 KB

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