k_quants.c 202 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060
  1. #include "k_quants.h"
  2. #include "ggml.h"
  3. #include <math.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #ifdef __ARM_NEON
  7. // if YCM cannot find <arm_neon.h>, make a symbolic link to it, for example:
  8. //
  9. // $ ln -sfn /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include/arm_neon.h ./src/
  10. //
  11. #include <arm_neon.h>
  12. #if !defined(__aarch64__)
  13. inline static int32_t vaddvq_s16(int16x8_t v) {
  14. return
  15. (int32_t)vgetq_lane_s16(v, 0) + (int32_t)vgetq_lane_s16(v, 1) +
  16. (int32_t)vgetq_lane_s16(v, 2) + (int32_t)vgetq_lane_s16(v, 3) +
  17. (int32_t)vgetq_lane_s16(v, 4) + (int32_t)vgetq_lane_s16(v, 5) +
  18. (int32_t)vgetq_lane_s16(v, 6) + (int32_t)vgetq_lane_s16(v, 7);
  19. }
  20. inline static int16x8_t vpaddq_s16(int16x8_t a, int16x8_t b) {
  21. int16x4_t a0 = vpadd_s16(vget_low_s16(a), vget_high_s16(a));
  22. int16x4_t b0 = vpadd_s16(vget_low_s16(b), vget_high_s16(b));
  23. return vcombine_s16(a0, b0);
  24. }
  25. inline static int32_t vaddvq_s32(int32x4_t v) {
  26. return vgetq_lane_s32(v, 0) + vgetq_lane_s32(v, 1) + vgetq_lane_s32(v, 2) + vgetq_lane_s32(v, 3);
  27. }
  28. #endif
  29. #else
  30. #ifdef __wasm_simd128__
  31. #include <wasm_simd128.h>
  32. #else
  33. #ifdef __POWER9_VECTOR__
  34. #include <altivec.h>
  35. #undef bool
  36. #define bool _Bool
  37. #else
  38. #if defined(_MSC_VER) || defined(__MINGW32__)
  39. #include <intrin.h>
  40. #else
  41. #if !defined(__riscv)
  42. #include <immintrin.h>
  43. #endif
  44. #endif
  45. #endif
  46. #endif
  47. #endif
  48. #ifdef __riscv_v_intrinsic
  49. #include <riscv_vector.h>
  50. #endif
  51. #undef MIN
  52. #undef MAX
  53. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  54. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  55. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  56. //
  57. // 2-6 bit quantization in super-blocks
  58. //
  59. //
  60. // ===================== Helper functions
  61. //
  62. static inline int nearest_int(float fval) {
  63. assert(fval <= 4194303.f);
  64. float val = fval + 12582912.f;
  65. int i; memcpy(&i, &val, sizeof(int));
  66. return (i & 0x007fffff) - 0x00400000;
  67. }
  68. static float make_qx_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, int rmse_type) {
  69. float max = 0;
  70. float amax = 0;
  71. for (int i = 0; i < n; ++i) {
  72. float ax = fabsf(x[i]);
  73. if (ax > amax) { amax = ax; max = x[i]; }
  74. }
  75. if (amax < 1e-30f) { // all zero
  76. for (int i = 0; i < n; ++i) {
  77. L[i] = 0;
  78. }
  79. return 0.f;
  80. }
  81. float iscale = -nmax / max;
  82. if (rmse_type == 0) {
  83. for (int i = 0; i < n; ++i) {
  84. int l = nearest_int(iscale * x[i]);
  85. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  86. }
  87. return 1/iscale;
  88. }
  89. bool return_early = false;
  90. if (rmse_type < 0) {
  91. rmse_type = -rmse_type;
  92. return_early = true;
  93. }
  94. int weight_type = rmse_type%2;
  95. float sumlx = 0;
  96. float suml2 = 0;
  97. for (int i = 0; i < n; ++i) {
  98. int l = nearest_int(iscale * x[i]);
  99. l = MAX(-nmax, MIN(nmax-1, l));
  100. L[i] = l + nmax;
  101. float w = weight_type == 1 ? x[i] * x[i] : 1;
  102. sumlx += w*x[i]*l;
  103. suml2 += w*l*l;
  104. }
  105. float scale = sumlx/suml2;
  106. if (return_early) return suml2 > 0 ? 0.5f*(scale + 1/iscale) : 1/iscale;
  107. float best = scale * sumlx;
  108. for (int is = -9; is <= 9; ++is) {
  109. if (is == 0) {
  110. continue;
  111. }
  112. iscale = -(nmax + 0.1f*is) / max;
  113. sumlx = suml2 = 0;
  114. for (int i = 0; i < n; ++i) {
  115. int l = nearest_int(iscale * x[i]);
  116. l = MAX(-nmax, MIN(nmax-1, l));
  117. float w = weight_type == 1 ? x[i] * x[i] : 1;
  118. sumlx += w*x[i]*l;
  119. suml2 += w*l*l;
  120. }
  121. if (suml2 > 0 && sumlx*sumlx > best*suml2) {
  122. for (int i = 0; i < n; ++i) {
  123. int l = nearest_int(iscale * x[i]);
  124. L[i] = nmax + MAX(-nmax, MIN(nmax-1, l));
  125. }
  126. scale = sumlx/suml2; best = scale*sumlx;
  127. }
  128. }
  129. return scale;
  130. }
  131. static float make_q3_quants(int n, int nmax, const float * restrict x, int8_t * restrict L, bool do_rmse) {
  132. float max = 0;
  133. float amax = 0;
  134. for (int i = 0; i < n; ++i) {
  135. float ax = fabsf(x[i]);
  136. if (ax > amax) { amax = ax; max = x[i]; }
  137. }
  138. if (!amax) { // all zero
  139. for (int i = 0; i < n; ++i) { L[i] = 0; }
  140. return 0.f;
  141. }
  142. float iscale = -nmax / max;
  143. if (do_rmse) {
  144. float sumlx = 0;
  145. float suml2 = 0;
  146. for (int i = 0; i < n; ++i) {
  147. int l = nearest_int(iscale * x[i]);
  148. l = MAX(-nmax, MIN(nmax-1, l));
  149. L[i] = l;
  150. float w = x[i]*x[i];
  151. sumlx += w*x[i]*l;
  152. suml2 += w*l*l;
  153. }
  154. for (int itry = 0; itry < 5; ++itry) {
  155. int n_changed = 0;
  156. for (int i = 0; i < n; ++i) {
  157. float w = x[i]*x[i];
  158. float slx = sumlx - w*x[i]*L[i];
  159. if (slx > 0) {
  160. float sl2 = suml2 - w*L[i]*L[i];
  161. int new_l = nearest_int(x[i] * sl2 / slx);
  162. new_l = MAX(-nmax, MIN(nmax-1, new_l));
  163. if (new_l != L[i]) {
  164. slx += w*x[i]*new_l;
  165. sl2 += w*new_l*new_l;
  166. if (sl2 > 0 && slx*slx*suml2 > sumlx*sumlx*sl2) {
  167. L[i] = new_l; sumlx = slx; suml2 = sl2;
  168. ++n_changed;
  169. }
  170. }
  171. }
  172. }
  173. if (!n_changed) {
  174. break;
  175. }
  176. }
  177. for (int i = 0; i < n; ++i) {
  178. L[i] += nmax;
  179. }
  180. return sumlx / suml2;
  181. }
  182. for (int i = 0; i < n; ++i) {
  183. int l = nearest_int(iscale * x[i]);
  184. l = MAX(-nmax, MIN(nmax-1, l));
  185. L[i] = l + nmax;
  186. }
  187. return 1/iscale;
  188. }
  189. static float make_qkx1_quants(int n, int nmax, const float * restrict x, uint8_t * restrict L, float * restrict the_min,
  190. int ntry, float alpha) {
  191. float min = x[0];
  192. float max = x[0];
  193. for (int i = 1; i < n; ++i) {
  194. if (x[i] < min) min = x[i];
  195. if (x[i] > max) max = x[i];
  196. }
  197. if (max == min) {
  198. for (int i = 0; i < n; ++i) L[i] = 0;
  199. *the_min = 0;
  200. return 0.f;
  201. }
  202. if (min > 0) min = 0;
  203. float iscale = nmax/(max - min);
  204. float scale = 1/iscale;
  205. for (int itry = 0; itry < ntry; ++itry) {
  206. float sumlx = 0; int suml2 = 0;
  207. bool did_change = false;
  208. for (int i = 0; i < n; ++i) {
  209. int l = nearest_int(iscale*(x[i] - min));
  210. l = MAX(0, MIN(nmax, l));
  211. if (l != L[i]) {
  212. L[i] = l;
  213. did_change = true;
  214. }
  215. sumlx += (x[i] - min)*l;
  216. suml2 += l*l;
  217. }
  218. scale = sumlx/suml2;
  219. float sum = 0;
  220. for (int i = 0; i < n; ++i) {
  221. sum += x[i] - scale*L[i];
  222. }
  223. min = alpha*min + (1 - alpha)*sum/n;
  224. if (min > 0) min = 0;
  225. iscale = 1/scale;
  226. if (!did_change) break;
  227. }
  228. *the_min = -min;
  229. return scale;
  230. }
  231. static float make_qkx2_quants(int n, int nmax, const float * restrict x, const float * restrict weights,
  232. uint8_t * restrict L, float * restrict the_min, uint8_t * restrict Laux,
  233. float rmin, float rdelta, int nstep, bool use_mad) {
  234. float min = x[0];
  235. float max = x[0];
  236. float sum_w = weights[0];
  237. float sum_x = sum_w * x[0];
  238. for (int i = 1; i < n; ++i) {
  239. if (x[i] < min) min = x[i];
  240. if (x[i] > max) max = x[i];
  241. float w = weights[i];
  242. sum_w += w;
  243. sum_x += w * x[i];
  244. }
  245. if (min > 0) min = 0;
  246. if (max == min) {
  247. for (int i = 0; i < n; ++i) L[i] = 0;
  248. *the_min = -min;
  249. return 0.f;
  250. }
  251. float iscale = nmax/(max - min);
  252. float scale = 1/iscale;
  253. float best_mad = 0;
  254. for (int i = 0; i < n; ++i) {
  255. int l = nearest_int(iscale*(x[i] - min));
  256. L[i] = MAX(0, MIN(nmax, l));
  257. float diff = scale * L[i] + min - x[i];
  258. diff = use_mad ? fabsf(diff) : diff * diff;
  259. float w = weights[i];
  260. best_mad += w * diff;
  261. }
  262. if (nstep < 1) {
  263. *the_min = -min;
  264. return scale;
  265. }
  266. for (int is = 0; is <= nstep; ++is) {
  267. iscale = (rmin + rdelta*is + nmax)/(max - min);
  268. float sum_l = 0, sum_l2 = 0, sum_xl = 0;
  269. for (int i = 0; i < n; ++i) {
  270. int l = nearest_int(iscale*(x[i] - min));
  271. l = MAX(0, MIN(nmax, l));
  272. Laux[i] = l;
  273. float w = weights[i];
  274. sum_l += w*l;
  275. sum_l2 += w*l*l;
  276. sum_xl += w*l*x[i];
  277. }
  278. float D = sum_w * sum_l2 - sum_l * sum_l;
  279. if (D > 0) {
  280. float this_scale = (sum_w * sum_xl - sum_x * sum_l)/D;
  281. float this_min = (sum_l2 * sum_x - sum_l * sum_xl)/D;
  282. if (this_min > 0) {
  283. this_min = 0;
  284. this_scale = sum_xl / sum_l2;
  285. }
  286. float mad = 0;
  287. for (int i = 0; i < n; ++i) {
  288. float diff = this_scale * Laux[i] + this_min - x[i];
  289. diff = use_mad ? fabsf(diff) : diff * diff;
  290. float w = weights[i];
  291. mad += w * diff;
  292. }
  293. if (mad < best_mad) {
  294. for (int i = 0; i < n; ++i) {
  295. L[i] = Laux[i];
  296. }
  297. best_mad = mad;
  298. scale = this_scale;
  299. min = this_min;
  300. }
  301. }
  302. }
  303. *the_min = -min;
  304. return scale;
  305. }
  306. #if QK_K == 256
  307. static inline void get_scale_min_k4(int j, const uint8_t * restrict q, uint8_t * restrict d, uint8_t * restrict m) {
  308. if (j < 4) {
  309. *d = q[j] & 63; *m = q[j + 4] & 63;
  310. } else {
  311. *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4);
  312. *m = (q[j+4] >> 4) | ((q[j-0] >> 6) << 4);
  313. }
  314. }
  315. #endif
  316. //========================- 2-bit (de)-quantization
  317. void quantize_row_q2_K_reference(const float * restrict x, block_q2_K * restrict y, int k) {
  318. assert(k % QK_K == 0);
  319. const int nb = k / QK_K;
  320. uint8_t L[QK_K];
  321. uint8_t Laux[16];
  322. float weights[16];
  323. float mins[QK_K/16];
  324. float scales[QK_K/16];
  325. const float q4scale = 15.f;
  326. for (int i = 0; i < nb; i++) {
  327. float max_scale = 0; // as we are deducting the min, scales are always positive
  328. float max_min = 0;
  329. for (int j = 0; j < QK_K/16; ++j) {
  330. for (int l = 0; l < 16; ++l) weights[l] = fabsf(x[16*j + l]);
  331. scales[j] = make_qkx2_quants(16, 3, x + 16*j, weights, L + 16*j, &mins[j], Laux, -0.5f, 0.1f, 15, true);
  332. float scale = scales[j];
  333. if (scale > max_scale) {
  334. max_scale = scale;
  335. }
  336. float min = mins[j];
  337. if (min > max_min) {
  338. max_min = min;
  339. }
  340. }
  341. if (max_scale > 0) {
  342. float iscale = q4scale/max_scale;
  343. for (int j = 0; j < QK_K/16; ++j) {
  344. int l = nearest_int(iscale*scales[j]);
  345. y[i].scales[j] = l;
  346. }
  347. y[i].d = ggml_fp32_to_fp16(max_scale/q4scale);
  348. } else {
  349. for (int j = 0; j < QK_K/16; ++j) y[i].scales[j] = 0;
  350. y[i].d = ggml_fp32_to_fp16(0.f);
  351. }
  352. if (max_min > 0) {
  353. float iscale = q4scale/max_min;
  354. for (int j = 0; j < QK_K/16; ++j) {
  355. int l = nearest_int(iscale*mins[j]);
  356. y[i].scales[j] |= (l << 4);
  357. }
  358. y[i].dmin = ggml_fp32_to_fp16(max_min/q4scale);
  359. } else {
  360. y[i].dmin = ggml_fp32_to_fp16(0.f);
  361. }
  362. for (int j = 0; j < QK_K/16; ++j) {
  363. const float d = ggml_fp16_to_fp32(y[i].d) * (y[i].scales[j] & 0xF);
  364. if (!d) continue;
  365. const float dm = ggml_fp16_to_fp32(y[i].dmin) * (y[i].scales[j] >> 4);
  366. for (int ii = 0; ii < 16; ++ii) {
  367. int l = nearest_int((x[16*j + ii] + dm)/d);
  368. l = MAX(0, MIN(3, l));
  369. L[16*j + ii] = l;
  370. }
  371. }
  372. #if QK_K == 256
  373. for (int j = 0; j < QK_K; j += 128) {
  374. for (int l = 0; l < 32; ++l) {
  375. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  376. }
  377. }
  378. #else
  379. for (int l = 0; l < 16; ++l) {
  380. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  381. }
  382. #endif
  383. x += QK_K;
  384. }
  385. }
  386. void dequantize_row_q2_K(const block_q2_K * restrict x, float * restrict y, int k) {
  387. assert(k % QK_K == 0);
  388. const int nb = k / QK_K;
  389. for (int i = 0; i < nb; i++) {
  390. const float d = ggml_fp16_to_fp32(x[i].d);
  391. const float min = ggml_fp16_to_fp32(x[i].dmin);
  392. const uint8_t * q = x[i].qs;
  393. #if QK_K == 256
  394. int is = 0;
  395. float dl, ml;
  396. for (int n = 0; n < QK_K; n += 128) {
  397. int shift = 0;
  398. for (int j = 0; j < 4; ++j) {
  399. uint8_t sc = x[i].scales[is++];
  400. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  401. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l] >> shift) & 3)) - ml;
  402. sc = x[i].scales[is++];
  403. dl = d * (sc & 0xF); ml = min * (sc >> 4);
  404. for (int l = 0; l < 16; ++l) *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3)) - ml;
  405. shift += 2;
  406. }
  407. q += 32;
  408. }
  409. #else
  410. float dl1 = d * (x[i].scales[0] & 0xF), ml1 = min * (x[i].scales[0] >> 4);
  411. float dl2 = d * (x[i].scales[1] & 0xF), ml2 = min * (x[i].scales[1] >> 4);
  412. float dl3 = d * (x[i].scales[2] & 0xF), ml3 = min * (x[i].scales[2] >> 4);
  413. float dl4 = d * (x[i].scales[3] & 0xF), ml4 = min * (x[i].scales[3] >> 4);
  414. for (int l = 0; l < 16; ++l) {
  415. y[l+ 0] = dl1 * ((int8_t)((q[l] >> 0) & 3)) - ml1;
  416. y[l+16] = dl2 * ((int8_t)((q[l] >> 2) & 3)) - ml2;
  417. y[l+32] = dl3 * ((int8_t)((q[l] >> 4) & 3)) - ml3;
  418. y[l+48] = dl4 * ((int8_t)((q[l] >> 6) & 3)) - ml4;
  419. }
  420. y += QK_K;
  421. #endif
  422. }
  423. }
  424. void quantize_row_q2_K(const float * restrict x, void * restrict vy, int k) {
  425. quantize_row_q2_K_reference(x, vy, k);
  426. }
  427. size_t ggml_quantize_q2_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  428. const int nb = k / QK_K;
  429. // TODO - collect histograms - although, at a second thought, I don't really care about them
  430. (void)hist;
  431. for (int j = 0; j < nb; j += k) {
  432. block_q2_K * restrict y = (block_q2_K *)dst + j/QK_K;
  433. quantize_row_q2_K_reference(src + j, y, k);
  434. }
  435. return (n/QK_K*sizeof(block_q2_K));
  436. }
  437. //========================= 3-bit (de)-quantization
  438. void quantize_row_q3_K_reference(const float * restrict x, block_q3_K * restrict y, int k) {
  439. assert(k % QK_K == 0);
  440. const int nb = k / QK_K;
  441. int8_t L[QK_K];
  442. float scales[QK_K / 16];
  443. for (int i = 0; i < nb; i++) {
  444. float max_scale = 0;
  445. float amax = 0;
  446. for (int j = 0; j < QK_K/16; ++j) {
  447. scales[j] = make_q3_quants(16, 4, x + 16*j, L + 16*j, true);
  448. float scale = fabsf(scales[j]);
  449. if (scale > amax) {
  450. amax = scale; max_scale = scales[j];
  451. }
  452. }
  453. #if QK_K == 256
  454. memset(y[i].scales, 0, 12);
  455. if (max_scale) {
  456. float iscale = -32.f/max_scale;
  457. for (int j = 0; j < QK_K/16; ++j) {
  458. int8_t l = nearest_int(iscale*scales[j]);
  459. l = MAX(-32, MIN(31, l)) + 32;
  460. if (j < 8) {
  461. y[i].scales[j] = l & 0xF;
  462. } else {
  463. y[i].scales[j-8] |= ((l & 0xF) << 4);
  464. }
  465. l >>= 4;
  466. y[i].scales[j%4 + 8] |= (l << (2*(j/4)));
  467. }
  468. y[i].d = ggml_fp32_to_fp16(1/iscale);
  469. } else {
  470. y[i].d = ggml_fp32_to_fp16(0.f);
  471. }
  472. int8_t sc;
  473. for (int j = 0; j < QK_K/16; ++j) {
  474. sc = j < 8 ? y[i].scales[j] & 0xF : y[i].scales[j-8] >> 4;
  475. sc = (sc | (((y[i].scales[8 + j%4] >> (2*(j/4))) & 3) << 4)) - 32;
  476. float d = ggml_fp16_to_fp32(y[i].d) * sc;
  477. if (!d) {
  478. continue;
  479. }
  480. for (int ii = 0; ii < 16; ++ii) {
  481. int l = nearest_int(x[16*j + ii]/d);
  482. l = MAX(-4, MIN(3, l));
  483. L[16*j + ii] = l + 4;
  484. }
  485. }
  486. #else
  487. if (max_scale) {
  488. float iscale = -8.f/max_scale;
  489. for (int j = 0; j < QK_K/16; j+=2) {
  490. int l1 = nearest_int(iscale*scales[j]);
  491. l1 = 8 + MAX(-8, MIN(7, l1));
  492. int l2 = nearest_int(iscale*scales[j+1]);
  493. l2 = 8 + MAX(-8, MIN(7, l2));
  494. y[i].scales[j/2] = l1 | (l2 << 4);
  495. }
  496. y[i].d = ggml_fp32_to_fp16(1/iscale);
  497. } else {
  498. for (int j = 0; j < QK_K/16; j+=2) {
  499. y[i].scales[j/2] = 0;
  500. }
  501. y[i].d = ggml_fp32_to_fp16(0.f);
  502. }
  503. for (int j = 0; j < QK_K/16; ++j) {
  504. int s = j%2 == 0 ? y[i].scales[j/2] & 0xF : y[i].scales[j/2] >> 4;
  505. float d = ggml_fp16_to_fp32(y[i].d) * (s - 8);
  506. if (!d) {
  507. continue;
  508. }
  509. for (int ii = 0; ii < 16; ++ii) {
  510. int l = nearest_int(x[16*j + ii]/d);
  511. l = MAX(-4, MIN(3, l));
  512. L[16*j + ii] = l + 4;
  513. }
  514. }
  515. #endif
  516. memset(y[i].hmask, 0, QK_K/8);
  517. // We put the high-bit for the 1st 8 quants into bit 0, the next 8 into bit 1, etc.
  518. int m = 0;
  519. uint8_t hm = 1;
  520. for (int j = 0; j < QK_K; ++j) {
  521. if (L[j] > 3) {
  522. y[i].hmask[m] |= hm;
  523. L[j] -= 4;
  524. }
  525. if (++m == QK_K/8) {
  526. m = 0; hm <<= 1;
  527. }
  528. }
  529. #if QK_K == 256
  530. for (int j = 0; j < QK_K; j += 128) {
  531. for (int l = 0; l < 32; ++l) {
  532. y[i].qs[j/4 + l] = L[j + l] | (L[j + l + 32] << 2) | (L[j + l + 64] << 4) | (L[j + l + 96] << 6);
  533. }
  534. }
  535. #else
  536. for (int l = 0; l < 16; ++l) {
  537. y[i].qs[l] = L[l] | (L[l + 16] << 2) | (L[l + 32] << 4) | (L[l + 48] << 6);
  538. }
  539. #endif
  540. x += QK_K;
  541. }
  542. }
  543. #if QK_K == 256
  544. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  545. assert(k % QK_K == 0);
  546. const int nb = k / QK_K;
  547. const uint32_t kmask1 = 0x03030303;
  548. const uint32_t kmask2 = 0x0f0f0f0f;
  549. uint32_t aux[4];
  550. const int8_t * scales = (const int8_t*)aux;
  551. for (int i = 0; i < nb; i++) {
  552. const float d_all = ggml_fp16_to_fp32(x[i].d);
  553. const uint8_t * restrict q = x[i].qs;
  554. const uint8_t * restrict hm = x[i].hmask;
  555. uint8_t m = 1;
  556. memcpy(aux, x[i].scales, 12);
  557. uint32_t tmp = aux[2];
  558. aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  559. aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  560. aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  561. aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  562. int is = 0;
  563. float dl;
  564. for (int n = 0; n < QK_K; n += 128) {
  565. int shift = 0;
  566. for (int j = 0; j < 4; ++j) {
  567. dl = d_all * (scales[is++] - 32);
  568. for (int l = 0; l < 16; ++l) {
  569. *y++ = dl * ((int8_t)((q[l+ 0] >> shift) & 3) - ((hm[l+ 0] & m) ? 0 : 4));
  570. }
  571. dl = d_all * (scales[is++] - 32);
  572. for (int l = 0; l < 16; ++l) {
  573. *y++ = dl * ((int8_t)((q[l+16] >> shift) & 3) - ((hm[l+16] & m) ? 0 : 4));
  574. }
  575. shift += 2;
  576. m <<= 1;
  577. }
  578. q += 32;
  579. }
  580. }
  581. }
  582. #else
  583. void dequantize_row_q3_K(const block_q3_K * restrict x, float * restrict y, int k) {
  584. assert(k % QK_K == 0);
  585. assert(QK_K == 64);
  586. const int nb = k / QK_K;
  587. for (int i = 0; i < nb; i++) {
  588. const float d_all = ggml_fp16_to_fp32(x[i].d);
  589. const uint8_t * restrict q = x[i].qs;
  590. const uint8_t * restrict hm = x[i].hmask;
  591. const float d1 = d_all * ((x[i].scales[0] & 0xF) - 8);
  592. const float d2 = d_all * ((x[i].scales[0] >> 4) - 8);
  593. const float d3 = d_all * ((x[i].scales[1] & 0xF) - 8);
  594. const float d4 = d_all * ((x[i].scales[1] >> 4) - 8);
  595. for (int l=0; l<8; ++l) {
  596. uint8_t h = hm[l];
  597. y[l+ 0] = d1 * ((int8_t)((q[l+0] >> 0) & 3) - ((h & 0x01) ? 0 : 4));
  598. y[l+ 8] = d1 * ((int8_t)((q[l+8] >> 0) & 3) - ((h & 0x02) ? 0 : 4));
  599. y[l+16] = d2 * ((int8_t)((q[l+0] >> 2) & 3) - ((h & 0x04) ? 0 : 4));
  600. y[l+24] = d2 * ((int8_t)((q[l+8] >> 2) & 3) - ((h & 0x08) ? 0 : 4));
  601. y[l+32] = d3 * ((int8_t)((q[l+0] >> 4) & 3) - ((h & 0x10) ? 0 : 4));
  602. y[l+40] = d3 * ((int8_t)((q[l+8] >> 4) & 3) - ((h & 0x20) ? 0 : 4));
  603. y[l+48] = d4 * ((int8_t)((q[l+0] >> 6) & 3) - ((h & 0x40) ? 0 : 4));
  604. y[l+56] = d4 * ((int8_t)((q[l+8] >> 6) & 3) - ((h & 0x80) ? 0 : 4));
  605. }
  606. y += QK_K;
  607. }
  608. }
  609. #endif
  610. void quantize_row_q3_K(const float * restrict x, void * restrict vy, int k) {
  611. quantize_row_q3_K_reference(x, vy, k);
  612. }
  613. size_t ggml_quantize_q3_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  614. const int nb = k / QK_K;
  615. // TODO - collect histograms - although, at a second thought, I don't really care about them
  616. (void)hist;
  617. for (int j = 0; j < nb; j += k) {
  618. block_q3_K * restrict y = (block_q3_K *)dst + j/QK_K;
  619. quantize_row_q3_K_reference(src + j, y, k);
  620. }
  621. return (n/QK_K*sizeof(block_q3_K));
  622. }
  623. // ====================== 4-bit (de)-quantization
  624. void quantize_row_q4_K_reference(const float * restrict x, block_q4_K * restrict y, int k) {
  625. assert(k % QK_K == 0);
  626. const int nb = k / QK_K;
  627. uint8_t L[QK_K];
  628. uint8_t Laux[32];
  629. float weights[32];
  630. float mins[QK_K/32];
  631. float scales[QK_K/32];
  632. for (int i = 0; i < nb; i++) {
  633. float max_scale = 0; // as we are deducting the min, scales are always positive
  634. float max_min = 0;
  635. for (int j = 0; j < QK_K/32; ++j) {
  636. //scales[j] = make_qkx1_quants(32, 15, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  637. float sum_x2 = 0;
  638. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  639. float av_x = sqrtf(sum_x2/32);
  640. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  641. scales[j] = make_qkx2_quants(32, 15, x + 32*j, weights, L + 32*j, &mins[j], Laux, -1.f, 0.1f, 20, false);
  642. float scale = scales[j];
  643. if (scale > max_scale) {
  644. max_scale = scale;
  645. }
  646. float min = mins[j];
  647. if (min > max_min) {
  648. max_min = min;
  649. }
  650. }
  651. #if QK_K == 256
  652. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  653. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  654. for (int j = 0; j < QK_K/32; ++j) {
  655. uint8_t ls = nearest_int(inv_scale*scales[j]);
  656. uint8_t lm = nearest_int(inv_min*mins[j]);
  657. ls = MIN(63, ls);
  658. lm = MIN(63, lm);
  659. if (j < 4) {
  660. y[i].scales[j] = ls;
  661. y[i].scales[j+4] = lm;
  662. } else {
  663. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  664. y[i].scales[j-4] |= ((ls >> 4) << 6);
  665. y[i].scales[j-0] |= ((lm >> 4) << 6);
  666. }
  667. }
  668. y[i].d = ggml_fp32_to_fp16(max_scale/63.f);
  669. y[i].dmin = ggml_fp32_to_fp16(max_min/63.f);
  670. uint8_t sc, m;
  671. for (int j = 0; j < QK_K/32; ++j) {
  672. get_scale_min_k4(j, y[i].scales, &sc, &m);
  673. const float d = ggml_fp16_to_fp32(y[i].d) * sc;
  674. if (!d) continue;
  675. const float dm = ggml_fp16_to_fp32(y[i].dmin) * m;
  676. for (int ii = 0; ii < 32; ++ii) {
  677. int l = nearest_int((x[32*j + ii] + dm)/d);
  678. l = MAX(0, MIN(15, l));
  679. L[32*j + ii] = l;
  680. }
  681. }
  682. #else
  683. const float s_factor = 15.f;
  684. float inv_scale = max_scale > 0 ? s_factor/max_scale : 0.f;
  685. float inv_min = max_min > 0 ? s_factor/max_min : 0.f;
  686. int d1 = nearest_int(inv_scale*scales[0]);
  687. int m1 = nearest_int(inv_min*mins[0]);
  688. int d2 = nearest_int(inv_scale*scales[1]);
  689. int m2 = nearest_int(inv_min*mins[1]);
  690. y[i].scales[0] = d1 | (m1 << 4);
  691. y[i].scales[1] = d2 | (m2 << 4);
  692. y[i].d[0] = ggml_fp32_to_fp16(max_scale/s_factor);
  693. y[i].d[1] = ggml_fp32_to_fp16(max_min/s_factor);
  694. float sumlx = 0;
  695. int suml2 = 0;
  696. for (int j = 0; j < QK_K/32; ++j) {
  697. const uint8_t sd = y[i].scales[j] & 0xF;
  698. const uint8_t sm = y[i].scales[j] >> 4;
  699. const float d = ggml_fp16_to_fp32(y[i].d[0]) * sd;
  700. if (!d) continue;
  701. const float m = ggml_fp16_to_fp32(y[i].d[1]) * sm;
  702. for (int ii = 0; ii < 32; ++ii) {
  703. int l = nearest_int((x[32*j + ii] + m)/d);
  704. l = MAX(0, MIN(15, l));
  705. L[32*j + ii] = l;
  706. sumlx += (x[32*j + ii] + m)*l*sd;
  707. suml2 += l*l*sd*sd;
  708. }
  709. }
  710. if (suml2) {
  711. y[i].d[0] = ggml_fp32_to_fp16(sumlx/suml2);
  712. }
  713. #endif
  714. uint8_t * q = y[i].qs;
  715. for (int j = 0; j < QK_K; j += 64) {
  716. for (int l = 0; l < 32; ++l) q[l] = L[j + l] | (L[j + l + 32] << 4);
  717. q += 32;
  718. }
  719. x += QK_K;
  720. }
  721. }
  722. void dequantize_row_q4_K(const block_q4_K * restrict x, float * restrict y, int k) {
  723. assert(k % QK_K == 0);
  724. const int nb = k / QK_K;
  725. for (int i = 0; i < nb; i++) {
  726. const uint8_t * q = x[i].qs;
  727. #if QK_K == 256
  728. const float d = ggml_fp16_to_fp32(x[i].d);
  729. const float min = ggml_fp16_to_fp32(x[i].dmin);
  730. int is = 0;
  731. uint8_t sc, m;
  732. for (int j = 0; j < QK_K; j += 64) {
  733. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  734. const float d1 = d * sc; const float m1 = min * m;
  735. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  736. const float d2 = d * sc; const float m2 = min * m;
  737. for (int l = 0; l < 32; ++l) *y++ = d1 * (q[l] & 0xF) - m1;
  738. for (int l = 0; l < 32; ++l) *y++ = d2 * (q[l] >> 4) - m2;
  739. q += 32; is += 2;
  740. }
  741. #else
  742. const float dall = ggml_fp16_to_fp32(x[i].d[0]);
  743. const float mall = ggml_fp16_to_fp32(x[i].d[1]);
  744. const float d1 = dall * (x[i].scales[0] & 0xF), m1 = mall * (x[i].scales[0] >> 4);
  745. const float d2 = dall * (x[i].scales[1] & 0xF), m2 = mall * (x[i].scales[1] >> 4);
  746. for (int l = 0; l < 32; ++l) {
  747. y[l+ 0] = d1 * (q[l] & 0xF) - m1;
  748. y[l+32] = d2 * (q[l] >> 4) - m2;
  749. }
  750. y += QK_K;
  751. #endif
  752. }
  753. }
  754. void quantize_row_q4_K(const float * restrict x, void * restrict vy, int k) {
  755. assert(k % QK_K == 0);
  756. block_q4_K * restrict y = vy;
  757. quantize_row_q4_K_reference(x, y, k);
  758. }
  759. size_t ggml_quantize_q4_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  760. assert(k % QK_K == 0);
  761. const int nb = k / QK_K;
  762. (void)hist; // TODO: collect histograms
  763. for (int j = 0; j < nb; j += k) {
  764. block_q4_K * restrict y = (block_q4_K *)dst + j/QK_K;
  765. quantize_row_q4_K_reference(src + j, y, k);
  766. }
  767. return (n/QK_K*sizeof(block_q4_K));
  768. }
  769. // ====================== 5-bit (de)-quantization
  770. void quantize_row_q5_K_reference(const float * restrict x, block_q5_K * restrict y, int k) {
  771. assert(k % QK_K == 0);
  772. const int nb = k / QK_K;
  773. #if QK_K == 256
  774. uint8_t L[QK_K];
  775. float mins[QK_K/32];
  776. float scales[QK_K/32];
  777. float weights[32];
  778. uint8_t Laux[32];
  779. #else
  780. int8_t L[QK_K];
  781. float scales[QK_K/16];
  782. #endif
  783. for (int i = 0; i < nb; i++) {
  784. #if QK_K == 256
  785. float max_scale = 0; // as we are deducting the min, scales are always positive
  786. float max_min = 0;
  787. for (int j = 0; j < QK_K/32; ++j) {
  788. //scales[j] = make_qkx1_quants(32, 31, x + 32*j, L + 32*j, &mins[j], 9, 0.5f);
  789. float sum_x2 = 0;
  790. for (int l = 0; l < 32; ++l) sum_x2 += x[32*j + l] * x[32*j + l];
  791. float av_x = sqrtf(sum_x2/32);
  792. for (int l = 0; l < 32; ++l) weights[l] = av_x + fabsf(x[32*j + l]);
  793. scales[j] = make_qkx2_quants(32, 31, x + 32*j, weights, L + 32*j, &mins[j], Laux, -0.5f, 0.1f, 15, false);
  794. float scale = scales[j];
  795. if (scale > max_scale) {
  796. max_scale = scale;
  797. }
  798. float min = mins[j];
  799. if (min > max_min) {
  800. max_min = min;
  801. }
  802. }
  803. float inv_scale = max_scale > 0 ? 63.f/max_scale : 0.f;
  804. float inv_min = max_min > 0 ? 63.f/max_min : 0.f;
  805. for (int j = 0; j < QK_K/32; ++j) {
  806. uint8_t ls = nearest_int(inv_scale*scales[j]);
  807. uint8_t lm = nearest_int(inv_min*mins[j]);
  808. ls = MIN(63, ls);
  809. lm = MIN(63, lm);
  810. if (j < 4) {
  811. y[i].scales[j] = ls;
  812. y[i].scales[j+4] = lm;
  813. } else {
  814. y[i].scales[j+4] = (ls & 0xF) | ((lm & 0xF) << 4);
  815. y[i].scales[j-4] |= ((ls >> 4) << 6);
  816. y[i].scales[j-0] |= ((lm >> 4) << 6);
  817. }
  818. }
  819. y[i].d = ggml_fp32_to_fp16(max_scale/63.f);
  820. y[i].dmin = ggml_fp32_to_fp16(max_min/63.f);
  821. uint8_t sc, m;
  822. for (int j = 0; j < QK_K/32; ++j) {
  823. get_scale_min_k4(j, y[i].scales, &sc, &m);
  824. const float d = ggml_fp16_to_fp32(y[i].d) * sc;
  825. if (!d) continue;
  826. const float dm = ggml_fp16_to_fp32(y[i].dmin) * m;
  827. for (int ii = 0; ii < 32; ++ii) {
  828. int l = nearest_int((x[32*j + ii] + dm)/d);
  829. l = MAX(0, MIN(31, l));
  830. L[32*j + ii] = l;
  831. }
  832. }
  833. uint8_t * restrict qh = y[i].qh;
  834. uint8_t * restrict ql = y[i].qs;
  835. memset(qh, 0, QK_K/8);
  836. uint8_t m1 = 1, m2 = 2;
  837. for (int n = 0; n < QK_K; n += 64) {
  838. for (int j = 0; j < 32; ++j) {
  839. int l1 = L[n + j];
  840. if (l1 > 15) {
  841. l1 -= 16; qh[j] |= m1;
  842. }
  843. int l2 = L[n + j + 32];
  844. if (l2 > 15) {
  845. l2 -= 16; qh[j] |= m2;
  846. }
  847. ql[j] = l1 | (l2 << 4);
  848. }
  849. m1 <<= 2; m2 <<= 2;
  850. ql += 32;
  851. }
  852. #else
  853. float max_scale = 0, amax = 0;
  854. for (int j = 0; j < QK_K/16; ++j) {
  855. scales[j] = make_qx_quants(16, 16, x + 16*j, L + 16*j, 1);
  856. float abs_scale = fabsf(scales[j]);
  857. if (abs_scale > amax) {
  858. amax = abs_scale;
  859. max_scale = scales[j];
  860. }
  861. }
  862. float iscale = -128.f/max_scale;
  863. for (int j = 0; j < QK_K/16; ++j) {
  864. int l = nearest_int(iscale*scales[j]);
  865. y[i].scales[j] = MAX(-128, MIN(127, l));
  866. }
  867. y[i].d = ggml_fp32_to_fp16(1/iscale);
  868. for (int j = 0; j < QK_K/16; ++j) {
  869. const float d = ggml_fp16_to_fp32(y[i].d) * y[i].scales[j];
  870. if (!d) continue;
  871. for (int ii = 0; ii < 16; ++ii) {
  872. int l = nearest_int(x[16*j + ii]/d);
  873. l = MAX(-16, MIN(15, l));
  874. L[16*j + ii] = l + 16;
  875. }
  876. }
  877. uint8_t * restrict qh = y[i].qh;
  878. uint8_t * restrict ql = y[i].qs;
  879. memset(qh, 0, QK_K/8);
  880. for (int j = 0; j < 32; ++j) {
  881. int jm = j%8;
  882. int is = j/8;
  883. int l1 = L[j];
  884. if (l1 > 15) {
  885. l1 -= 16; qh[jm] |= (1 << is);
  886. }
  887. int l2 = L[j + 32];
  888. if (l2 > 15) {
  889. l2 -= 16; qh[jm] |= (1 << (4 + is));
  890. }
  891. ql[j] = l1 | (l2 << 4);
  892. }
  893. #endif
  894. x += QK_K;
  895. }
  896. }
  897. void dequantize_row_q5_K(const block_q5_K * restrict x, float * restrict y, int k) {
  898. assert(k % QK_K == 0);
  899. const int nb = k / QK_K;
  900. for (int i = 0; i < nb; i++) {
  901. const uint8_t * ql = x[i].qs;
  902. const uint8_t * qh = x[i].qh;
  903. #if QK_K == 256
  904. const float d = ggml_fp16_to_fp32(x[i].d);
  905. const float min = ggml_fp16_to_fp32(x[i].dmin);
  906. int is = 0;
  907. uint8_t sc, m;
  908. uint8_t u1 = 1, u2 = 2;
  909. for (int j = 0; j < QK_K; j += 64) {
  910. get_scale_min_k4(is + 0, x[i].scales, &sc, &m);
  911. const float d1 = d * sc; const float m1 = min * m;
  912. get_scale_min_k4(is + 1, x[i].scales, &sc, &m);
  913. const float d2 = d * sc; const float m2 = min * m;
  914. for (int l = 0; l < 32; ++l) *y++ = d1 * ((ql[l] & 0xF) + (qh[l] & u1 ? 16 : 0)) - m1;
  915. for (int l = 0; l < 32; ++l) *y++ = d2 * ((ql[l] >> 4) + (qh[l] & u2 ? 16 : 0)) - m2;
  916. ql += 32; is += 2;
  917. u1 <<= 2; u2 <<= 2;
  918. }
  919. #else
  920. float d = ggml_fp16_to_fp32(x[i].d);
  921. const int8_t * restrict s = x[i].scales;
  922. for (int l = 0; l < 8; ++l) {
  923. y[l+ 0] = d * s[0] * ((ql[l+ 0] & 0xF) - (qh[l] & 0x01 ? 0 : 16));
  924. y[l+ 8] = d * s[0] * ((ql[l+ 8] & 0xF) - (qh[l] & 0x02 ? 0 : 16));
  925. y[l+16] = d * s[1] * ((ql[l+16] & 0xF) - (qh[l] & 0x04 ? 0 : 16));
  926. y[l+24] = d * s[1] * ((ql[l+24] & 0xF) - (qh[l] & 0x08 ? 0 : 16));
  927. y[l+32] = d * s[2] * ((ql[l+ 0] >> 4) - (qh[l] & 0x10 ? 0 : 16));
  928. y[l+40] = d * s[2] * ((ql[l+ 8] >> 4) - (qh[l] & 0x20 ? 0 : 16));
  929. y[l+48] = d * s[3] * ((ql[l+16] >> 4) - (qh[l] & 0x40 ? 0 : 16));
  930. y[l+56] = d * s[3] * ((ql[l+24] >> 4) - (qh[l] & 0x80 ? 0 : 16));
  931. }
  932. y += QK_K;
  933. #endif
  934. }
  935. }
  936. void quantize_row_q5_K(const float * restrict x, void * restrict vy, int k) {
  937. assert(k % QK_K == 0);
  938. block_q5_K * restrict y = vy;
  939. quantize_row_q5_K_reference(x, y, k);
  940. }
  941. size_t ggml_quantize_q5_K(const float * restrict src, void * restrict dst, int n, int k, int64_t * restrict hist) {
  942. assert(k % QK_K == 0);
  943. const int nb = k / QK_K;
  944. (void)hist;
  945. for (int j = 0; j < nb; j += k) {
  946. block_q5_K * restrict y = (block_q5_K *)dst + j/QK_K;
  947. quantize_row_q5_K_reference(src + j, y, k);
  948. }
  949. return (n/QK_K*sizeof(block_q5_K));
  950. }
  951. // ====================== 6-bit (de)-quantization
  952. void quantize_row_q6_K_reference(const float * restrict x, block_q6_K * restrict y, int k) {
  953. assert(k % QK_K == 0);
  954. const int nb = k / QK_K;
  955. int8_t L[QK_K];
  956. float scales[QK_K/16];
  957. for (int i = 0; i < nb; i++) {
  958. float max_scale = 0;
  959. float max_abs_scale = 0;
  960. for (int ib = 0; ib < QK_K/16; ++ib) {
  961. const float scale = make_qx_quants(16, 32, x + 16*ib, L + 16*ib, 1);
  962. scales[ib] = scale;
  963. const float abs_scale = fabsf(scale);
  964. if (abs_scale > max_abs_scale) {
  965. max_abs_scale = abs_scale;
  966. max_scale = scale;
  967. }
  968. }
  969. if (!max_abs_scale) {
  970. memset(&y[i], 0, sizeof(block_q6_K));
  971. y[i].d = ggml_fp32_to_fp16(0.f);
  972. x += QK_K;
  973. continue;
  974. }
  975. float iscale = -128.f/max_scale;
  976. y[i].d = ggml_fp32_to_fp16(1/iscale);
  977. for (int ib = 0; ib < QK_K/16; ++ib) {
  978. y[i].scales[ib] = MIN(127, nearest_int(iscale*scales[ib]));
  979. }
  980. for (int j = 0; j < QK_K/16; ++j) {
  981. float d = ggml_fp16_to_fp32(y[i].d) * y[i].scales[j];
  982. if (!d) {
  983. continue;
  984. }
  985. for (int ii = 0; ii < 16; ++ii) {
  986. int l = nearest_int(x[16*j + ii]/d);
  987. l = MAX(-32, MIN(31, l));
  988. L[16*j + ii] = l + 32;
  989. }
  990. }
  991. uint8_t * restrict ql = y[i].ql;
  992. uint8_t * restrict qh = y[i].qh;
  993. #if QK_K == 256
  994. for (int j = 0; j < QK_K; j += 128) {
  995. for (int l = 0; l < 32; ++l) {
  996. const uint8_t q1 = L[j + l + 0] & 0xF;
  997. const uint8_t q2 = L[j + l + 32] & 0xF;
  998. const uint8_t q3 = L[j + l + 64] & 0xF;
  999. const uint8_t q4 = L[j + l + 96] & 0xF;
  1000. ql[l+ 0] = q1 | (q3 << 4);
  1001. ql[l+32] = q2 | (q4 << 4);
  1002. qh[l] = (L[j + l] >> 4) | ((L[j + l + 32] >> 4) << 2) | ((L[j + l + 64] >> 4) << 4) | ((L[j + l + 96] >> 4) << 6);
  1003. }
  1004. ql += 64;
  1005. qh += 32;
  1006. }
  1007. #else
  1008. for (int l = 0; l < 32; ++l) {
  1009. const uint8_t q1 = L[l + 0] & 0xF;
  1010. const uint8_t q2 = L[l + 32] & 0xF;
  1011. ql[l] = q1 | (q2 << 4);
  1012. }
  1013. for (int l = 0; l < 16; ++l) {
  1014. qh[l] = (L[l] >> 4) | ((L[l + 16] >> 4) << 2) | ((L[l + 32] >> 4) << 4) | ((L[l + 48] >> 4) << 6);
  1015. }
  1016. #endif
  1017. x += QK_K;
  1018. }
  1019. }
  1020. void dequantize_row_q6_K(const block_q6_K * restrict x, float * restrict y, int k) {
  1021. assert(k % QK_K == 0);
  1022. const int nb = k / QK_K;
  1023. for (int i = 0; i < nb; i++) {
  1024. const float d = ggml_fp16_to_fp32(x[i].d);
  1025. const uint8_t * restrict ql = x[i].ql;
  1026. const uint8_t * restrict qh = x[i].qh;
  1027. const int8_t * restrict sc = x[i].scales;
  1028. #if QK_K == 256
  1029. for (int n = 0; n < QK_K; n += 128) {
  1030. for (int l = 0; l < 32; ++l) {
  1031. int is = l/16;
  1032. const int8_t q1 = (int8_t)((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  1033. const int8_t q2 = (int8_t)((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  1034. const int8_t q3 = (int8_t)((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  1035. const int8_t q4 = (int8_t)((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  1036. y[l + 0] = d * sc[is + 0] * q1;
  1037. y[l + 32] = d * sc[is + 2] * q2;
  1038. y[l + 64] = d * sc[is + 4] * q3;
  1039. y[l + 96] = d * sc[is + 6] * q4;
  1040. }
  1041. y += 128;
  1042. ql += 64;
  1043. qh += 32;
  1044. sc += 8;
  1045. }
  1046. #else
  1047. for (int l = 0; l < 16; ++l) {
  1048. const int8_t q1 = (int8_t)((ql[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  1049. const int8_t q2 = (int8_t)((ql[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  1050. const int8_t q3 = (int8_t)((ql[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  1051. const int8_t q4 = (int8_t)((ql[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  1052. y[l+ 0] = d * sc[0] * q1;
  1053. y[l+16] = d * sc[1] * q2;
  1054. y[l+32] = d * sc[2] * q3;
  1055. y[l+48] = d * sc[3] * q4;
  1056. }
  1057. y += 64;
  1058. #endif
  1059. }
  1060. }
  1061. void quantize_row_q6_K(const float * restrict x, void * restrict vy, int k) {
  1062. assert(k % QK_K == 0);
  1063. block_q6_K * restrict y = vy;
  1064. quantize_row_q6_K_reference(x, y, k);
  1065. }
  1066. size_t ggml_quantize_q6_K(const float * src, void * dst, int n, int k, int64_t * hist) {
  1067. assert(k % QK_K == 0);
  1068. const int nb = k / QK_K;
  1069. (void)hist; // TODO
  1070. for (int j = 0; j < nb; j += k) {
  1071. block_q6_K * restrict y = (block_q6_K *)dst + j/QK_K;
  1072. quantize_row_q6_K_reference(src + j, y, k);
  1073. }
  1074. return (n/QK_K*sizeof(block_q6_K));
  1075. }
  1076. //===================================== Q8_K ==============================================
  1077. void quantize_row_q8_K_reference(const float * restrict x, block_q8_K * restrict y, int k) {
  1078. assert(k % QK_K == 0);
  1079. const int nb = k / QK_K;
  1080. for (int i = 0; i < nb; i++) {
  1081. float max = 0;
  1082. float amax = 0;
  1083. for (int j = 0; j < QK_K; ++j) {
  1084. float ax = fabsf(x[j]);
  1085. if (ax > amax) {
  1086. amax = ax; max = x[j];
  1087. }
  1088. }
  1089. if (!amax) {
  1090. y[i].d = 0;
  1091. memset(y[i].qs, 0, QK_K);
  1092. x += QK_K;
  1093. continue;
  1094. }
  1095. const float iscale = -128.f/max;
  1096. for (int j = 0; j < QK_K; ++j) {
  1097. int v = nearest_int(iscale*x[j]);
  1098. y[i].qs[j] = MIN(127, v);
  1099. }
  1100. for (int j = 0; j < QK_K/16; ++j) {
  1101. int sum = 0;
  1102. for (int ii = 0; ii < 16; ++ii) {
  1103. sum += y[i].qs[j*16 + ii];
  1104. }
  1105. y[i].bsums[j] = sum;
  1106. }
  1107. y[i].d = 1/iscale;
  1108. x += QK_K;
  1109. }
  1110. }
  1111. void dequantize_row_q8_K(const block_q8_K * restrict x, float * restrict y, int k) {
  1112. assert(k % QK_K == 0);
  1113. const int nb = k / QK_K;
  1114. for (int i = 0; i < nb; i++) {
  1115. for (int j = 0; j < QK_K; ++j) {
  1116. *y++ = x[i].d * x[i].qs[j];
  1117. }
  1118. }
  1119. }
  1120. void quantize_row_q8_K(const float * restrict x, void * restrict y, int k) {
  1121. quantize_row_q8_K_reference(x, y, k);
  1122. }
  1123. //===================================== Dot ptoducts =================================
  1124. //
  1125. // Helper functions
  1126. //
  1127. #if __AVX__ || __AVX2__ || __AVX512F__
  1128. // horizontally add 8 floats
  1129. static inline float hsum_float_8(const __m256 x) {
  1130. __m128 res = _mm256_extractf128_ps(x, 1);
  1131. res = _mm_add_ps(res, _mm256_castps256_ps128(x));
  1132. res = _mm_add_ps(res, _mm_movehl_ps(res, res));
  1133. res = _mm_add_ss(res, _mm_movehdup_ps(res));
  1134. return _mm_cvtss_f32(res);
  1135. }
  1136. // shuffles to pick the required scales in dot products
  1137. static inline __m256i get_scale_shuffle_q3k(int i) {
  1138. static const uint8_t k_shuffle[128] = {
  1139. 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,
  1140. 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,
  1141. 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,
  1142. 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,
  1143. };
  1144. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  1145. }
  1146. static inline __m256i get_scale_shuffle_k4(int i) {
  1147. static const uint8_t k_shuffle[256] = {
  1148. 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,
  1149. 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,
  1150. 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,
  1151. 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,
  1152. 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,
  1153. 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,
  1154. 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,
  1155. 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
  1156. };
  1157. return _mm256_loadu_si256((const __m256i*)k_shuffle + i);
  1158. }
  1159. static inline __m128i get_scale_shuffle(int i) {
  1160. static const uint8_t k_shuffle[128] = {
  1161. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  1162. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  1163. 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  1164. 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7,
  1165. 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,
  1166. 10,10,10,10,10,10,10,10, 11,11,11,11,11,11,11,11,
  1167. 12,12,12,12,12,12,12,12, 13,13,13,13,13,13,13,13,
  1168. 14,14,14,14,14,14,14,14, 15,15,15,15,15,15,15,15
  1169. };
  1170. return _mm_loadu_si128((const __m128i*)k_shuffle + i);
  1171. }
  1172. #endif
  1173. #if QK_K == 256
  1174. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  1175. const block_q2_K * restrict x = vx;
  1176. const block_q8_K * restrict y = vy;
  1177. const int nb = n / QK_K;
  1178. #ifdef __ARM_NEON
  1179. const uint8x16_t m3 = vdupq_n_u8(0x3);
  1180. const uint8x16_t m4 = vdupq_n_u8(0xF);
  1181. #if defined(__ARM_FEATURE_DOTPROD)
  1182. const int32x4_t vzero = vdupq_n_s32(0);
  1183. #endif
  1184. int8x16x2_t q2bytes;
  1185. uint8_t aux[16];
  1186. float sum = 0;
  1187. for (int i = 0; i < nb; ++i) {
  1188. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1189. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  1190. const uint8_t * restrict q2 = x[i].qs;
  1191. const int8_t * restrict q8 = y[i].qs;
  1192. const uint8_t * restrict sc = x[i].scales;
  1193. const uint8x16_t mins_and_scales = vld1q_u8(sc);
  1194. const uint8x16_t scales = vandq_u8(mins_and_scales, m4);
  1195. vst1q_u8(aux, scales);
  1196. const uint8x16_t mins = vshrq_n_u8(mins_and_scales, 4);
  1197. const int16x8x2_t q8sums = vld1q_s16_x2(y[i].bsums);
  1198. const int16x8x2_t mins16 = {vreinterpretq_s16_u16(vmovl_u8(vget_low_u8(mins))), vreinterpretq_s16_u16(vmovl_u8(vget_high_u8(mins)))};
  1199. const int32x4_t s0 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[0]), vget_low_s16 (q8sums.val[0])),
  1200. vmull_s16(vget_high_s16(mins16.val[0]), vget_high_s16(q8sums.val[0])));
  1201. const int32x4_t s1 = vaddq_s32(vmull_s16(vget_low_s16 (mins16.val[1]), vget_low_s16 (q8sums.val[1])),
  1202. vmull_s16(vget_high_s16(mins16.val[1]), vget_high_s16(q8sums.val[1])));
  1203. sum += dmin * vaddvq_s32(vaddq_s32(s0, s1));
  1204. int isum = 0;
  1205. int is = 0;
  1206. // We use this macro instead of a function call because for some reason
  1207. // the code runs 2-3% slower, even if the function is declared inline
  1208. #if defined(__ARM_FEATURE_DOTPROD)
  1209. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  1210. isum += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * aux[is+(index)];\
  1211. isum += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * aux[is+1+(index)];
  1212. #else
  1213. #define MULTIPLY_ACCUM_WITH_SCALE(index)\
  1214. {\
  1215. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[0]), vget_low_s8 (q8bytes.val[0])),\
  1216. vmull_s8(vget_high_s8(q2bytes.val[0]), vget_high_s8(q8bytes.val[0])));\
  1217. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[1]), vget_low_s8 (q8bytes.val[1])),\
  1218. vmull_s8(vget_high_s8(q2bytes.val[1]), vget_high_s8(q8bytes.val[1])));\
  1219. isum += vaddvq_s16(p1) * aux[is+(index)] + vaddvq_s16(p2) * aux[is+1+(index)];\
  1220. }
  1221. #endif
  1222. #define SHIFT_MULTIPLY_ACCUM_WITH_SCALE(shift, index)\
  1223. q8bytes = vld1q_s8_x2(q8); q8 += 32;\
  1224. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[0], (shift)), m3));\
  1225. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits.val[1], (shift)), m3));\
  1226. MULTIPLY_ACCUM_WITH_SCALE((index));
  1227. for (int j = 0; j < QK_K/128; ++j) {
  1228. const uint8x16x2_t q2bits = vld1q_u8_x2(q2); q2 += 32;
  1229. int8x16x2_t q8bytes = vld1q_s8_x2(q8); q8 += 32;
  1230. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[0], m3));
  1231. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(q2bits.val[1], m3));
  1232. MULTIPLY_ACCUM_WITH_SCALE(0);
  1233. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(2, 2);
  1234. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(4, 4);
  1235. SHIFT_MULTIPLY_ACCUM_WITH_SCALE(6, 6);
  1236. is += 8;
  1237. }
  1238. sum += d * isum;
  1239. }
  1240. *s = sum;
  1241. #elif defined __AVX2__
  1242. const __m256i m3 = _mm256_set1_epi8(3);
  1243. const __m128i m4 = _mm_set1_epi8(0xF);
  1244. __m256 acc = _mm256_setzero_ps();
  1245. for (int i = 0; i < nb; ++i) {
  1246. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1247. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  1248. const uint8_t * restrict q2 = x[i].qs;
  1249. const int8_t * restrict q8 = y[i].qs;
  1250. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  1251. const __m128i scales8 = _mm_and_si128(mins_and_scales, m4);
  1252. const __m128i mins8 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  1253. const __m256i mins = _mm256_cvtepi8_epi16(mins8);
  1254. const __m256i prod = _mm256_madd_epi16(mins, _mm256_loadu_si256((const __m256i*)y[i].bsums));
  1255. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(prod), acc);
  1256. const __m256i all_scales = _mm256_cvtepi8_epi16(scales8);
  1257. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  1258. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  1259. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  1260. __m256i sumi = _mm256_setzero_si256();
  1261. for (int j = 0; j < QK_K/128; ++j) {
  1262. const __m256i q2bits = _mm256_loadu_si256((const __m256i*)q2); q2 += 32;
  1263. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  1264. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  1265. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  1266. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  1267. const __m256i q2_0 = _mm256_and_si256(q2bits, m3);
  1268. const __m256i q2_1 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 2), m3);
  1269. const __m256i q2_2 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 4), m3);
  1270. const __m256i q2_3 = _mm256_and_si256(_mm256_srli_epi16(q2bits, 6), m3);
  1271. __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  1272. __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  1273. __m256i p2 = _mm256_maddubs_epi16(q2_2, q8_2);
  1274. __m256i p3 = _mm256_maddubs_epi16(q2_3, q8_3);
  1275. p0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(0)), p0);
  1276. p1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(1)), p1);
  1277. p2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(2)), p2);
  1278. p3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(3)), p3);
  1279. p0 = _mm256_add_epi32(p0, p1);
  1280. p2 = _mm256_add_epi32(p2, p3);
  1281. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p0, p2));
  1282. }
  1283. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  1284. }
  1285. *s = hsum_float_8(acc);
  1286. #elif defined __AVX__
  1287. const __m128i m3 = _mm_set1_epi8(0x3);
  1288. const __m128i m4 = _mm_set1_epi8(0xF);
  1289. const __m128i m2 = _mm_set1_epi8(0x2);
  1290. __m256 acc = _mm256_setzero_ps();
  1291. for (int i = 0; i < nb; ++i) {
  1292. const float dall = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1293. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  1294. const uint8_t * restrict q2 = x[i].qs;
  1295. const int8_t * restrict q8 = y[i].qs;
  1296. // load mins and scales from block_q2_K.scales[QK_K/16]
  1297. const __m128i mins_and_scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  1298. const __m128i scales16 = _mm_and_si128(mins_and_scales, m4);
  1299. const __m128i mins16 = _mm_and_si128(_mm_srli_epi16(mins_and_scales, 4), m4);
  1300. const __m128i mins_0 = _mm_cvtepi8_epi16(mins16);
  1301. const __m128i mins_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(mins16, mins16));
  1302. // summs = y[i].bsums * (x[i].scales >> 4) in 16bits*8*2 to 32bits*4*2
  1303. const __m128i summs_0 = _mm_madd_epi16(mins_0, _mm_loadu_si128((const __m128i*)&y[i].bsums[0]));
  1304. const __m128i summs_1 = _mm_madd_epi16(mins_1, _mm_loadu_si128((const __m128i*)&y[i].bsums[8]));
  1305. // sumf += -dmin * summs in 32bits*8
  1306. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dmin), _mm256_cvtepi32_ps(MM256_SET_M128I(summs_1, summs_0))), acc);
  1307. const __m128i scales_0 = _mm_cvtepi8_epi16(scales16);
  1308. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales16, scales16));
  1309. const __m128i scales[2] = { scales_0, scales_1 };
  1310. __m128i sumi_0 = _mm_setzero_si128();
  1311. __m128i sumi_1 = _mm_setzero_si128();
  1312. for (int j = 0; j < QK_K/128; ++j) {
  1313. // load Q8 quants int8*16*8 from block_q8_K.qs[QK_K]
  1314. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1315. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1316. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1317. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1318. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1319. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1320. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1321. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1322. // load 2bits*16*8 from block_q2_K.qs[QK_K/4]
  1323. __m128i q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  1324. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  1325. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  1326. const __m128i q2_4 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  1327. const __m128i q2_6 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  1328. q2bits = _mm_loadu_si128((const __m128i*)q2); q2 += 16;
  1329. const __m128i q2_1 = _mm_and_si128(q2bits, m3);
  1330. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  1331. const __m128i q2_5 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  1332. const __m128i q2_7 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  1333. // isuml = q8[l] * ((q2[l] >> shift) & 3) in 8bits*16*8 to 16bits*8*8
  1334. __m128i p0 = _mm_maddubs_epi16(q2_0, q8_0);
  1335. __m128i p1 = _mm_maddubs_epi16(q2_1, q8_1);
  1336. __m128i p2 = _mm_maddubs_epi16(q2_2, q8_2);
  1337. __m128i p3 = _mm_maddubs_epi16(q2_3, q8_3);
  1338. __m128i p4 = _mm_maddubs_epi16(q2_4, q8_4);
  1339. __m128i p5 = _mm_maddubs_epi16(q2_5, q8_5);
  1340. __m128i p6 = _mm_maddubs_epi16(q2_6, q8_6);
  1341. __m128i p7 = _mm_maddubs_epi16(q2_7, q8_7);
  1342. // isum += (x[i].scales[is++] & 0xF) * isuml in 16bits*8*8 to 32bits*4*8
  1343. __m128i shuffle = _mm_set1_epi16(0x0100);
  1344. p0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p0);
  1345. shuffle = _mm_add_epi16(shuffle, m2);
  1346. p1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p1);
  1347. shuffle = _mm_add_epi16(shuffle, m2);
  1348. p2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p2);
  1349. shuffle = _mm_add_epi16(shuffle, m2);
  1350. p3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p3);
  1351. shuffle = _mm_add_epi16(shuffle, m2);
  1352. p4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p4);
  1353. shuffle = _mm_add_epi16(shuffle, m2);
  1354. p5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p5);
  1355. shuffle = _mm_add_epi16(shuffle, m2);
  1356. p6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p6);
  1357. shuffle = _mm_add_epi16(shuffle, m2);
  1358. p7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p7);
  1359. p0 = _mm_add_epi32(p0, p1);
  1360. p2 = _mm_add_epi32(p2, p3);
  1361. p4 = _mm_add_epi32(p4, p5);
  1362. p6 = _mm_add_epi32(p6, p7);
  1363. // isum in 32bits*4*2
  1364. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p0, p2));
  1365. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p4, p6));
  1366. }
  1367. // sumf += dall * isum - dmin * summs in 32bits
  1368. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  1369. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&dall), _mm256_cvtepi32_ps(sumi)), acc);
  1370. }
  1371. *s = hsum_float_8(acc);
  1372. #elif defined __riscv_v_intrinsic
  1373. float sumf = 0;
  1374. uint8_t temp_01[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  1375. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  1376. for (int i = 0; i < nb; ++i) {
  1377. const uint8_t * q2 = x[i].qs;
  1378. const int8_t * q8 = y[i].qs;
  1379. const uint8_t * sc = x[i].scales;
  1380. const float dall = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1381. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  1382. size_t vl = 16;
  1383. vuint8m1_t scales = __riscv_vle8_v_u8m1(sc, vl);
  1384. vuint8m1_t aux = __riscv_vand_vx_u8m1(scales, 0x0F, vl);
  1385. vint16m1_t q8sums = __riscv_vle16_v_i16m1(y[i].bsums, vl);
  1386. vuint8mf2_t scales_2 = __riscv_vle8_v_u8mf2(sc, vl);
  1387. vuint8mf2_t mins8 = __riscv_vsrl_vx_u8mf2(scales_2, 0x4, vl);
  1388. vint16m1_t mins = __riscv_vreinterpret_v_u16m1_i16m1(__riscv_vzext_vf2_u16m1(mins8, vl));
  1389. vint32m2_t prod = __riscv_vwmul_vv_i32m2(q8sums, mins, vl);
  1390. vint32m1_t vsums = __riscv_vredsum_vs_i32m2_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  1391. sumf += dmin * __riscv_vmv_x_s_i32m1_i32(vsums);
  1392. vl = 32;
  1393. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  1394. vuint8m1_t v_b = __riscv_vle8_v_u8m1(temp_01, vl);
  1395. uint8_t is=0;
  1396. int isum=0;
  1397. for (int j = 0; j < QK_K/128; ++j) {
  1398. // load Q2
  1399. vuint8m1_t q2_x = __riscv_vle8_v_u8m1(q2, vl);
  1400. vuint8m1_t q2_0 = __riscv_vand_vx_u8m1(q2_x, 0x03, vl);
  1401. vuint8m1_t q2_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x2, vl), 0x03 , vl);
  1402. vuint8m1_t q2_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x4, vl), 0x03 , vl);
  1403. vuint8m1_t q2_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q2_x, 0x6, vl), 0x03 , vl);
  1404. // duplicate scale elements for product
  1405. vuint8m1_t sc0 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 0+is, vl), vl);
  1406. vuint8m1_t sc1 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 2+is, vl), vl);
  1407. vuint8m1_t sc2 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 4+is, vl), vl);
  1408. vuint8m1_t sc3 = __riscv_vrgather_vv_u8m1(aux, __riscv_vadd_vx_u8m1(v_b, 6+is, vl), vl);
  1409. vint16m2_t p0 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_0, sc0, vl));
  1410. vint16m2_t p1 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_1, sc1, vl));
  1411. vint16m2_t p2 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_2, sc2, vl));
  1412. vint16m2_t p3 = __riscv_vreinterpret_v_u16m2_i16m2(__riscv_vwmulu_vv_u16m2(q2_3, sc3, vl));
  1413. // load Q8
  1414. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  1415. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  1416. vint8m1_t q8_2 = __riscv_vle8_v_i8m1(q8+64, vl);
  1417. vint8m1_t q8_3 = __riscv_vle8_v_i8m1(q8+96, vl);
  1418. vint32m4_t s0 = __riscv_vwmul_vv_i32m4(p0, __riscv_vwcvt_x_x_v_i16m2(q8_0, vl), vl);
  1419. vint32m4_t s1 = __riscv_vwmul_vv_i32m4(p1, __riscv_vwcvt_x_x_v_i16m2(q8_1, vl), vl);
  1420. vint32m4_t s2 = __riscv_vwmul_vv_i32m4(p2, __riscv_vwcvt_x_x_v_i16m2(q8_2, vl), vl);
  1421. vint32m4_t s3 = __riscv_vwmul_vv_i32m4(p3, __riscv_vwcvt_x_x_v_i16m2(q8_3, vl), vl);
  1422. vint32m1_t isum0 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s0, s1, vl), vzero, vl);
  1423. vint32m1_t isum1 = __riscv_vredsum_vs_i32m4_i32m1(__riscv_vadd_vv_i32m4(s2, s3, vl), isum0, vl);
  1424. isum += __riscv_vmv_x_s_i32m1_i32(isum1);
  1425. q2+=32; q8+=128; is=8;
  1426. }
  1427. sumf += dall * isum;
  1428. }
  1429. *s = sumf;
  1430. #else
  1431. float sumf = 0;
  1432. for (int i = 0; i < nb; ++i) {
  1433. const uint8_t * q2 = x[i].qs;
  1434. const int8_t * q8 = y[i].qs;
  1435. const uint8_t * sc = x[i].scales;
  1436. int summs = 0;
  1437. for (int j = 0; j < 16; ++j) {
  1438. summs += y[i].bsums[j] * (sc[j] >> 4);
  1439. }
  1440. const float dall = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1441. const float dmin = y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  1442. int isum = 0;
  1443. int is = 0;
  1444. int d;
  1445. for (int k = 0; k < QK_K/128; ++k) {
  1446. int shift = 0;
  1447. for (int j = 0; j < 4; ++j) {
  1448. d = sc[is++] & 0xF;
  1449. int isuml = 0;
  1450. for (int l = 0; l < 16; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  1451. isum += d * isuml;
  1452. d = sc[is++] & 0xF;
  1453. isuml = 0;
  1454. for (int l = 16; l < 32; ++l) isuml += q8[l] * ((q2[l] >> shift) & 3);
  1455. isum += d * isuml;
  1456. shift += 2;
  1457. q8 += 32;
  1458. }
  1459. q2 += 32;
  1460. }
  1461. sumf += dall * isum - dmin * summs;
  1462. }
  1463. *s = sumf;
  1464. #endif
  1465. }
  1466. #else
  1467. void ggml_vec_dot_q2_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  1468. const block_q2_K * restrict x = vx;
  1469. const block_q8_K * restrict y = vy;
  1470. const int nb = n / QK_K;
  1471. #ifdef __ARM_NEON
  1472. const uint8x16_t m3 = vdupq_n_u8(0x3);
  1473. #if defined(__ARM_FEATURE_DOTPROD)
  1474. const int32x4_t vzero = vdupq_n_s32(0);
  1475. #endif
  1476. int8x16x4_t q2bytes;
  1477. uint32_t aux32[2];
  1478. const uint8_t * scales = (const uint8_t *)aux32;
  1479. float sum = 0;
  1480. for (int i = 0; i < nb; ++i) {
  1481. const float d = y[i].d * (float)x[i].d;
  1482. const float dmin = -y[i].d * (float)x[i].dmin;
  1483. const uint8_t * restrict q2 = x[i].qs;
  1484. const int8_t * restrict q8 = y[i].qs;
  1485. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  1486. aux32[0] = sc[0] & 0x0f0f0f0f;
  1487. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  1488. 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]);
  1489. int isum1 = 0, isum2 = 0;
  1490. const uint8x16_t q2bits = vld1q_u8(q2);
  1491. const int8x16x4_t q8bytes = vld1q_s8_x4(q8);
  1492. q2bytes.val[0] = vreinterpretq_s8_u8(vandq_u8(q2bits, m3));
  1493. q2bytes.val[1] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 2), m3));
  1494. q2bytes.val[2] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 4), m3));
  1495. q2bytes.val[3] = vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q2bits, 6), m3));
  1496. #if defined(__ARM_FEATURE_DOTPROD)
  1497. isum1 += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[0], q8bytes.val[0])) * scales[0];
  1498. isum2 += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[1], q8bytes.val[1])) * scales[1];
  1499. isum1 += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[2], q8bytes.val[2])) * scales[2];
  1500. isum2 += vaddvq_s32(vdotq_s32(vzero, q2bytes.val[3], q8bytes.val[3])) * scales[3];
  1501. #else
  1502. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  1503. vmull_s8(vget_high_s8(q2bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  1504. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  1505. vmull_s8(vget_high_s8(q2bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  1506. isum1 += vaddvq_s16(p1) * scales[0];
  1507. isum2 += vaddvq_s16(p2) * scales[1];
  1508. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  1509. vmull_s8(vget_high_s8(q2bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  1510. const int16x8_t p4 = vaddq_s16(vmull_s8(vget_low_s8 (q2bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  1511. vmull_s8(vget_high_s8(q2bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  1512. isum1 += vaddvq_s16(p3) * scales[2];
  1513. isum2 += vaddvq_s16(p4) * scales[3];
  1514. #endif
  1515. sum += d * (isum1 + isum2);
  1516. }
  1517. *s = sum;
  1518. #elif defined __AVX2__
  1519. const __m256i m3 = _mm256_set1_epi8(3);
  1520. __m256 acc = _mm256_setzero_ps();
  1521. uint32_t ud, um;
  1522. const uint8_t * restrict db = (const uint8_t *)&ud;
  1523. const uint8_t * restrict mb = (const uint8_t *)&um;
  1524. float summs = 0;
  1525. // TODO: optimize this
  1526. for (int i = 0; i < nb; ++i) {
  1527. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1528. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  1529. const uint8_t * restrict q2 = x[i].qs;
  1530. const int8_t * restrict q8 = y[i].qs;
  1531. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  1532. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  1533. um = (sc[0] >> 4) & 0x0f0f0f0f;
  1534. 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];
  1535. summs += dmin * smin;
  1536. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  1537. const __m256i q2_0 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 2), q2bits), m3);
  1538. const __m256i q2_1 = _mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q2bits, 6), _mm_srli_epi16(q2bits, 4)), m3);
  1539. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  1540. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  1541. const __m256i p0 = _mm256_maddubs_epi16(q2_0, q8_0);
  1542. const __m256i p1 = _mm256_maddubs_epi16(q2_1, q8_1);
  1543. const __m256i p_0 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 0));
  1544. const __m256i p_1 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p0, 1));
  1545. const __m256i p_2 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 0));
  1546. const __m256i p_3 = _mm256_cvtepi16_epi32(_mm256_extracti128_si256(p1, 1));
  1547. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0), acc);
  1548. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1), acc);
  1549. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2), acc);
  1550. acc = _mm256_fmadd_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3), acc);
  1551. }
  1552. *s = hsum_float_8(acc) + summs;
  1553. #elif defined __AVX__
  1554. const __m128i m3 = _mm_set1_epi8(3);
  1555. __m256 acc = _mm256_setzero_ps();
  1556. uint32_t ud, um;
  1557. const uint8_t * restrict db = (const uint8_t *)&ud;
  1558. const uint8_t * restrict mb = (const uint8_t *)&um;
  1559. float summs = 0;
  1560. // TODO: optimize this
  1561. for (int i = 0; i < nb; ++i) {
  1562. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1563. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  1564. const uint8_t * restrict q2 = x[i].qs;
  1565. const int8_t * restrict q8 = y[i].qs;
  1566. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  1567. ud = (sc[0] >> 0) & 0x0f0f0f0f;
  1568. um = (sc[0] >> 4) & 0x0f0f0f0f;
  1569. 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];
  1570. summs += dmin * smin;
  1571. const __m128i q2bits = _mm_loadu_si128((const __m128i*)q2);
  1572. const __m128i q2_0 = _mm_and_si128(q2bits, m3);
  1573. const __m128i q2_1 = _mm_and_si128(_mm_srli_epi16(q2bits, 2), m3);
  1574. const __m128i q2_2 = _mm_and_si128(_mm_srli_epi16(q2bits, 4), m3);
  1575. const __m128i q2_3 = _mm_and_si128(_mm_srli_epi16(q2bits, 6), m3);
  1576. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  1577. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  1578. const __m128i p0 = _mm_maddubs_epi16(q2_0, _mm256_extractf128_si256(q8_0, 0));
  1579. const __m128i p1 = _mm_maddubs_epi16(q2_1, _mm256_extractf128_si256(q8_0, 1));
  1580. const __m128i p2 = _mm_maddubs_epi16(q2_2, _mm256_extractf128_si256(q8_1, 0));
  1581. const __m128i p3 = _mm_maddubs_epi16(q2_3, _mm256_extractf128_si256(q8_1, 1));
  1582. const __m256i p_0 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p0, p0)), _mm_cvtepi16_epi32(p0));
  1583. const __m256i p_1 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p1, p1)), _mm_cvtepi16_epi32(p1));
  1584. const __m256i p_2 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p2, p2)), _mm_cvtepi16_epi32(p2));
  1585. const __m256i p_3 = MM256_SET_M128I(_mm_cvtepi16_epi32(_mm_unpackhi_epi64(p3, p3)), _mm_cvtepi16_epi32(p3));
  1586. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[0]), _mm256_cvtepi32_ps(p_0)), acc);
  1587. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[1]), _mm256_cvtepi32_ps(p_1)), acc);
  1588. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[2]), _mm256_cvtepi32_ps(p_2)), acc);
  1589. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d * db[3]), _mm256_cvtepi32_ps(p_3)), acc);
  1590. }
  1591. *s = hsum_float_8(acc) + summs;
  1592. #elif defined __riscv_v_intrinsic
  1593. uint32_t aux32[2];
  1594. const uint8_t * scales = (const uint8_t *)aux32;
  1595. float sumf = 0;
  1596. for (int i = 0; i < nb; ++i) {
  1597. const float d = y[i].d * (float)x[i].d;
  1598. const float dmin = -y[i].d * (float)x[i].dmin;
  1599. const uint8_t * restrict q2 = x[i].qs;
  1600. const int8_t * restrict q8 = y[i].qs;
  1601. const uint32_t * restrict sc = (const uint32_t *)x[i].scales;
  1602. aux32[0] = sc[0] & 0x0f0f0f0f;
  1603. aux32[1] = (sc[0] >> 4) & 0x0f0f0f0f;
  1604. 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]);
  1605. int isum1 = 0;
  1606. int isum2 = 0;
  1607. size_t vl = 16;
  1608. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  1609. // load Q2
  1610. vuint8mf2_t q2_x = __riscv_vle8_v_u8mf2(q2, vl);
  1611. vint8mf2_t q2_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q2_x, 0x03, vl));
  1612. vint8mf2_t q2_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x2, vl), 0x03 , vl));
  1613. vint8mf2_t q2_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x4, vl), 0x03 , vl));
  1614. vint8mf2_t q2_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(q2_x, 0x6, vl), 0x03 , vl));
  1615. // load Q8, and take product with Q2
  1616. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q2_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  1617. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q2_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  1618. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q2_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  1619. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q2_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  1620. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m1_i16m1(p0, vzero, vl);
  1621. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m1_i16m1(p1, vzero, vl);
  1622. vint16m1_t vs_2 = __riscv_vredsum_vs_i16m1_i16m1(p2, vzero, vl);
  1623. vint16m1_t vs_3 = __riscv_vredsum_vs_i16m1_i16m1(p3, vzero, vl);
  1624. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[0];
  1625. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[1];
  1626. isum1 += __riscv_vmv_x_s_i16m1_i16(vs_2) * scales[2];
  1627. isum2 += __riscv_vmv_x_s_i16m1_i16(vs_3) * scales[3];
  1628. sumf += d * (isum1 + isum2);
  1629. }
  1630. *s = sumf;
  1631. #else
  1632. float sumf = 0;
  1633. int isum[4];
  1634. for (int i = 0; i < nb; ++i) {
  1635. const uint8_t * q2 = x[i].qs;
  1636. const int8_t * q8 = y[i].qs;
  1637. const uint8_t * sc = x[i].scales;
  1638. int summs = 0;
  1639. for (int j = 0; j < QK_K/16; ++j) {
  1640. summs += y[i].bsums[j] * (sc[j] >> 4);
  1641. }
  1642. const float dall = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1643. const float dmin = y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  1644. isum[0] = isum[1] = isum[2] = isum[3] = 0;
  1645. for (int l = 0; l < 16; ++l) {
  1646. isum[0] += q8[l+ 0] * ((q2[l] >> 0) & 3);
  1647. isum[1] += q8[l+16] * ((q2[l] >> 2) & 3);
  1648. isum[2] += q8[l+32] * ((q2[l] >> 4) & 3);
  1649. isum[3] += q8[l+48] * ((q2[l] >> 6) & 3);
  1650. }
  1651. for (int l = 0; l < 4; ++l) {
  1652. isum[l] *= (sc[l] & 0xF);
  1653. }
  1654. sumf += dall * (isum[0] + isum[1] + isum[2] + isum[3]) - dmin * summs;
  1655. }
  1656. *s = sumf;
  1657. #endif
  1658. }
  1659. #endif
  1660. #if QK_K == 256
  1661. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  1662. assert(n % QK_K == 0);
  1663. const uint32_t kmask1 = 0x03030303;
  1664. const uint32_t kmask2 = 0x0f0f0f0f;
  1665. const block_q3_K * restrict x = vx;
  1666. const block_q8_K * restrict y = vy;
  1667. const int nb = n / QK_K;
  1668. #ifdef __ARM_NEON
  1669. uint32_t aux[3];
  1670. uint32_t utmp[4];
  1671. const uint8x16_t m3b = vdupq_n_u8(0x3);
  1672. #ifdef __ARM_FEATURE_DOTPROD
  1673. const int32x4_t vzero = vdupq_n_s32(0);
  1674. #endif
  1675. const uint8x16_t m0 = vdupq_n_u8(1);
  1676. const uint8x16_t m1 = vshlq_n_u8(m0, 1);
  1677. const uint8x16_t m2 = vshlq_n_u8(m0, 2);
  1678. const uint8x16_t m3 = vshlq_n_u8(m0, 3);
  1679. const int8_t m32 = 32;
  1680. int8x16x4_t q3bytes;
  1681. float sum = 0;
  1682. for (int i = 0; i < nb; ++i) {
  1683. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1684. const uint8_t * restrict q3 = x[i].qs;
  1685. const uint8_t * restrict qh = x[i].hmask;
  1686. const int8_t * restrict q8 = y[i].qs;
  1687. uint8x16x2_t qhbits = vld1q_u8_x2(qh);
  1688. uint8x16x4_t q3h;
  1689. int32_t isum = 0;
  1690. // Set up scales
  1691. memcpy(aux, x[i].scales, 12);
  1692. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  1693. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  1694. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  1695. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  1696. int8_t * scale = (int8_t *)utmp;
  1697. for (int j = 0; j < 16; ++j) scale[j] -= m32;
  1698. for (int j = 0; j < QK_K/128; ++j) {
  1699. const uint8x16x2_t q3bits = vld1q_u8_x2(q3); q3 += 32;
  1700. const int8x16x4_t q8bytes_1 = vld1q_s8_x4(q8); q8 += 64;
  1701. const int8x16x4_t q8bytes_2 = vld1q_s8_x4(q8); q8 += 64;
  1702. q3h.val[0] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[0]), 2);
  1703. q3h.val[1] = vshlq_n_u8(vbicq_u8(m0, qhbits.val[1]), 2);
  1704. q3h.val[2] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[0]), 1);
  1705. q3h.val[3] = vshlq_n_u8(vbicq_u8(m1, qhbits.val[1]), 1);
  1706. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[0], m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  1707. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q3bits.val[1], m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  1708. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 2), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  1709. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 2), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  1710. #if defined(__ARM_FEATURE_DOTPROD)
  1711. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[0], q8bytes_1.val[0])) * scale[0];
  1712. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[1], q8bytes_1.val[1])) * scale[1];
  1713. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[2], q8bytes_1.val[2])) * scale[2];
  1714. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[3], q8bytes_1.val[3])) * scale[3];
  1715. #else
  1716. int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[0]), vget_low_s8 (q8bytes_1.val[0])),
  1717. vmull_s8(vget_high_s8(q3bytes.val[0]), vget_high_s8(q8bytes_1.val[0])));
  1718. int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[1]), vget_low_s8 (q8bytes_1.val[1])),
  1719. vmull_s8(vget_high_s8(q3bytes.val[1]), vget_high_s8(q8bytes_1.val[1])));
  1720. int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[2]), vget_low_s8 (q8bytes_1.val[2])),
  1721. vmull_s8(vget_high_s8(q3bytes.val[2]), vget_high_s8(q8bytes_1.val[2])));
  1722. int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[3]), vget_low_s8 (q8bytes_1.val[3])),
  1723. vmull_s8(vget_high_s8(q3bytes.val[3]), vget_high_s8(q8bytes_1.val[3])));
  1724. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1] + vaddvq_s16(p2) * scale[2] + vaddvq_s16(p3) * scale[3];
  1725. #endif
  1726. scale += 4;
  1727. q3h.val[0] = vbicq_u8(m2, qhbits.val[0]);
  1728. q3h.val[1] = vbicq_u8(m2, qhbits.val[1]);
  1729. q3h.val[2] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[0]), 1);
  1730. q3h.val[3] = vshrq_n_u8(vbicq_u8(m3, qhbits.val[1]), 1);
  1731. q3bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 4), m3b)), vreinterpretq_s8_u8(q3h.val[0]));
  1732. q3bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 4), m3b)), vreinterpretq_s8_u8(q3h.val[1]));
  1733. q3bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[0], 6), m3b)), vreinterpretq_s8_u8(q3h.val[2]));
  1734. q3bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(vshrq_n_u8(q3bits.val[1], 6), m3b)), vreinterpretq_s8_u8(q3h.val[3]));
  1735. #if defined(__ARM_FEATURE_DOTPROD)
  1736. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[0], q8bytes_2.val[0])) * scale[0];
  1737. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[1], q8bytes_2.val[1])) * scale[1];
  1738. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[2], q8bytes_2.val[2])) * scale[2];
  1739. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[3], q8bytes_2.val[3])) * scale[3];
  1740. #else
  1741. p0 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[0]), vget_low_s8 (q8bytes_2.val[0])),
  1742. vmull_s8(vget_high_s8(q3bytes.val[0]), vget_high_s8(q8bytes_2.val[0])));
  1743. p1 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[1]), vget_low_s8 (q8bytes_2.val[1])),
  1744. vmull_s8(vget_high_s8(q3bytes.val[1]), vget_high_s8(q8bytes_2.val[1])));
  1745. p2 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[2]), vget_low_s8 (q8bytes_2.val[2])),
  1746. vmull_s8(vget_high_s8(q3bytes.val[2]), vget_high_s8(q8bytes_2.val[2])));
  1747. p3 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[3]), vget_low_s8 (q8bytes_2.val[3])),
  1748. vmull_s8(vget_high_s8(q3bytes.val[3]), vget_high_s8(q8bytes_2.val[3])));
  1749. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1] + vaddvq_s16(p2) * scale[2] + vaddvq_s16(p3) * scale[3];
  1750. #endif
  1751. scale += 4;
  1752. if (j == 0) {
  1753. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 4);
  1754. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 4);
  1755. }
  1756. }
  1757. sum += d * isum;
  1758. }
  1759. *s = sum;
  1760. #elif defined __AVX2__
  1761. const __m256i m3 = _mm256_set1_epi8(3);
  1762. const __m256i mone = _mm256_set1_epi8(1);
  1763. const __m128i m32 = _mm_set1_epi8(32);
  1764. __m256 acc = _mm256_setzero_ps();
  1765. uint32_t aux[3];
  1766. for (int i = 0; i < nb; ++i) {
  1767. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1768. const uint8_t * restrict q3 = x[i].qs;
  1769. const int8_t * restrict q8 = y[i].qs;
  1770. // Set up scales
  1771. memcpy(aux, x[i].scales, 12);
  1772. __m128i scales128 = _mm_set_epi32(
  1773. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  1774. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  1775. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  1776. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  1777. scales128 = _mm_sub_epi8(scales128, m32);
  1778. const __m256i all_scales = _mm256_cvtepi8_epi16(scales128);
  1779. const __m128i l_scales = _mm256_extracti128_si256(all_scales, 0);
  1780. const __m128i h_scales = _mm256_extracti128_si256(all_scales, 1);
  1781. const __m256i scales[2] = {MM256_SET_M128I(l_scales, l_scales), MM256_SET_M128I(h_scales, h_scales)};
  1782. // high bit
  1783. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].hmask);
  1784. // integer accumulator
  1785. __m256i sumi = _mm256_setzero_si256();
  1786. int bit = 0;
  1787. int is = 0;
  1788. for (int j = 0; j < QK_K/128; ++j) {
  1789. // load low 2 bits
  1790. const __m256i q3bits = _mm256_loadu_si256((const __m256i*)q3); q3 += 32;
  1791. // prepare low and high bits
  1792. const __m256i q3l_0 = _mm256_and_si256(q3bits, m3);
  1793. const __m256i q3h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  1794. ++bit;
  1795. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 2), m3);
  1796. const __m256i q3h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  1797. ++bit;
  1798. const __m256i q3l_2 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 4), m3);
  1799. const __m256i q3h_2 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  1800. ++bit;
  1801. const __m256i q3l_3 = _mm256_and_si256(_mm256_srli_epi16(q3bits, 6), m3);
  1802. const __m256i q3h_3 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_andnot_si256(hbits, _mm256_slli_epi16(mone, bit)), bit), 2);
  1803. ++bit;
  1804. // load Q8 quants
  1805. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  1806. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  1807. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  1808. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  1809. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  1810. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  1811. // and 2 if the high bit was set)
  1812. __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  1813. __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  1814. __m256i q8s_2 = _mm256_maddubs_epi16(q3h_2, q8_2);
  1815. __m256i q8s_3 = _mm256_maddubs_epi16(q3h_3, q8_3);
  1816. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  1817. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  1818. __m256i p16_2 = _mm256_maddubs_epi16(q3l_2, q8_2);
  1819. __m256i p16_3 = _mm256_maddubs_epi16(q3l_3, q8_3);
  1820. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  1821. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  1822. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  1823. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  1824. // multiply with scales
  1825. p16_0 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 0)), p16_0);
  1826. p16_1 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 1)), p16_1);
  1827. p16_2 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 2)), p16_2);
  1828. p16_3 = _mm256_madd_epi16(_mm256_shuffle_epi8(scales[j], get_scale_shuffle_q3k(is + 3)), p16_3);
  1829. // accumulate
  1830. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  1831. p16_2 = _mm256_add_epi32(p16_2, p16_3);
  1832. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_2));
  1833. }
  1834. // multiply with block scale and accumulate
  1835. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  1836. }
  1837. *s = hsum_float_8(acc);
  1838. #elif defined __AVX__
  1839. const __m128i m3 = _mm_set1_epi8(3);
  1840. const __m128i mone = _mm_set1_epi8(1);
  1841. const __m128i m32 = _mm_set1_epi8(32);
  1842. const __m128i m2 = _mm_set1_epi8(2);
  1843. __m256 acc = _mm256_setzero_ps();
  1844. const uint32_t *aux;
  1845. for (int i = 0; i < nb; ++i) {
  1846. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  1847. const uint8_t * restrict q3 = x[i].qs;
  1848. const int8_t * restrict q8 = y[i].qs;
  1849. // Set up scales
  1850. aux = (const uint32_t *)x[i].scales;
  1851. __m128i scales128 = _mm_set_epi32(
  1852. ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4),
  1853. ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4),
  1854. (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4),
  1855. (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4));
  1856. scales128 = _mm_sub_epi8(scales128, m32);
  1857. const __m128i scales_0 = _mm_cvtepi8_epi16(scales128);
  1858. const __m128i scales_1 = _mm_cvtepi8_epi16(_mm_unpackhi_epi64(scales128, scales128));
  1859. const __m128i scales[2] = { scales_0, scales_1 };
  1860. // high bit *128*2 from block_q3_K.hmask[QK_K/8]
  1861. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].hmask[0]);
  1862. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].hmask[16]);
  1863. // integer accumulator
  1864. __m128i sumi_0 = _mm_setzero_si128();
  1865. __m128i sumi_1 = _mm_setzero_si128();
  1866. for (int j = 0; j < QK_K/128; ++j) {
  1867. // load low 2 bits *64*2 from block_q3_K.qs[QK_K/4]
  1868. const __m128i q3bits_0 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  1869. const __m128i q3bits_1 = _mm_loadu_si128((const __m128i*)q3); q3 += 16;
  1870. // prepare low and high bits
  1871. const int bit = j << 2;
  1872. const __m128i q3l_0 = _mm_and_si128(q3bits_0, m3);
  1873. const __m128i q3l_1 = _mm_and_si128(q3bits_1, m3);
  1874. const __m128i q3h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit)), bit), 2);
  1875. const __m128i q3h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit)), bit), 2);
  1876. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 2), m3);
  1877. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 2), m3);
  1878. const __m128i q3h_2 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  1879. const __m128i q3h_3 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+1)), bit+1), 2);
  1880. const __m128i q3l_4 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 4), m3);
  1881. const __m128i q3l_5 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 4), m3);
  1882. const __m128i q3h_4 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  1883. const __m128i q3h_5 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+2)), bit+2), 2);
  1884. const __m128i q3l_6 = _mm_and_si128(_mm_srli_epi16(q3bits_0, 6), m3);
  1885. const __m128i q3l_7 = _mm_and_si128(_mm_srli_epi16(q3bits_1, 6), m3);
  1886. const __m128i q3h_6 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_0, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  1887. const __m128i q3h_7 = _mm_slli_epi16(_mm_srli_epi16(_mm_andnot_si128(hbits_1, _mm_slli_epi16(mone, bit+3)), bit+3), 2);
  1888. // load Q8 quants from block_q8_K.qs[QK_K]
  1889. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1890. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1891. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1892. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1893. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1894. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1895. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1896. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  1897. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  1898. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  1899. // and 2 if the high bit was set)
  1900. __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, q8_0);
  1901. __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, q8_1);
  1902. __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, q8_2);
  1903. __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, q8_3);
  1904. __m128i q8s_4 = _mm_maddubs_epi16(q3h_4, q8_4);
  1905. __m128i q8s_5 = _mm_maddubs_epi16(q3h_5, q8_5);
  1906. __m128i q8s_6 = _mm_maddubs_epi16(q3h_6, q8_6);
  1907. __m128i q8s_7 = _mm_maddubs_epi16(q3h_7, q8_7);
  1908. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, q8_0);
  1909. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, q8_1);
  1910. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, q8_2);
  1911. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, q8_3);
  1912. __m128i p16_4 = _mm_maddubs_epi16(q3l_4, q8_4);
  1913. __m128i p16_5 = _mm_maddubs_epi16(q3l_5, q8_5);
  1914. __m128i p16_6 = _mm_maddubs_epi16(q3l_6, q8_6);
  1915. __m128i p16_7 = _mm_maddubs_epi16(q3l_7, q8_7);
  1916. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  1917. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  1918. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  1919. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  1920. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  1921. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  1922. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  1923. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  1924. // multiply with scales
  1925. __m128i shuffle = _mm_set1_epi16(0x0100);
  1926. p16_0 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_0);
  1927. shuffle = _mm_add_epi16(shuffle, m2);
  1928. p16_1 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_1);
  1929. shuffle = _mm_add_epi16(shuffle, m2);
  1930. p16_2 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_2);
  1931. shuffle = _mm_add_epi16(shuffle, m2);
  1932. p16_3 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_3);
  1933. shuffle = _mm_add_epi16(shuffle, m2);
  1934. p16_4 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_4);
  1935. shuffle = _mm_add_epi16(shuffle, m2);
  1936. p16_5 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_5);
  1937. shuffle = _mm_add_epi16(shuffle, m2);
  1938. p16_6 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_6);
  1939. shuffle = _mm_add_epi16(shuffle, m2);
  1940. p16_7 = _mm_madd_epi16(_mm_shuffle_epi8(scales[j], shuffle), p16_7);
  1941. // accumulate
  1942. p16_0 = _mm_add_epi32(p16_0, p16_1);
  1943. p16_2 = _mm_add_epi32(p16_2, p16_3);
  1944. p16_4 = _mm_add_epi32(p16_4, p16_5);
  1945. p16_6 = _mm_add_epi32(p16_6, p16_7);
  1946. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  1947. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_4, p16_6));
  1948. }
  1949. // multiply with block scale and accumulate
  1950. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  1951. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  1952. }
  1953. *s = hsum_float_8(acc);
  1954. #elif defined __riscv_v_intrinsic
  1955. uint32_t aux[3];
  1956. uint32_t utmp[4];
  1957. float sumf = 0;
  1958. for (int i = 0; i < nb; ++i) {
  1959. const uint8_t * restrict q3 = x[i].qs;
  1960. const uint8_t * restrict qh = x[i].hmask;
  1961. const int8_t * restrict q8 = y[i].qs;
  1962. memcpy(aux, x[i].scales, 12);
  1963. utmp[3] = ((aux[1] >> 4) & kmask2) | (((aux[2] >> 6) & kmask1) << 4);
  1964. utmp[2] = ((aux[0] >> 4) & kmask2) | (((aux[2] >> 4) & kmask1) << 4);
  1965. utmp[1] = (aux[1] & kmask2) | (((aux[2] >> 2) & kmask1) << 4);
  1966. utmp[0] = (aux[0] & kmask2) | (((aux[2] >> 0) & kmask1) << 4);
  1967. int8_t * scale = (int8_t *)utmp;
  1968. for (int j = 0; j < 16; ++j) scale[j] -= 32;
  1969. size_t vl = 32;
  1970. uint8_t m = 1;
  1971. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  1972. vuint8m1_t vqh = __riscv_vle8_v_u8m1(qh, vl);
  1973. int sum_t = 0;
  1974. for (int j = 0; j < QK_K; j += 128) {
  1975. vl = 32;
  1976. // load Q3
  1977. vuint8m1_t q3_x = __riscv_vle8_v_u8m1(q3, vl);
  1978. vint8m1_t q3_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q3_x, 0x03, vl));
  1979. vint8m1_t q3_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x2, vl), 0x03 , vl));
  1980. vint8m1_t q3_2 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x4, vl), 0x03 , vl));
  1981. vint8m1_t q3_3 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(q3_x, 0x6, vl), 0x03 , vl));
  1982. // compute mask for subtraction
  1983. vuint8m1_t qh_m0 = __riscv_vand_vx_u8m1(vqh, m, vl);
  1984. vbool8_t vmask_0 = __riscv_vmseq_vx_u8m1_b8(qh_m0, 0, vl);
  1985. vint8m1_t q3_m0 = __riscv_vsub_vx_i8m1_m(vmask_0, q3_0, 0x4, vl);
  1986. m <<= 1;
  1987. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  1988. vbool8_t vmask_1 = __riscv_vmseq_vx_u8m1_b8(qh_m1, 0, vl);
  1989. vint8m1_t q3_m1 = __riscv_vsub_vx_i8m1_m(vmask_1, q3_1, 0x4, vl);
  1990. m <<= 1;
  1991. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  1992. vbool8_t vmask_2 = __riscv_vmseq_vx_u8m1_b8(qh_m2, 0, vl);
  1993. vint8m1_t q3_m2 = __riscv_vsub_vx_i8m1_m(vmask_2, q3_2, 0x4, vl);
  1994. m <<= 1;
  1995. vuint8m1_t qh_m3 = __riscv_vand_vx_u8m1(vqh, m, vl);
  1996. vbool8_t vmask_3 = __riscv_vmseq_vx_u8m1_b8(qh_m3, 0, vl);
  1997. vint8m1_t q3_m3 = __riscv_vsub_vx_i8m1_m(vmask_3, q3_3, 0x4, vl);
  1998. m <<= 1;
  1999. // load Q8 and take product with Q3
  2000. vint16m2_t a0 = __riscv_vwmul_vv_i16m2(q3_m0, __riscv_vle8_v_i8m1(q8, vl), vl);
  2001. vint16m2_t a1 = __riscv_vwmul_vv_i16m2(q3_m1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  2002. vint16m2_t a2 = __riscv_vwmul_vv_i16m2(q3_m2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  2003. vint16m2_t a3 = __riscv_vwmul_vv_i16m2(q3_m3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  2004. vl = 16;
  2005. // retreive lane to multiply with scale
  2006. vint32m2_t aux0_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 0), (scale[0]), vl);
  2007. vint32m2_t aux0_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a0, 1), (scale[1]), vl);
  2008. vint32m2_t aux1_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 0), (scale[2]), vl);
  2009. vint32m2_t aux1_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a1, 1), (scale[3]), vl);
  2010. vint32m2_t aux2_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 0), (scale[4]), vl);
  2011. vint32m2_t aux2_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a2, 1), (scale[5]), vl);
  2012. vint32m2_t aux3_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 0), (scale[6]), vl);
  2013. vint32m2_t aux3_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(a3, 1), (scale[7]), vl);
  2014. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux0_0, aux0_1, vl), vzero, vl);
  2015. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux1_0, aux1_1, vl), isum0, vl);
  2016. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux2_0, aux2_1, vl), isum1, vl);
  2017. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(aux3_0, aux3_1, vl), isum2, vl);
  2018. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  2019. q3 += 32; q8 += 128; scale += 8;
  2020. }
  2021. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  2022. sumf += d*sum_t;
  2023. }
  2024. *s = sumf;
  2025. #else
  2026. // scalar version
  2027. // This function is written like this so the compiler can manage to vectorize most of it
  2028. // Using -Ofast, GCC and clang manage to produce code that is within a factor of 2 or so from the
  2029. // manually vectorized version above. Every other version I tried would run at least 4 times slower.
  2030. // The ideal situation would be if we could just write the code once, and the compiler would
  2031. // automatically produce the best possible set of machine instructions, instead of us having to manually
  2032. // write vectorized versions for AVX, ARM_NEON, etc.
  2033. int8_t aux8[QK_K];
  2034. int16_t aux16[8];
  2035. float sums [8];
  2036. int32_t aux32[8];
  2037. memset(sums, 0, 8*sizeof(float));
  2038. uint32_t auxs[4];
  2039. const int8_t * scales = (const int8_t*)auxs;
  2040. float sumf = 0;
  2041. for (int i = 0; i < nb; ++i) {
  2042. const uint8_t * restrict q3 = x[i].qs;
  2043. const uint8_t * restrict hm = x[i].hmask;
  2044. const int8_t * restrict q8 = y[i].qs;
  2045. memset(aux32, 0, 8*sizeof(int32_t));
  2046. int8_t * restrict a = aux8;
  2047. uint8_t m = 1;
  2048. for (int j = 0; j < QK_K; j += 128) {
  2049. for (int l = 0; l < 32; ++l) a[l] = q3[l] & 3;
  2050. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  2051. a += 32; m <<= 1;
  2052. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 2) & 3;
  2053. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  2054. a += 32; m <<= 1;
  2055. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 4) & 3;
  2056. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  2057. a += 32; m <<= 1;
  2058. for (int l = 0; l < 32; ++l) a[l] = (q3[l] >> 6) & 3;
  2059. for (int l = 0; l < 32; ++l) a[l] -= (hm[l] & m ? 0 : 4);
  2060. a += 32; m <<= 1;
  2061. q3 += 32;
  2062. }
  2063. a = aux8;
  2064. memcpy(auxs, x[i].scales, 12);
  2065. uint32_t tmp = auxs[2];
  2066. auxs[2] = ((auxs[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
  2067. auxs[3] = ((auxs[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
  2068. auxs[0] = (auxs[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
  2069. auxs[1] = (auxs[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
  2070. for (int j = 0; j < QK_K/16; ++j) {
  2071. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  2072. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  2073. q8 += 8; a += 8;
  2074. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  2075. for (int l = 0; l < 8; ++l) aux32[l] += (scales[j] - 32) * aux16[l];
  2076. q8 += 8; a += 8;
  2077. }
  2078. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  2079. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  2080. }
  2081. for (int l = 0; l < 8; ++l) sumf += sums[l];
  2082. *s = sumf;
  2083. #endif
  2084. }
  2085. #else
  2086. void ggml_vec_dot_q3_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2087. assert(n % QK_K == 0);
  2088. const block_q3_K * restrict x = vx;
  2089. const block_q8_K * restrict y = vy;
  2090. const int nb = n / QK_K;
  2091. #ifdef __ARM_NEON
  2092. #ifdef __ARM_FEATURE_DOTPROD
  2093. const int32x4_t vzero = vdupq_n_s32(0);
  2094. #endif
  2095. const uint8x16_t m3b = vdupq_n_u8(0x3);
  2096. const uint8x16_t mh = vdupq_n_u8(4);
  2097. int8x16x4_t q3bytes;
  2098. uint16_t aux16[2];
  2099. int8_t * scales = (int8_t *)aux16;
  2100. float sum = 0;
  2101. for (int i = 0; i < nb; ++i) {
  2102. uint8x16x4_t q3h;
  2103. const uint8x8_t hbits = vld1_u8(x[i].hmask);
  2104. const uint8x16_t q3bits = vld1q_u8(x[i].qs);
  2105. const int8x16x4_t q8bytes = vld1q_s8_x4(y[i].qs);
  2106. const uint16_t a = *(const uint16_t *)x[i].scales;
  2107. aux16[0] = a & 0x0f0f;
  2108. aux16[1] = (a >> 4) & 0x0f0f;
  2109. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  2110. 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]);
  2111. const float d = y[i].d * (float)x[i].d;
  2112. const uint8x16_t htmp = vcombine_u8(hbits, vshr_n_u8(hbits, 1));
  2113. q3h.val[0] = vandq_u8(mh, vshlq_n_u8(htmp, 2));
  2114. q3h.val[1] = vandq_u8(mh, htmp);
  2115. q3h.val[2] = vandq_u8(mh, vshrq_n_u8(htmp, 2));
  2116. q3h.val[3] = vandq_u8(mh, vshrq_n_u8(htmp, 4));
  2117. q3bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q3bits, m3b), q3h.val[0]));
  2118. q3bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 2), m3b), q3h.val[1]));
  2119. q3bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(vshrq_n_u8(q3bits, 4), m3b), q3h.val[2]));
  2120. q3bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q3bits, 6), q3h.val[3]));
  2121. #if defined(__ARM_FEATURE_DOTPROD)
  2122. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[0], q8bytes.val[0])) * scales[0];
  2123. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[1], q8bytes.val[1])) * scales[2];
  2124. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[2], q8bytes.val[2])) * scales[1];
  2125. isum += vaddvq_s32(vdotq_s32(vzero, q3bytes.val[3], q8bytes.val[3])) * scales[3];
  2126. #else
  2127. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  2128. vmull_s8(vget_high_s8(q3bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  2129. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  2130. vmull_s8(vget_high_s8(q3bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  2131. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  2132. vmull_s8(vget_high_s8(q3bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  2133. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q3bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  2134. vmull_s8(vget_high_s8(q3bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  2135. isum += vaddvq_s16(p0) * scales[0] + vaddvq_s16(p1) * scales[2] + vaddvq_s16(p2) * scales[1] + vaddvq_s16(p3) * scales[3];
  2136. #endif
  2137. sum += d * isum;
  2138. }
  2139. *s = sum;
  2140. #elif defined __AVX2__
  2141. const __m256i m3 = _mm256_set1_epi8(3);
  2142. const __m256i m1 = _mm256_set1_epi8(1);
  2143. __m256 acc = _mm256_setzero_ps();
  2144. uint64_t aux64;
  2145. uint16_t aux16[2];
  2146. const int8_t * aux8 = (const int8_t *)aux16;
  2147. for (int i = 0; i < nb; ++i) {
  2148. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2149. const uint8_t * restrict q3 = x[i].qs;
  2150. const int8_t * restrict q8 = y[i].qs;
  2151. const uint16_t a = *(const uint16_t *)x[i].scales;
  2152. aux16[0] = a & 0x0f0f;
  2153. aux16[1] = (a >> 4) & 0x0f0f;
  2154. const __m256i scale_0 = MM256_SET_M128I(_mm_set1_epi16(aux8[2] - 8), _mm_set1_epi16(aux8[0] - 8));
  2155. const __m256i scale_1 = MM256_SET_M128I(_mm_set1_epi16(aux8[3] - 8), _mm_set1_epi16(aux8[1] - 8));
  2156. memcpy(&aux64, x[i].hmask, 8);
  2157. const __m128i haux = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  2158. __m256i q3h_0 = MM256_SET_M128I(_mm_srli_epi16(haux, 2), haux);
  2159. __m256i q3h_1 = _mm256_srli_epi16(q3h_0, 4);
  2160. q3h_0 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_0, m1), 2);
  2161. q3h_1 = _mm256_slli_epi16(_mm256_andnot_si256(q3h_1, m1), 2);
  2162. // load low 2 bits
  2163. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  2164. // prepare low and high bits
  2165. const __m256i q3aux = MM256_SET_M128I(_mm_srli_epi16(q3bits, 2), q3bits);
  2166. const __m256i q3l_0 = _mm256_and_si256(q3aux, m3);
  2167. const __m256i q3l_1 = _mm256_and_si256(_mm256_srli_epi16(q3aux, 4), m3);
  2168. // load Q8 quants
  2169. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  2170. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  2171. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm256_maddubs_epi16,
  2172. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  2173. // and 2 if the high bit was set)
  2174. const __m256i q8s_0 = _mm256_maddubs_epi16(q3h_0, q8_0);
  2175. const __m256i q8s_1 = _mm256_maddubs_epi16(q3h_1, q8_1);
  2176. __m256i p16_0 = _mm256_maddubs_epi16(q3l_0, q8_0);
  2177. __m256i p16_1 = _mm256_maddubs_epi16(q3l_1, q8_1);
  2178. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  2179. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  2180. // multiply with scales
  2181. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  2182. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  2183. p16_0 = _mm256_add_epi32(p16_0, p16_1);
  2184. // multiply with block scale and accumulate
  2185. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16_0), acc);
  2186. }
  2187. *s = hsum_float_8(acc);
  2188. #elif defined __AVX__
  2189. const __m128i m3 = _mm_set1_epi8(3);
  2190. const __m128i m1 = _mm_set1_epi8(1);
  2191. __m256 acc = _mm256_setzero_ps();
  2192. uint64_t aux64;
  2193. uint16_t aux16[2];
  2194. const int8_t * aux8 = (const int8_t *)aux16;
  2195. for (int i = 0; i < nb; ++i) {
  2196. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2197. const uint8_t * restrict q3 = x[i].qs;
  2198. const int8_t * restrict q8 = y[i].qs;
  2199. const uint16_t a = *(const uint16_t *)x[i].scales;
  2200. aux16[0] = a & 0x0f0f;
  2201. aux16[1] = (a >> 4) & 0x0f0f;
  2202. const __m128i scale_0 = _mm_set1_epi16(aux8[0] - 8);
  2203. const __m128i scale_1 = _mm_set1_epi16(aux8[2] - 8);
  2204. const __m128i scale_2 = _mm_set1_epi16(aux8[1] - 8);
  2205. const __m128i scale_3 = _mm_set1_epi16(aux8[3] - 8);
  2206. memcpy(&aux64, x[i].hmask, 8);
  2207. __m128i q3h_0 = _mm_set_epi64x(aux64 >> 1, aux64 >> 0);
  2208. __m128i q3h_1 = _mm_srli_epi16(q3h_0, 2);
  2209. __m128i q3h_2 = _mm_srli_epi16(q3h_0, 4);
  2210. __m128i q3h_3 = _mm_srli_epi16(q3h_0, 6);
  2211. q3h_0 = _mm_slli_epi16(_mm_andnot_si128(q3h_0, m1), 2);
  2212. q3h_1 = _mm_slli_epi16(_mm_andnot_si128(q3h_1, m1), 2);
  2213. q3h_2 = _mm_slli_epi16(_mm_andnot_si128(q3h_2, m1), 2);
  2214. q3h_3 = _mm_slli_epi16(_mm_andnot_si128(q3h_3, m1), 2);
  2215. // load low 2 bits
  2216. const __m128i q3bits = _mm_loadu_si128((const __m128i*)q3);
  2217. // prepare low and high bits
  2218. const __m128i q3l_0 = _mm_and_si128(q3bits, m3);
  2219. const __m128i q3l_1 = _mm_and_si128(_mm_srli_epi16(q3bits, 2), m3);
  2220. const __m128i q3l_2 = _mm_and_si128(_mm_srli_epi16(q3bits, 4), m3);
  2221. const __m128i q3l_3 = _mm_and_si128(_mm_srli_epi16(q3bits, 6), m3);
  2222. // load Q8 quants
  2223. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  2224. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  2225. // Dot product: we multiply the 2 low bits and 1 high bit part separately, so we can use _mm_maddubs_epi16,
  2226. // and then subtract. The high bit part has the 2 already subtracted (and so, it is zero if the high bit was not set,
  2227. // and 2 if the high bit was set)
  2228. const __m128i q8s_0 = _mm_maddubs_epi16(q3h_0, _mm256_extractf128_si256(q8_0, 0));
  2229. const __m128i q8s_1 = _mm_maddubs_epi16(q3h_1, _mm256_extractf128_si256(q8_0, 1));
  2230. const __m128i q8s_2 = _mm_maddubs_epi16(q3h_2, _mm256_extractf128_si256(q8_1, 0));
  2231. const __m128i q8s_3 = _mm_maddubs_epi16(q3h_3, _mm256_extractf128_si256(q8_1, 1));
  2232. __m128i p16_0 = _mm_maddubs_epi16(q3l_0, _mm256_extractf128_si256(q8_0, 0));
  2233. __m128i p16_1 = _mm_maddubs_epi16(q3l_1, _mm256_extractf128_si256(q8_0, 1));
  2234. __m128i p16_2 = _mm_maddubs_epi16(q3l_2, _mm256_extractf128_si256(q8_1, 0));
  2235. __m128i p16_3 = _mm_maddubs_epi16(q3l_3, _mm256_extractf128_si256(q8_1, 1));
  2236. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  2237. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  2238. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  2239. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  2240. // multiply with scales
  2241. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  2242. p16_1 = _mm_madd_epi16(scale_1, p16_1);
  2243. p16_2 = _mm_madd_epi16(scale_2, p16_2);
  2244. p16_3 = _mm_madd_epi16(scale_3, p16_3);
  2245. p16_0 = _mm_add_epi32(p16_0, p16_2);
  2246. p16_1 = _mm_add_epi32(p16_1, p16_3);
  2247. __m256i p16 = MM256_SET_M128I(p16_1, p16_0);
  2248. // multiply with block scale and accumulate
  2249. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(p16)), acc);
  2250. }
  2251. *s = hsum_float_8(acc);
  2252. #elif defined __riscv_v_intrinsic
  2253. uint16_t aux16[2];
  2254. int8_t * scales = (int8_t *)aux16;
  2255. float sumf = 0;
  2256. for (int i = 0; i < nb; ++i) {
  2257. const uint8_t * restrict q3 = x[i].qs;
  2258. const int8_t * restrict q8 = y[i].qs;
  2259. const uint16_t a = *(const uint16_t *)x[i].scales;
  2260. aux16[0] = a & 0x0f0f;
  2261. aux16[1] = (a >> 4) & 0x0f0f;
  2262. for (int j = 0; j < 4; ++j) scales[j] -= 8;
  2263. 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]);
  2264. const float d = y[i].d * (float)x[i].d;
  2265. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  2266. // load qh
  2267. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(x[i].hmask, 8);
  2268. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  2269. size_t vl = 16;
  2270. // extend and combine both qh_x1 and qh_x2
  2271. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  2272. vuint8mf2_t qh_0 = __riscv_vand_vx_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  2273. vuint8mf2_t qh_1 = __riscv_vand_vx_u8mf2(qh_x, 0x4, vl);
  2274. vuint8mf2_t qh_2 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl), 0x4, vl);
  2275. vuint8mf2_t qh_3 = __riscv_vand_vx_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), 0x4, vl);
  2276. // load Q3
  2277. vuint8mf2_t q3_x = __riscv_vle8_v_u8mf2(q3, vl);
  2278. vuint8mf2_t q3h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q3_x, 0x3, vl), qh_0, vl);
  2279. 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);
  2280. 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);
  2281. vuint8mf2_t q3h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q3_x, 0x6, vl), qh_3, vl);
  2282. vint8mf2_t q3_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_0);
  2283. vint8mf2_t q3_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_1);
  2284. vint8mf2_t q3_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_2);
  2285. vint8mf2_t q3_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(q3h_3);
  2286. // load Q8 and take product with Q3
  2287. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q3_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  2288. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q3_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  2289. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q3_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  2290. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q3_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  2291. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  2292. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  2293. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  2294. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  2295. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scales[0];
  2296. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scales[2];
  2297. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scales[1];
  2298. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scales[3];
  2299. sumf += d * isum;
  2300. }
  2301. *s = sumf;
  2302. #else
  2303. int8_t aux8[QK_K];
  2304. int16_t aux16[8];
  2305. float sums [8];
  2306. int32_t aux32[8];
  2307. int32_t scales[4];
  2308. memset(sums, 0, 8*sizeof(float));
  2309. float sumf = 0;
  2310. for (int i = 0; i < nb; ++i) {
  2311. const uint8_t * restrict q3 = x[i].qs;
  2312. const uint8_t * restrict hm = x[i].hmask;
  2313. const int8_t * restrict q8 = y[i].qs;
  2314. int8_t * restrict a = aux8;
  2315. for (int l = 0; l < 8; ++l) {
  2316. a[l+ 0] = (int8_t)((q3[l+0] >> 0) & 3) - (hm[l] & 0x01 ? 0 : 4);
  2317. a[l+ 8] = (int8_t)((q3[l+8] >> 0) & 3) - (hm[l] & 0x02 ? 0 : 4);
  2318. a[l+16] = (int8_t)((q3[l+0] >> 2) & 3) - (hm[l] & 0x04 ? 0 : 4);
  2319. a[l+24] = (int8_t)((q3[l+8] >> 2) & 3) - (hm[l] & 0x08 ? 0 : 4);
  2320. a[l+32] = (int8_t)((q3[l+0] >> 4) & 3) - (hm[l] & 0x10 ? 0 : 4);
  2321. a[l+40] = (int8_t)((q3[l+8] >> 4) & 3) - (hm[l] & 0x20 ? 0 : 4);
  2322. a[l+48] = (int8_t)((q3[l+0] >> 6) & 3) - (hm[l] & 0x40 ? 0 : 4);
  2323. a[l+56] = (int8_t)((q3[l+8] >> 6) & 3) - (hm[l] & 0x80 ? 0 : 4);
  2324. }
  2325. scales[0] = (x[i].scales[0] & 0xF) - 8;
  2326. scales[1] = (x[i].scales[0] >> 4) - 8;
  2327. scales[2] = (x[i].scales[1] & 0xF) - 8;
  2328. scales[3] = (x[i].scales[1] >> 4) - 8;
  2329. memset(aux32, 0, 8*sizeof(int32_t));
  2330. for (int j = 0; j < QK_K/16; ++j) {
  2331. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  2332. q8 += 8; a += 8;
  2333. for (int l = 0; l < 8; ++l) aux16[l] += q8[l] * a[l];
  2334. q8 += 8; a += 8;
  2335. for (int l = 0; l < 8; ++l) aux32[l] += scales[j] * aux16[l];
  2336. }
  2337. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  2338. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  2339. }
  2340. for (int l = 0; l < 8; ++l) sumf += sums[l];
  2341. *s = sumf;
  2342. #endif
  2343. }
  2344. #endif
  2345. #if QK_K == 256
  2346. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2347. assert(n % QK_K == 0);
  2348. const block_q4_K * restrict x = vx;
  2349. const block_q8_K * restrict y = vy;
  2350. const int nb = n / QK_K;
  2351. static const uint32_t kmask1 = 0x3f3f3f3f;
  2352. static const uint32_t kmask2 = 0x0f0f0f0f;
  2353. static const uint32_t kmask3 = 0x03030303;
  2354. uint32_t utmp[4];
  2355. #ifdef __ARM_NEON
  2356. const uint8x16_t m4b = vdupq_n_u8(0xf);
  2357. #ifdef __ARM_FEATURE_DOTPROD
  2358. const int32x4_t mzero = vdupq_n_s32(0);
  2359. #endif
  2360. int8x16x2_t q4bytes;
  2361. int8x16x2_t q8bytes;
  2362. float sumf = 0;
  2363. for (int i = 0; i < nb; ++i) {
  2364. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2365. const float dmin = y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  2366. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  2367. memcpy(utmp, x[i].scales, 12);
  2368. uint32x2_t mins8 = { 0 };
  2369. mins8 = vset_lane_u32(utmp[1] & kmask1, mins8, 0);
  2370. mins8 = vset_lane_u32(((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4), mins8, 1);
  2371. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  2372. utmp[0] &= kmask1;
  2373. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(vreinterpret_u8_u32(mins8)));
  2374. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  2375. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  2376. sumf -= dmin * vaddvq_s32(prod);
  2377. const uint8_t * scales = (const uint8_t *)utmp;
  2378. const uint8_t * restrict q4 = x[i].qs;
  2379. const int8_t * restrict q8 = y[i].qs;
  2380. int32_t sumi1 = 0;
  2381. int32_t sumi2 = 0;
  2382. for (int j = 0; j < QK_K/64; ++j) {
  2383. const uint8x16x2_t q4bits = vld1q_u8_x2(q4); q4 += 32;
  2384. #ifdef __ARM_FEATURE_DOTPROD
  2385. q8bytes = vld1q_s8_x2(q8); q8 += 32;
  2386. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  2387. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  2388. const int32x4_t p1 = vdotq_s32(vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  2389. sumi1 += vaddvq_s32(p1) * scales[2*j+0];
  2390. q8bytes = vld1q_s8_x2(q8); q8 += 32;
  2391. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  2392. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  2393. const int32x4_t p2 = vdotq_s32(vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  2394. sumi2 += vaddvq_s32(p2) * scales[2*j+1];
  2395. #else
  2396. q8bytes = vld1q_s8_x2(q8); q8 += 32;
  2397. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  2398. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  2399. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  2400. vmull_s8(vget_high_s8(q4bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  2401. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  2402. vmull_s8(vget_high_s8(q4bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  2403. sumi1 += vaddvq_s16(vaddq_s16(p0, p1)) * scales[2*j+0];
  2404. q8bytes = vld1q_s8_x2(q8); q8 += 32;
  2405. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  2406. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  2407. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  2408. vmull_s8(vget_high_s8(q4bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  2409. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  2410. vmull_s8(vget_high_s8(q4bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  2411. sumi2 += vaddvq_s16(vaddq_s16(p2, p3)) * scales[2*j+1];
  2412. #endif
  2413. }
  2414. sumf += d * (sumi1 + sumi2);
  2415. }
  2416. *s = sumf;
  2417. #elif defined __AVX2__
  2418. const __m256i m4 = _mm256_set1_epi8(0xF);
  2419. __m256 acc = _mm256_setzero_ps();
  2420. __m128 acc_m = _mm_setzero_ps();
  2421. for (int i = 0; i < nb; ++i) {
  2422. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2423. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  2424. memcpy(utmp, x[i].scales, 12);
  2425. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  2426. const uint32_t uaux = utmp[1] & kmask1;
  2427. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  2428. utmp[2] = uaux;
  2429. utmp[0] &= kmask1;
  2430. const uint8_t * restrict q4 = x[i].qs;
  2431. const int8_t * restrict q8 = y[i].qs;
  2432. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  2433. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  2434. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  2435. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  2436. acc_m = _mm_fmadd_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod), acc_m);
  2437. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  2438. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  2439. __m256i sumi = _mm256_setzero_si256();
  2440. for (int j = 0; j < QK_K/64; ++j) {
  2441. const __m256i scale_l = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  2442. const __m256i scale_h = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  2443. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  2444. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  2445. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  2446. const __m256i q8l = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2447. __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  2448. p16l = _mm256_madd_epi16(scale_l, p16l);
  2449. const __m256i q8h = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2450. __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  2451. p16h = _mm256_madd_epi16(scale_h, p16h);
  2452. const __m256i sumj = _mm256_add_epi32(p16l, p16h);
  2453. sumi = _mm256_add_epi32(sumi, sumj);
  2454. }
  2455. __m256 vd = _mm256_set1_ps(d);
  2456. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  2457. }
  2458. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  2459. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  2460. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  2461. #elif defined __AVX__
  2462. const __m128i m4 = _mm_set1_epi8(0xF);
  2463. const __m128i m2 = _mm_set1_epi8(0x2);
  2464. __m256 acc = _mm256_setzero_ps();
  2465. __m128 acc_m = _mm_setzero_ps();
  2466. for (int i = 0; i < nb; ++i) {
  2467. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2468. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  2469. const uint8_t * restrict q4 = x[i].qs;
  2470. const int8_t * restrict q8 = y[i].qs;
  2471. memcpy(utmp, x[i].scales, 12);
  2472. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  2473. const uint32_t uaux = utmp[1] & kmask1;
  2474. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  2475. utmp[2] = uaux;
  2476. utmp[0] &= kmask1;
  2477. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  2478. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  2479. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  2480. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  2481. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  2482. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  2483. const __m128i prod = _mm_madd_epi16(mins, q8s);
  2484. acc_m = _mm_add_ps(_mm_mul_ps(_mm_set1_ps(dmin), _mm_cvtepi32_ps(prod)), acc_m);
  2485. __m128i sumi_0 = _mm_setzero_si128();
  2486. __m128i sumi_1 = _mm_setzero_si128();
  2487. __m128i shuffle = _mm_set1_epi16(0x0100);
  2488. for (int j = 0; j < QK_K/64; ++j) {
  2489. const __m128i scale_l = _mm_shuffle_epi8(scales, shuffle);
  2490. shuffle = _mm_add_epi16(shuffle, m2);
  2491. const __m128i scale_h = _mm_shuffle_epi8(scales, shuffle);
  2492. shuffle = _mm_add_epi16(shuffle, m2);
  2493. __m128i q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  2494. const __m128i q4l_0 = _mm_and_si128(q4bits, m4);
  2495. const __m128i q4h_0 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  2496. q4bits = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  2497. const __m128i q4l_1 = _mm_and_si128(q4bits, m4);
  2498. const __m128i q4h_1 = _mm_and_si128(_mm_srli_epi16(q4bits, 4), m4);
  2499. const __m128i q8l_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  2500. __m128i p16l = _mm_maddubs_epi16(q4l_0, q8l_0);
  2501. p16l = _mm_madd_epi16(scale_l, p16l);
  2502. sumi_0 = _mm_add_epi32(sumi_0, p16l);
  2503. const __m128i q8l_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  2504. p16l = _mm_maddubs_epi16(q4l_1, q8l_1);
  2505. p16l = _mm_madd_epi16(scale_l, p16l);
  2506. sumi_1 = _mm_add_epi32(sumi_1, p16l);
  2507. const __m128i q8h_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  2508. __m128i p16h = _mm_maddubs_epi16(q4h_0, q8h_0);
  2509. p16h = _mm_madd_epi16(scale_h, p16h);
  2510. sumi_0 = _mm_add_epi32(sumi_0, p16h);
  2511. const __m128i q8h_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  2512. p16h = _mm_maddubs_epi16(q4h_1, q8h_1);
  2513. p16h = _mm_madd_epi16(scale_h, p16h);
  2514. sumi_1 = _mm_add_epi32(sumi_1, p16h);
  2515. }
  2516. __m256 vd = _mm256_set1_ps(d);
  2517. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  2518. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  2519. }
  2520. acc_m = _mm_add_ps(acc_m, _mm_movehl_ps(acc_m, acc_m));
  2521. acc_m = _mm_add_ss(acc_m, _mm_movehdup_ps(acc_m));
  2522. *s = hsum_float_8(acc) + _mm_cvtss_f32(acc_m);
  2523. #elif defined __riscv_v_intrinsic
  2524. const uint8_t * scales = (const uint8_t*)&utmp[0];
  2525. const uint8_t * mins = (const uint8_t*)&utmp[2];
  2526. float sumf = 0;
  2527. for (int i = 0; i < nb; ++i) {
  2528. size_t vl = 8;
  2529. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2530. const float dmin = y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  2531. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  2532. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  2533. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  2534. memcpy(utmp, x[i].scales, 12);
  2535. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  2536. const uint32_t uaux = utmp[1] & kmask1;
  2537. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  2538. utmp[2] = uaux;
  2539. utmp[0] &= kmask1;
  2540. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  2541. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  2542. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  2543. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  2544. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  2545. const uint8_t * restrict q4 = x[i].qs;
  2546. const int8_t * restrict q8 = y[i].qs;
  2547. vl = 32;
  2548. int32_t sum_1 = 0;
  2549. int32_t sum_2 = 0;
  2550. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  2551. for (int j = 0; j < QK_K/64; ++j) {
  2552. // load Q4
  2553. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  2554. // load Q8 and multiply it with lower Q4 nibble
  2555. vint8m1_t q8_0 = __riscv_vle8_v_i8m1(q8, vl);
  2556. vint8m1_t q4_0 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  2557. vint16m2_t qv_0 = __riscv_vwmul_vv_i16m2(q4_0, q8_0, vl);
  2558. vint16m1_t vs_0 = __riscv_vredsum_vs_i16m2_i16m1(qv_0, vzero, vl);
  2559. sum_1 += __riscv_vmv_x_s_i16m1_i16(vs_0) * scales[2*j+0];
  2560. // load Q8 and multiply it with upper Q4 nibble
  2561. vint8m1_t q8_1 = __riscv_vle8_v_i8m1(q8+32, vl);
  2562. vint8m1_t q4_1 = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  2563. vint16m2_t qv_1 = __riscv_vwmul_vv_i16m2(q4_1, q8_1, vl);
  2564. vint16m1_t vs_1 = __riscv_vredsum_vs_i16m2_i16m1(qv_1, vzero, vl);
  2565. sum_2 += __riscv_vmv_x_s_i16m1_i16(vs_1) * scales[2*j+1];
  2566. q4 += 32; q8 += 64;
  2567. }
  2568. sumf += d*(sum_1 + sum_2);
  2569. }
  2570. *s = sumf;
  2571. #else
  2572. const uint8_t * scales = (const uint8_t*)&utmp[0];
  2573. const uint8_t * mins = (const uint8_t*)&utmp[2];
  2574. int8_t aux8[QK_K];
  2575. int16_t aux16[8];
  2576. float sums [8];
  2577. int32_t aux32[8];
  2578. memset(sums, 0, 8*sizeof(float));
  2579. float sumf = 0;
  2580. for (int i = 0; i < nb; ++i) {
  2581. const uint8_t * restrict q4 = x[i].qs;
  2582. const int8_t * restrict q8 = y[i].qs;
  2583. memset(aux32, 0, 8*sizeof(int32_t));
  2584. int8_t * restrict a = aux8;
  2585. for (int j = 0; j < QK_K/64; ++j) {
  2586. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  2587. a += 32;
  2588. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  2589. a += 32; q4 += 32;
  2590. }
  2591. memcpy(utmp, x[i].scales, 12);
  2592. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  2593. const uint32_t uaux = utmp[1] & kmask1;
  2594. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  2595. utmp[2] = uaux;
  2596. utmp[0] &= kmask1;
  2597. int sumi = 0;
  2598. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  2599. a = aux8;
  2600. int is = 0;
  2601. for (int j = 0; j < QK_K/32; ++j) {
  2602. int32_t scale = scales[is++];
  2603. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  2604. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  2605. q8 += 8; a += 8;
  2606. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  2607. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  2608. q8 += 8; a += 8;
  2609. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  2610. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  2611. q8 += 8; a += 8;
  2612. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  2613. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  2614. q8 += 8; a += 8;
  2615. }
  2616. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  2617. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  2618. const float dmin = ggml_fp16_to_fp32(x[i].dmin) * y[i].d;
  2619. sumf -= dmin * sumi;
  2620. }
  2621. for (int l = 0; l < 8; ++l) sumf += sums[l];
  2622. *s = sumf;
  2623. #endif
  2624. }
  2625. #else
  2626. void ggml_vec_dot_q4_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2627. assert(n % QK_K == 0);
  2628. const block_q4_K * restrict x = vx;
  2629. const block_q8_K * restrict y = vy;
  2630. const int nb = n / QK_K;
  2631. #ifdef __ARM_NEON
  2632. const uint8x16_t m4b = vdupq_n_u8(0xf);
  2633. #ifdef __ARM_FEATURE_DOTPROD
  2634. const int32x4_t mzero = vdupq_n_s32(0);
  2635. #endif
  2636. float sumf = 0;
  2637. int8x16x2_t q4bytes;
  2638. int8x16x4_t q8bytes;
  2639. float sum_mins = 0.f;
  2640. uint16_t aux16[2];
  2641. const uint8_t * restrict scales = (const uint8_t *)aux16;
  2642. for (int i = 0; i < nb; ++i) {
  2643. const uint8_t * restrict q4 = x[i].qs;
  2644. const int8_t * restrict q8 = y[i].qs;
  2645. const uint16_t * restrict a = (const uint16_t *)x[i].scales;
  2646. aux16[0] = a[0] & 0x0f0f;
  2647. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2648. const int32_t summi = scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]);
  2649. sum_mins += y[i].d * (float)x[i].d[1] * summi;
  2650. const float d = y[i].d * (float)x[i].d[0];
  2651. const uint8x16x2_t q4bits = vld1q_u8_x2(q4);
  2652. #ifdef __ARM_FEATURE_DOTPROD
  2653. q8bytes = vld1q_s8_x4(q8);
  2654. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  2655. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  2656. const int32x4_t p1 = vdotq_s32(vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[0]), q4bytes.val[1], q8bytes.val[1]);
  2657. const int32_t sumi1 = vaddvq_s32(p1) * scales[0];
  2658. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  2659. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  2660. const int32x4_t p2 = vdotq_s32(vdotq_s32(mzero, q4bytes.val[0], q8bytes.val[2]), q4bytes.val[1], q8bytes.val[3]);
  2661. const int32_t sumi2 = vaddvq_s32(p2) * scales[1];
  2662. #else
  2663. q8bytes = vld1q_s8_x4(q8);
  2664. q4bytes.val[0] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[0], m4b));
  2665. q4bytes.val[1] = vreinterpretq_s8_u8(vandq_u8 (q4bits.val[1], m4b));
  2666. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  2667. vmull_s8(vget_high_s8(q4bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  2668. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  2669. vmull_s8(vget_high_s8(q4bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  2670. int32_t sumi1 = vaddvq_s16(vaddq_s16(p0, p1)) * scales[0];
  2671. q4bytes.val[0] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[0], 4));
  2672. q4bytes.val[1] = vreinterpretq_s8_u8(vshrq_n_u8(q4bits.val[1], 4));
  2673. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[0]), vget_low_s8 (q8bytes.val[2])),
  2674. vmull_s8(vget_high_s8(q4bytes.val[0]), vget_high_s8(q8bytes.val[2])));
  2675. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q4bytes.val[1]), vget_low_s8 (q8bytes.val[3])),
  2676. vmull_s8(vget_high_s8(q4bytes.val[1]), vget_high_s8(q8bytes.val[3])));
  2677. int32_t sumi2 = vaddvq_s16(vaddq_s16(p2, p3)) * scales[1];
  2678. #endif
  2679. sumf += d * (sumi1 + sumi2);
  2680. }
  2681. *s = sumf - sum_mins;
  2682. #elif defined __AVX2__
  2683. const __m256i m4 = _mm256_set1_epi8(0xF);
  2684. __m256 acc = _mm256_setzero_ps();
  2685. float summs = 0;
  2686. uint16_t aux16[2];
  2687. const uint8_t * scales = (const uint8_t *)aux16;
  2688. for (int i = 0; i < nb; ++i) {
  2689. const float d = ggml_fp16_to_fp32(x[i].d[0]) * y[i].d;
  2690. const float m = ggml_fp16_to_fp32(x[i].d[1]) * y[i].d;
  2691. const __m256 vd = _mm256_set1_ps(d);
  2692. const uint16_t * a = (const uint16_t *)x[i].scales;
  2693. aux16[0] = a[0] & 0x0f0f;
  2694. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2695. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  2696. const uint8_t * restrict q4 = x[i].qs;
  2697. const int8_t * restrict q8 = y[i].qs;
  2698. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  2699. const __m256i q4l = _mm256_and_si256(q4bits, m4);
  2700. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(q4bits, 4), m4);
  2701. const __m256i q8l = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  2702. const __m256i q8h = _mm256_loadu_si256((const __m256i*)(q8+32));
  2703. const __m256i p16l = _mm256_maddubs_epi16(q4l, q8l);
  2704. const __m256i p16h = _mm256_maddubs_epi16(q4h, q8h);
  2705. const __m256i p32l = _mm256_madd_epi16(_mm256_set1_epi16(scales[0]), p16l);
  2706. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32l), acc);
  2707. const __m256i p32h = _mm256_madd_epi16(_mm256_set1_epi16(scales[1]), p16h);
  2708. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(p32h), acc);
  2709. }
  2710. *s = hsum_float_8(acc) - summs;
  2711. #elif defined __AVX__
  2712. const __m128i m4 = _mm_set1_epi8(0xF);
  2713. __m256 acc = _mm256_setzero_ps();
  2714. float summs = 0;
  2715. uint16_t aux16[2];
  2716. const uint8_t * scales = (const uint8_t *)aux16;
  2717. for (int i = 0; i < nb; ++i) {
  2718. const float d = ggml_fp16_to_fp32(x[i].d[0]) * y[i].d;
  2719. const float m = ggml_fp16_to_fp32(x[i].d[1]) * y[i].d;
  2720. const __m256 vd = _mm256_set1_ps(d);
  2721. const uint16_t * a = (const uint16_t *)x[i].scales;
  2722. aux16[0] = a[0] & 0x0f0f;
  2723. aux16[1] = (a[0] >> 4) & 0x0f0f;
  2724. summs += m * (scales[2] * (y[i].bsums[0] + y[i].bsums[1]) + scales[3] * (y[i].bsums[2] + y[i].bsums[3]));
  2725. const uint8_t * restrict q4 = x[i].qs;
  2726. const int8_t * restrict q8 = y[i].qs;
  2727. const __m256i q4bits = _mm256_loadu_si256((const __m256i*)q4);
  2728. const __m128i q4bits_0 = _mm256_extractf128_si256(q4bits, 0);
  2729. const __m128i q4bits_1 = _mm256_extractf128_si256(q4bits, 1);
  2730. const __m128i q4_0 = _mm_and_si128(q4bits_0, m4);
  2731. const __m128i q4_1 = _mm_and_si128(q4bits_1, m4);
  2732. const __m128i q4_2 = _mm_and_si128(_mm_srli_epi16(q4bits_0, 4), m4);
  2733. const __m128i q4_3 = _mm_and_si128(_mm_srli_epi16(q4bits_1, 4), m4);
  2734. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  2735. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  2736. const __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  2737. const __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  2738. const __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  2739. const __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  2740. const __m128i p32_0 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_0);
  2741. const __m128i p32_1 = _mm_madd_epi16(_mm_set1_epi16(scales[0]), p16_1);
  2742. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_1, p32_0))), acc);
  2743. const __m128i p32_2 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_2);
  2744. const __m128i p32_3 = _mm_madd_epi16(_mm_set1_epi16(scales[1]), p16_3);
  2745. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(MM256_SET_M128I(p32_3, p32_2))), acc);
  2746. }
  2747. *s = hsum_float_8(acc) - summs;
  2748. #elif defined __riscv_v_intrinsic
  2749. uint16_t s16[2];
  2750. const uint8_t * restrict scales = (const uint8_t *)s16;
  2751. float sumf = 0;
  2752. for (int i = 0; i < nb; ++i) {
  2753. const uint8_t * restrict q4 = x[i].qs;
  2754. const int8_t * restrict q8 = y[i].qs;
  2755. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  2756. s16[0] = b[0] & 0x0f0f;
  2757. s16[1] = (b[0] >> 4) & 0x0f0f;
  2758. 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]));
  2759. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d[0]);
  2760. size_t vl = 32;
  2761. vint16m1_t vzero = __riscv_vmv_v_x_i16m1(0, 1);
  2762. // load Q4
  2763. vuint8m1_t q4_x = __riscv_vle8_v_u8m1(q4, vl);
  2764. // load Q8 and multiply it with lower Q4 nibble
  2765. vint8m1_t q4_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q4_x, 0x0F, vl));
  2766. vint16m2_t va_0 = __riscv_vwmul_vv_i16m2(q4_a, __riscv_vle8_v_i8m1(q8, vl), vl);
  2767. vint16m1_t aux1 = __riscv_vredsum_vs_i16m2_i16m1(va_0, vzero, vl);
  2768. sumf += d*scales[0]*__riscv_vmv_x_s_i16m1_i16(aux1);
  2769. // load Q8 and multiply it with upper Q4 nibble
  2770. vint8m1_t q4_s = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q4_x, 0x04, vl));
  2771. vint16m2_t va_1 = __riscv_vwmul_vv_i16m2(q4_s, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  2772. vint16m1_t aux2 = __riscv_vredsum_vs_i16m2_i16m1(va_1, vzero, vl);
  2773. sumf += d*scales[1]*__riscv_vmv_x_s_i16m1_i16(aux2);
  2774. }
  2775. *s = sumf;
  2776. #else
  2777. uint8_t aux8[QK_K];
  2778. int16_t aux16[16];
  2779. float sums [8];
  2780. memset(sums, 0, 8*sizeof(float));
  2781. uint16_t s16[2];
  2782. const uint8_t * restrict scales = (const uint8_t *)s16;
  2783. float sumf = 0;
  2784. for (int i = 0; i < nb; ++i) {
  2785. const uint8_t * restrict q4 = x[i].qs;
  2786. const int8_t * restrict q8 = y[i].qs;
  2787. uint8_t * restrict a = aux8;
  2788. for (int l = 0; l < 32; ++l) a[l+ 0] = q4[l] & 0xF;
  2789. for (int l = 0; l < 32; ++l) a[l+32] = q4[l] >> 4;
  2790. const uint16_t * restrict b = (const uint16_t *)x[i].scales;
  2791. s16[0] = b[0] & 0x0f0f;
  2792. s16[1] = (b[0] >> 4) & 0x0f0f;
  2793. 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]));
  2794. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d[0]);
  2795. for (int j = 0; j < QK_K/32; ++j) {
  2796. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  2797. q8 += 16; a += 16;
  2798. for (int l = 0; l < 16; ++l) aux16[l] += q8[l] * a[l];
  2799. q8 += 16; a += 16;
  2800. const float dl = d * scales[j];
  2801. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[l+8]);
  2802. }
  2803. }
  2804. for (int l = 0; l < 8; ++l) sumf += sums[l];
  2805. *s = sumf;
  2806. #endif
  2807. }
  2808. #endif
  2809. #if QK_K == 256
  2810. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  2811. assert(n % QK_K == 0);
  2812. const block_q5_K * restrict x = vx;
  2813. const block_q8_K * restrict y = vy;
  2814. const int nb = n / QK_K;
  2815. static const uint32_t kmask1 = 0x3f3f3f3f;
  2816. static const uint32_t kmask2 = 0x0f0f0f0f;
  2817. static const uint32_t kmask3 = 0x03030303;
  2818. uint32_t utmp[4];
  2819. #ifdef __ARM_NEON
  2820. const uint8x16_t m4b = vdupq_n_u8(0xf);
  2821. const uint8x16_t mone = vdupq_n_u8(1);
  2822. const uint8x16_t mtwo = vdupq_n_u8(2);
  2823. #if defined(__ARM_FEATURE_DOTPROD)
  2824. const int32x4_t mzero = vdupq_n_s32(0);
  2825. #endif
  2826. int8x16x4_t q5bytes;
  2827. float sumf = 0;
  2828. for (int i = 0; i < nb; ++i) {
  2829. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2830. const float dmin = y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  2831. const int16x8_t q8sums = vpaddq_s16(vld1q_s16(y[i].bsums), vld1q_s16(y[i].bsums + 8));
  2832. memcpy(utmp, x[i].scales, 12);
  2833. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  2834. const uint32_t uaux = utmp[1] & kmask1;
  2835. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  2836. utmp[2] = uaux;
  2837. utmp[0] &= kmask1;
  2838. const uint8x8_t mins8 = vld1_u8((const uint8_t*)utmp + 8);
  2839. const int16x8_t mins = vreinterpretq_s16_u16(vmovl_u8(mins8));
  2840. const int32x4_t prod = vaddq_s32(vmull_s16(vget_low_s16 (q8sums), vget_low_s16 (mins)),
  2841. vmull_s16(vget_high_s16(q8sums), vget_high_s16(mins)));
  2842. int32_t sumi_mins = vaddvq_s32(prod);
  2843. const uint8_t * scales = (const uint8_t *)utmp;
  2844. const uint8_t * restrict q5 = x[i].qs;
  2845. const uint8_t * restrict qh = x[i].qh;
  2846. const int8_t * restrict q8 = y[i].qs;
  2847. uint8x16x2_t qhbits = vld1q_u8_x2(qh);
  2848. uint8x16x4_t q5h;
  2849. int32_t sumi = 0;
  2850. for (int j = 0; j < QK_K/64; ++j) {
  2851. const uint8x16x2_t q5bits = vld1q_u8_x2(q5); q5 += 32;
  2852. const int8x16x4_t q8bytes = vld1q_s8_x4(q8); q8 += 64;
  2853. q5h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  2854. q5h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  2855. q5h.val[2] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[0]), 3);
  2856. q5h.val[3] = vshlq_n_u8(vandq_u8(mtwo, qhbits.val[1]), 3);
  2857. qhbits.val[0] = vshrq_n_u8(qhbits.val[0], 2);
  2858. qhbits.val[1] = vshrq_n_u8(qhbits.val[1], 2);
  2859. q5bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[0], m4b), q5h.val[0]));
  2860. q5bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q5bits.val[1], m4b), q5h.val[1]));
  2861. q5bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[0], 4), q5h.val[2]));
  2862. q5bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q5bits.val[1], 4), q5h.val[3]));
  2863. #if defined(__ARM_FEATURE_DOTPROD)
  2864. sumi += vaddvq_s32(vdotq_s32(vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]), q5bytes.val[1], q8bytes.val[1])) * *scales++;
  2865. sumi += vaddvq_s32(vdotq_s32(vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]), q5bytes.val[3], q8bytes.val[3])) * *scales++;
  2866. #else
  2867. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  2868. vmull_s8(vget_high_s8(q5bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  2869. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  2870. vmull_s8(vget_high_s8(q5bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  2871. sumi += vaddvq_s16(vaddq_s16(p0, p1)) * *scales++;
  2872. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  2873. vmull_s8(vget_high_s8(q5bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  2874. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  2875. vmull_s8(vget_high_s8(q5bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  2876. sumi += vaddvq_s16(vaddq_s16(p2, p3)) * *scales++;
  2877. #endif
  2878. }
  2879. sumf += d * sumi - dmin * sumi_mins;
  2880. }
  2881. *s = sumf;
  2882. #elif defined __AVX2__
  2883. const __m256i m4 = _mm256_set1_epi8(0xF);
  2884. const __m128i mzero = _mm_setzero_si128();
  2885. const __m256i mone = _mm256_set1_epi8(1);
  2886. __m256 acc = _mm256_setzero_ps();
  2887. float summs = 0.f;
  2888. for (int i = 0; i < nb; ++i) {
  2889. const uint8_t * restrict q5 = x[i].qs;
  2890. const int8_t * restrict q8 = y[i].qs;
  2891. #if QK_K == 256
  2892. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2893. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  2894. memcpy(utmp, x[i].scales, 12);
  2895. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  2896. const uint32_t uaux = utmp[1] & kmask1;
  2897. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  2898. utmp[2] = uaux;
  2899. utmp[0] &= kmask1;
  2900. #else
  2901. // TODO
  2902. const float d = 0, dmin = 0;
  2903. #endif
  2904. const __m256i mins_and_scales = _mm256_cvtepu8_epi16(_mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]));
  2905. const __m256i q8sums = _mm256_loadu_si256((const __m256i*)y[i].bsums);
  2906. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  2907. const __m128i prod = _mm_madd_epi16(_mm256_extracti128_si256(mins_and_scales, 1), q8s);
  2908. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  2909. summs += dmin * _mm_extract_epi32(hsum, 0);
  2910. const __m128i sc128 = _mm256_extracti128_si256(mins_and_scales, 0);
  2911. const __m256i scales = MM256_SET_M128I(sc128, sc128);
  2912. const __m256i hbits = _mm256_loadu_si256((const __m256i*)x[i].qh);
  2913. __m256i hmask = mone;
  2914. __m256i sumi = _mm256_setzero_si256();
  2915. int bit = 0;
  2916. for (int j = 0; j < QK_K/64; ++j) {
  2917. const __m256i scale_0 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+0));
  2918. const __m256i scale_1 = _mm256_shuffle_epi8(scales, get_scale_shuffle_k4(2*j+1));
  2919. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5); q5 += 32;
  2920. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  2921. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  2922. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  2923. hmask = _mm256_slli_epi16(hmask, 1);
  2924. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  2925. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), bit++), 4);
  2926. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  2927. hmask = _mm256_slli_epi16(hmask, 1);
  2928. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2929. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  2930. __m256i p16_0 = _mm256_maddubs_epi16(q5_0, q8_0);
  2931. __m256i p16_1 = _mm256_maddubs_epi16(q5_1, q8_1);
  2932. p16_0 = _mm256_madd_epi16(scale_0, p16_0);
  2933. p16_1 = _mm256_madd_epi16(scale_1, p16_1);
  2934. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  2935. }
  2936. __m256 vd = _mm256_set1_ps(d);
  2937. acc = _mm256_fmadd_ps(vd, _mm256_cvtepi32_ps(sumi), acc);
  2938. }
  2939. *s = hsum_float_8(acc) + summs;
  2940. #elif defined __AVX__
  2941. const __m128i m4 = _mm_set1_epi8(0xF);
  2942. const __m128i mzero = _mm_setzero_si128();
  2943. const __m128i mone = _mm_set1_epi8(1);
  2944. const __m128i m2 = _mm_set1_epi8(2);
  2945. __m256 acc = _mm256_setzero_ps();
  2946. float summs = 0.f;
  2947. for (int i = 0; i < nb; ++i) {
  2948. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  2949. const float dmin = -y[i].d * ggml_fp16_to_fp32(x[i].dmin);
  2950. const uint8_t * restrict q5 = x[i].qs;
  2951. const int8_t * restrict q8 = y[i].qs;
  2952. memcpy(utmp, x[i].scales, 12);
  2953. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  2954. const uint32_t uaux = utmp[1] & kmask1;
  2955. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  2956. utmp[2] = uaux;
  2957. utmp[0] &= kmask1;
  2958. const __m128i utmps = _mm_set_epi32(utmp[3], utmp[2], utmp[1], utmp[0]);
  2959. const __m128i scales = _mm_cvtepu8_epi16(utmps);
  2960. const __m128i mins = _mm_cvtepu8_epi16(_mm_unpackhi_epi64(utmps, utmps));
  2961. const __m128i q8sums_0 = _mm_loadu_si128((const __m128i*)&y[i].bsums[0]);
  2962. const __m128i q8sums_1 = _mm_loadu_si128((const __m128i*)&y[i].bsums[8]);
  2963. const __m128i q8s = _mm_hadd_epi16(q8sums_0, q8sums_1);
  2964. const __m128i prod = _mm_madd_epi16(mins, q8s);
  2965. const __m128i hsum = _mm_hadd_epi32(_mm_hadd_epi32(prod, mzero), mzero);
  2966. summs += dmin * _mm_extract_epi32(hsum, 0);
  2967. const __m128i hbits_0 = _mm_loadu_si128((const __m128i*)&x[i].qh[0]);
  2968. const __m128i hbits_1 = _mm_loadu_si128((const __m128i*)&x[i].qh[16]);
  2969. __m128i hmask = mone;
  2970. __m128i sumi_0 = _mm_setzero_si128();
  2971. __m128i sumi_1 = _mm_setzero_si128();
  2972. int bit = 0;
  2973. __m128i shuffle = _mm_set1_epi16(0x0100);
  2974. for (int j = 0; j < QK_K/64; ++j) {
  2975. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  2976. shuffle = _mm_add_epi16(shuffle, m2);
  2977. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  2978. shuffle = _mm_add_epi16(shuffle, m2);
  2979. const __m128i q5bits_0 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  2980. const __m128i q5bits_1 = _mm_loadu_si128((const __m128i*)q5); q5 += 16;
  2981. __m128i q5l_0 = _mm_and_si128(q5bits_0, m4);
  2982. __m128i q5l_1 = _mm_and_si128(q5bits_1, m4);
  2983. __m128i q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  2984. __m128i q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  2985. __m128i q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  2986. __m128i q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  2987. hmask = _mm_slli_epi16(hmask, 1);
  2988. __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  2989. __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  2990. __m128i p16_0 = _mm_maddubs_epi16(q5_0, q8_0);
  2991. __m128i p16_1 = _mm_maddubs_epi16(q5_1, q8_1);
  2992. p16_0 = _mm_madd_epi16(scale_0, p16_0);
  2993. p16_1 = _mm_madd_epi16(scale_0, p16_1);
  2994. q5l_0 = _mm_and_si128(_mm_srli_epi16(q5bits_0, 4), m4);
  2995. q5l_1 = _mm_and_si128(_mm_srli_epi16(q5bits_1, 4), m4);
  2996. q5h_0 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_0, hmask), bit), 4);
  2997. q5h_1 = _mm_slli_epi16(_mm_srli_epi16(_mm_and_si128(hbits_1, hmask), bit++), 4);
  2998. q5_0 = _mm_add_epi8(q5l_0, q5h_0);
  2999. q5_1 = _mm_add_epi8(q5l_1, q5h_1);
  3000. hmask = _mm_slli_epi16(hmask, 1);
  3001. q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3002. q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3003. __m128i p16_2 = _mm_maddubs_epi16(q5_0, q8_0);
  3004. __m128i p16_3 = _mm_maddubs_epi16(q5_1, q8_1);
  3005. p16_2 = _mm_madd_epi16(scale_1, p16_2);
  3006. p16_3 = _mm_madd_epi16(scale_1, p16_3);
  3007. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  3008. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  3009. }
  3010. __m256 vd = _mm256_set1_ps(d);
  3011. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3012. acc = _mm256_add_ps(_mm256_mul_ps(vd, _mm256_cvtepi32_ps(sumi)), acc);
  3013. }
  3014. *s = hsum_float_8(acc) + summs;
  3015. #elif defined __riscv_v_intrinsic
  3016. const uint8_t * scales = (const uint8_t*)&utmp[0];
  3017. const uint8_t * mins = (const uint8_t*)&utmp[2];
  3018. float sumf = 0;
  3019. float sums = 0.0;
  3020. size_t vl;
  3021. for (int i = 0; i < nb; ++i) {
  3022. vl = 8;
  3023. const uint8_t * restrict q5 = x[i].qs;
  3024. const uint8_t * restrict hm = x[i].qh;
  3025. const int8_t * restrict q8 = y[i].qs;
  3026. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  3027. const float dmin = ggml_fp16_to_fp32(x[i].dmin) * y[i].d;
  3028. vint16mf2_t q8sums_0 = __riscv_vlse16_v_i16mf2(y[i].bsums, 4, vl);
  3029. vint16mf2_t q8sums_1 = __riscv_vlse16_v_i16mf2(y[i].bsums+1, 4, vl);
  3030. vint16mf2_t q8sums = __riscv_vadd_vv_i16mf2(q8sums_0, q8sums_1, vl);
  3031. memcpy(utmp, x[i].scales, 12);
  3032. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  3033. const uint32_t uaux = utmp[1] & kmask1;
  3034. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  3035. utmp[2] = uaux;
  3036. utmp[0] &= kmask1;
  3037. vuint8mf4_t mins8 = __riscv_vle8_v_u8mf4(mins, vl);
  3038. vint16mf2_t v_mins = __riscv_vreinterpret_v_u16mf2_i16mf2(__riscv_vzext_vf2_u16mf2(mins8, vl));
  3039. vint32m1_t prod = __riscv_vwmul_vv_i32m1(q8sums, v_mins, vl);
  3040. vint32m1_t sumi = __riscv_vredsum_vs_i32m1_i32m1(prod, __riscv_vmv_v_x_i32m1(0, 1), vl);
  3041. sumf -= dmin * __riscv_vmv_x_s_i32m1_i32(sumi);
  3042. vl = 32;
  3043. int32_t aux32 = 0;
  3044. int is = 0;
  3045. uint8_t m = 1;
  3046. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3047. vuint8m1_t vqh = __riscv_vle8_v_u8m1(hm, vl);
  3048. for (int j = 0; j < QK_K/64; ++j) {
  3049. // load Q5 and Q8
  3050. vuint8m1_t q5_x = __riscv_vle8_v_u8m1(q5, vl);
  3051. vint8m1_t q8_y1 = __riscv_vle8_v_i8m1(q8, vl);
  3052. vint8m1_t q8_y2 = __riscv_vle8_v_i8m1(q8+32, vl);
  3053. // compute mask for addition
  3054. vint8m1_t q5_a = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vand_vx_u8m1(q5_x, 0x0F, vl));
  3055. vuint8m1_t qh_m1 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3056. vbool8_t vmask_1 = __riscv_vmsne_vx_u8m1_b8(qh_m1, 0, vl);
  3057. vint8m1_t q5_m1 = __riscv_vadd_vx_i8m1_m(vmask_1, q5_a, 16, vl);
  3058. m <<= 1;
  3059. vint8m1_t q5_l = __riscv_vreinterpret_v_u8m1_i8m1(__riscv_vsrl_vx_u8m1(q5_x, 0x04, vl));
  3060. vuint8m1_t qh_m2 = __riscv_vand_vx_u8m1(vqh, m, vl);
  3061. vbool8_t vmask_2 = __riscv_vmsne_vx_u8m1_b8(qh_m2, 0, vl);
  3062. vint8m1_t q5_m2 = __riscv_vadd_vx_i8m1_m(vmask_2, q5_l, 16, vl);
  3063. m <<= 1;
  3064. vint16m2_t v0 = __riscv_vwmul_vv_i16m2(q5_m1, q8_y1, vl);
  3065. vint16m2_t v1 = __riscv_vwmul_vv_i16m2(q5_m2, q8_y2, vl);
  3066. vint32m4_t vs1 = __riscv_vwmul_vx_i32m4(v0, scales[is++], vl);
  3067. vint32m4_t vs2 = __riscv_vwmul_vx_i32m4(v1, scales[is++], vl);
  3068. vint32m1_t vacc1 = __riscv_vredsum_vs_i32m4_i32m1(vs1, vzero, vl);
  3069. vint32m1_t vacc2 = __riscv_vredsum_vs_i32m4_i32m1(vs2, vzero, vl);
  3070. aux32 += __riscv_vmv_x_s_i32m1_i32(vacc1) + __riscv_vmv_x_s_i32m1_i32(vacc2);
  3071. q5 += 32; q8 += 64;
  3072. }
  3073. vfloat32m1_t vaux = __riscv_vfmul_vf_f32m1(__riscv_vfmv_v_f_f32m1(aux32, 1), d, 1);
  3074. sums += __riscv_vfmv_f_s_f32m1_f32(vaux);
  3075. }
  3076. *s = sumf+sums;
  3077. #else
  3078. const uint8_t * scales = (const uint8_t*)&utmp[0];
  3079. const uint8_t * mins = (const uint8_t*)&utmp[2];
  3080. int8_t aux8[QK_K];
  3081. int16_t aux16[8];
  3082. float sums [8];
  3083. int32_t aux32[8];
  3084. memset(sums, 0, 8*sizeof(float));
  3085. float sumf = 0;
  3086. for (int i = 0; i < nb; ++i) {
  3087. const uint8_t * restrict q4 = x[i].qs;
  3088. const uint8_t * restrict hm = x[i].qh;
  3089. const int8_t * restrict q8 = y[i].qs;
  3090. memset(aux32, 0, 8*sizeof(int32_t));
  3091. int8_t * restrict a = aux8;
  3092. uint8_t m = 1;
  3093. for (int j = 0; j < QK_K/64; ++j) {
  3094. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] & 0xF);
  3095. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  3096. a += 32; m <<= 1;
  3097. for (int l = 0; l < 32; ++l) a[l] = (int8_t)(q4[l] >> 4);
  3098. for (int l = 0; l < 32; ++l) a[l] += (hm[l] & m ? 16 : 0);
  3099. a += 32; m <<= 1;
  3100. q4 += 32;
  3101. }
  3102. memcpy(utmp, x[i].scales, 12);
  3103. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  3104. const uint32_t uaux = utmp[1] & kmask1;
  3105. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  3106. utmp[2] = uaux;
  3107. utmp[0] &= kmask1;
  3108. int sumi = 0;
  3109. for (int j = 0; j < QK_K/16; ++j) sumi += y[i].bsums[j] * mins[j/2];
  3110. a = aux8;
  3111. int is = 0;
  3112. for (int j = 0; j < QK_K/32; ++j) {
  3113. int32_t scale = scales[is++];
  3114. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3115. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  3116. q8 += 8; a += 8;
  3117. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3118. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  3119. q8 += 8; a += 8;
  3120. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3121. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  3122. q8 += 8; a += 8;
  3123. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3124. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  3125. q8 += 8; a += 8;
  3126. }
  3127. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  3128. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  3129. const float dmin = ggml_fp16_to_fp32(x[i].dmin) * y[i].d;
  3130. sumf -= dmin * sumi;
  3131. }
  3132. for (int l = 0; l < 8; ++l) sumf += sums[l];
  3133. *s = sumf;
  3134. #endif
  3135. }
  3136. #else
  3137. void ggml_vec_dot_q5_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3138. assert(n % QK_K == 0);
  3139. const block_q5_K * restrict x = vx;
  3140. const block_q8_K * restrict y = vy;
  3141. const int nb = n / QK_K;
  3142. #ifdef __ARM_NEON
  3143. const uint8x16_t m4b = vdupq_n_u8(0xf);
  3144. const uint8x16_t mh = vdupq_n_u8(16);
  3145. #if defined(__ARM_FEATURE_DOTPROD)
  3146. const int32x4_t mzero = vdupq_n_s32(0);
  3147. #endif
  3148. int8x16x4_t q5bytes;
  3149. uint8x16x4_t q5h;
  3150. float sumf = 0;
  3151. for (int i = 0; i < nb; ++i) {
  3152. const float d = y[i].d * (float)x[i].d;
  3153. const int8_t * sc = x[i].scales;
  3154. const uint8_t * restrict q5 = x[i].qs;
  3155. const uint8_t * restrict qh = x[i].qh;
  3156. const int8_t * restrict q8 = y[i].qs;
  3157. const uint8x8_t qhbits = vld1_u8(qh);
  3158. const uint8x16x2_t q5bits = vld1q_u8_x2(q5);
  3159. const int8x16x4_t q8bytes = vld1q_s8_x4(q8);
  3160. const uint8x16_t htmp = vcombine_u8(qhbits, vshr_n_u8(qhbits, 1));
  3161. q5h.val[0] = vbicq_u8(mh, vshlq_n_u8(htmp, 4));
  3162. q5h.val[1] = vbicq_u8(mh, vshlq_n_u8(htmp, 2));
  3163. q5h.val[2] = vbicq_u8(mh, htmp);
  3164. q5h.val[3] = vbicq_u8(mh, vshrq_n_u8(htmp, 2));
  3165. q5bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[0], m4b)), vreinterpretq_s8_u8(q5h.val[0]));
  3166. q5bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vandq_u8(q5bits.val[1], m4b)), vreinterpretq_s8_u8(q5h.val[1]));
  3167. q5bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[0], 4)), vreinterpretq_s8_u8(q5h.val[2]));
  3168. q5bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vshrq_n_u8(q5bits.val[1], 4)), vreinterpretq_s8_u8(q5h.val[3]));
  3169. #if defined(__ARM_FEATURE_DOTPROD)
  3170. int32_t sumi1 = sc[0] * vaddvq_s32(vdotq_s32(mzero, q5bytes.val[0], q8bytes.val[0]));
  3171. int32_t sumi2 = sc[1] * vaddvq_s32(vdotq_s32(mzero, q5bytes.val[1], q8bytes.val[1]));
  3172. int32_t sumi3 = sc[2] * vaddvq_s32(vdotq_s32(mzero, q5bytes.val[2], q8bytes.val[2]));
  3173. int32_t sumi4 = sc[3] * vaddvq_s32(vdotq_s32(mzero, q5bytes.val[3], q8bytes.val[3]));
  3174. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  3175. #else
  3176. const int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  3177. vmull_s8(vget_high_s8(q5bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  3178. const int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  3179. vmull_s8(vget_high_s8(q5bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  3180. int32_t sumi = sc[0] * vaddvq_s16(p0) + sc[1] * vaddvq_s16(p1);
  3181. const int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  3182. vmull_s8(vget_high_s8(q5bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  3183. const int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q5bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  3184. vmull_s8(vget_high_s8(q5bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  3185. sumi += sc[2] * vaddvq_s16(p2) + sc[3] * vaddvq_s16(p3);
  3186. sumf += d*sumi;
  3187. #endif
  3188. }
  3189. *s = sumf;
  3190. #elif defined __AVX2__
  3191. const __m256i m4 = _mm256_set1_epi8(0xF);
  3192. const __m256i mone = _mm256_set1_epi8(1);
  3193. __m256 acc = _mm256_setzero_ps();
  3194. for (int i = 0; i < nb; ++i) {
  3195. const uint8_t * restrict q5 = x[i].qs;
  3196. const int8_t * restrict q8 = y[i].qs;
  3197. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  3198. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  3199. const __m256i scale_l = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[1]), _mm_set1_epi16(x[i].scales[0]));
  3200. const __m256i scale_h = MM256_SET_M128I(_mm_set1_epi16(x[i].scales[3]), _mm_set1_epi16(x[i].scales[2]));
  3201. int64_t aux64;
  3202. memcpy(&aux64, x[i].qh, 8);
  3203. const __m128i haux128 = _mm_set_epi64x(aux64 >> 1, aux64);
  3204. const __m256i haux256 = MM256_SET_M128I(_mm_srli_epi16(haux128, 2), haux128);
  3205. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_andnot_si256(haux256, mone), 4);
  3206. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_andnot_si256(_mm256_srli_epi16(haux256, 4), mone), 4);
  3207. const __m256i q5l_0 = _mm256_and_si256(q5bits, m4);
  3208. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), m4);
  3209. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3210. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3211. const __m256i p16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5l_0, q8_0));
  3212. const __m256i p16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5l_1, q8_1));
  3213. const __m256i s16_0 = _mm256_madd_epi16(scale_l, _mm256_maddubs_epi16(q5h_0, q8_0));
  3214. const __m256i s16_1 = _mm256_madd_epi16(scale_h, _mm256_maddubs_epi16(q5h_1, q8_1));
  3215. const __m256i dot = _mm256_sub_epi32(_mm256_add_epi32(p16_0, p16_1), _mm256_add_epi32(s16_0, s16_1));
  3216. acc = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(dot), acc);
  3217. }
  3218. *s = hsum_float_8(acc);
  3219. #elif defined __AVX__
  3220. const __m128i m4 = _mm_set1_epi8(0xF);
  3221. const __m128i mone = _mm_set1_epi8(1);
  3222. __m256 acc = _mm256_setzero_ps();
  3223. for (int i = 0; i < nb; ++i) {
  3224. const uint8_t * restrict q5 = x[i].qs;
  3225. const int8_t * restrict q8 = y[i].qs;
  3226. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  3227. const __m256i q5bits = _mm256_loadu_si256((const __m256i*)q5);
  3228. const __m128i scale_0 = _mm_set1_epi16(x[i].scales[0]);
  3229. const __m128i scale_1 = _mm_set1_epi16(x[i].scales[1]);
  3230. const __m128i scale_2 = _mm_set1_epi16(x[i].scales[2]);
  3231. const __m128i scale_3 = _mm_set1_epi16(x[i].scales[3]);
  3232. int64_t aux64;
  3233. memcpy(&aux64, x[i].qh, 8);
  3234. const __m128i haux128_0 = _mm_set_epi64x(aux64 >> 1, aux64);
  3235. const __m128i haux128_1 = _mm_srli_epi16(haux128_0, 2);
  3236. const __m128i q5h_0 = _mm_slli_epi16(_mm_andnot_si128(haux128_0, mone), 4);
  3237. const __m128i q5h_1 = _mm_slli_epi16(_mm_andnot_si128(haux128_1, mone), 4);
  3238. const __m128i q5h_2 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_0, 4), mone), 4);
  3239. const __m128i q5h_3 = _mm_slli_epi16(_mm_andnot_si128(_mm_srli_epi16(haux128_1, 4), mone), 4);
  3240. const __m128i q5l_0 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 0), m4);
  3241. const __m128i q5l_1 = _mm_and_si128(_mm256_extractf128_si256(q5bits, 1), m4);
  3242. const __m128i q5l_2 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 0), 4), m4);
  3243. const __m128i q5l_3 = _mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q5bits, 1), 4), m4);
  3244. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3245. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3246. const __m128i p16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5l_0, _mm256_extractf128_si256(q8_0, 0)));
  3247. const __m128i p16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5l_1, _mm256_extractf128_si256(q8_0, 1)));
  3248. const __m128i p16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5l_2, _mm256_extractf128_si256(q8_1, 0)));
  3249. const __m128i p16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5l_3, _mm256_extractf128_si256(q8_1, 1)));
  3250. const __m128i s16_0 = _mm_madd_epi16(scale_0, _mm_maddubs_epi16(q5h_0, _mm256_extractf128_si256(q8_0, 0)));
  3251. const __m128i s16_1 = _mm_madd_epi16(scale_1, _mm_maddubs_epi16(q5h_1, _mm256_extractf128_si256(q8_0, 1)));
  3252. const __m128i s16_2 = _mm_madd_epi16(scale_2, _mm_maddubs_epi16(q5h_2, _mm256_extractf128_si256(q8_1, 0)));
  3253. const __m128i s16_3 = _mm_madd_epi16(scale_3, _mm_maddubs_epi16(q5h_3, _mm256_extractf128_si256(q8_1, 1)));
  3254. const __m128i dot_0 = _mm_sub_epi32(_mm_add_epi32(p16_0, p16_2), _mm_add_epi32(s16_0, s16_2));
  3255. const __m128i dot_1 = _mm_sub_epi32(_mm_add_epi32(p16_1, p16_3), _mm_add_epi32(s16_1, s16_3));
  3256. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(MM256_SET_M128I(dot_1, dot_0))), acc);
  3257. }
  3258. *s = hsum_float_8(acc);
  3259. #elif defined __riscv_v_intrinsic
  3260. float sumf = 0;
  3261. for (int i = 0; i < nb; ++i) {
  3262. const float d = y[i].d * (float)x[i].d;
  3263. const int8_t * sc = x[i].scales;
  3264. const uint8_t * restrict q5 = x[i].qs;
  3265. const uint8_t * restrict qh = x[i].qh;
  3266. const int8_t * restrict q8 = y[i].qs;
  3267. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3268. // load qh
  3269. vuint8mf4_t qh_x1 = __riscv_vle8_v_u8mf4(qh, 8);
  3270. vuint8mf2_t qh_x2 = __riscv_vlmul_ext_v_u8mf4_u8mf2(__riscv_vsrl_vx_u8mf4(qh_x1, 1, 8));
  3271. size_t vl = 16;
  3272. // combine both qh_1 and qh_2
  3273. vuint8mf2_t qh_x = __riscv_vslideup_vx_u8mf2(__riscv_vlmul_ext_v_u8mf4_u8mf2(qh_x1), qh_x2, vl/2, vl);
  3274. vuint8mf2_t qh_h0 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  3275. vuint8mf2_t qh_h1 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsll_vx_u8mf2(qh_x, 0x2, vl), vl), 16, vl);
  3276. vuint8mf2_t qh_h2 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(qh_x, vl), 16, vl);
  3277. vuint8mf2_t qh_h3 = __riscv_vand_vx_u8mf2(__riscv_vnot_v_u8mf2(__riscv_vsrl_vx_u8mf2(qh_x, 0x4, vl), vl), 16, vl);
  3278. vint8mf2_t qh_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h0);
  3279. vint8mf2_t qh_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h1);
  3280. vint8mf2_t qh_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h2);
  3281. vint8mf2_t qh_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(qh_h3);
  3282. // load q5
  3283. vuint8mf2_t q5_x1 = __riscv_vle8_v_u8mf2(q5, vl);
  3284. vuint8mf2_t q5_x2 = __riscv_vle8_v_u8mf2(q5+16, vl);
  3285. vint8mf2_t q5s_0 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x1, 0xF, vl));
  3286. vint8mf2_t q5s_1 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vand_vx_u8mf2(q5_x2, 0xF, vl));
  3287. vint8mf2_t q5s_2 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x1, 0x4, vl));
  3288. vint8mf2_t q5s_3 = __riscv_vreinterpret_v_u8mf2_i8mf2(__riscv_vsrl_vx_u8mf2(q5_x2, 0x4, vl));
  3289. vint8mf2_t q5_0 = __riscv_vsub_vv_i8mf2(q5s_0, qh_0, vl);
  3290. vint8mf2_t q5_1 = __riscv_vsub_vv_i8mf2(q5s_1, qh_1, vl);
  3291. vint8mf2_t q5_2 = __riscv_vsub_vv_i8mf2(q5s_2, qh_2, vl);
  3292. vint8mf2_t q5_3 = __riscv_vsub_vv_i8mf2(q5s_3, qh_3, vl);
  3293. // load Q8 and multiply it with Q5
  3294. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q5_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  3295. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q5_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  3296. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q5_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  3297. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q5_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  3298. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  3299. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  3300. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  3301. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  3302. int32_t sumi1 = sc[0] * __riscv_vmv_x_s_i32m1_i32(vs_0);
  3303. int32_t sumi2 = sc[1] * __riscv_vmv_x_s_i32m1_i32(vs_1);
  3304. int32_t sumi3 = sc[2] * __riscv_vmv_x_s_i32m1_i32(vs_2);
  3305. int32_t sumi4 = sc[3] * __riscv_vmv_x_s_i32m1_i32(vs_3);
  3306. sumf += d * (sumi1 + sumi2 + sumi3 + sumi4);
  3307. }
  3308. *s = sumf;
  3309. #else
  3310. int8_t aux8[QK_K];
  3311. int16_t aux16[16];
  3312. float sums [8];
  3313. memset(sums, 0, 8*sizeof(float));
  3314. float sumf = 0;
  3315. for (int i = 0; i < nb; ++i) {
  3316. const uint8_t * restrict q4 = x[i].qs;
  3317. const uint8_t * restrict hm = x[i].qh;
  3318. const int8_t * restrict q8 = y[i].qs;
  3319. int8_t * restrict a = aux8;
  3320. for (int l = 0; l < 32; ++l) {
  3321. a[l+ 0] = q4[l] & 0xF;
  3322. a[l+32] = q4[l] >> 4;
  3323. }
  3324. for (int is = 0; is < 8; ++is) {
  3325. uint8_t m = 1 << is;
  3326. for (int l = 0; l < 8; ++l) a[8*is + l] -= (hm[l] & m ? 0 : 16);
  3327. }
  3328. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  3329. const int8_t * restrict sc = x[i].scales;
  3330. for (int j = 0; j < QK_K/16; ++j) {
  3331. const float dl = d * sc[j];
  3332. for (int l = 0; l < 16; ++l) aux16[l] = q8[l] * a[l];
  3333. for (int l = 0; l < 8; ++l) sums[l] += dl * (aux16[l] + aux16[8+l]);
  3334. q8 += 16; a += 16;
  3335. }
  3336. }
  3337. for (int l = 0; l < 8; ++l) sumf += sums[l];
  3338. *s = sumf;
  3339. #endif
  3340. }
  3341. #endif
  3342. #if QK_K == 256
  3343. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3344. assert(n % QK_K == 0);
  3345. const block_q6_K * restrict x = vx;
  3346. const block_q8_K * restrict y = vy;
  3347. const int nb = n / QK_K;
  3348. #ifdef __ARM_NEON
  3349. float sum = 0;
  3350. const uint8x16_t m4b = vdupq_n_u8(0xF);
  3351. #if defined(__ARM_FEATURE_DOTPROD)
  3352. const int32x4_t vzero = vdupq_n_s32(0);
  3353. #endif
  3354. //const int8x16_t m32s = vdupq_n_s8(32);
  3355. const uint8x16_t mone = vdupq_n_u8(3);
  3356. int8x16x4_t q6bytes;
  3357. uint8x16x4_t q6h;
  3358. for (int i = 0; i < nb; ++i) {
  3359. const float d_all = ggml_fp16_to_fp32(x[i].d);
  3360. const uint8_t * restrict q6 = x[i].ql;
  3361. const uint8_t * restrict qh = x[i].qh;
  3362. const int8_t * restrict q8 = y[i].qs;
  3363. const int8_t * restrict scale = x[i].scales;
  3364. const int16x8x2_t q8sums = vld1q_s16_x2(y[i].bsums);
  3365. const int8x16_t scales = vld1q_s8(scale);
  3366. const int16x8x2_t q6scales = {vmovl_s8(vget_low_s8(scales)), vmovl_s8(vget_high_s8(scales))};
  3367. const int32x4_t prod = vaddq_s32(vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[0]), vget_low_s16 (q6scales.val[0])),
  3368. vmull_s16(vget_high_s16(q8sums.val[0]), vget_high_s16(q6scales.val[0]))),
  3369. vaddq_s32(vmull_s16(vget_low_s16 (q8sums.val[1]), vget_low_s16 (q6scales.val[1])),
  3370. vmull_s16(vget_high_s16(q8sums.val[1]), vget_high_s16(q6scales.val[1]))));
  3371. int32_t isum_mins = vaddvq_s32(prod);
  3372. int32_t isum = 0;
  3373. for (int j = 0; j < QK_K/128; ++j) {
  3374. uint8x16x2_t qhbits = vld1q_u8_x2(qh); qh += 32;
  3375. uint8x16x4_t q6bits = vld1q_u8_x4(q6); q6 += 64;
  3376. int8x16x4_t q8bytes = vld1q_s8_x4(q8); q8 += 64;
  3377. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits.val[0]), 4);
  3378. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, qhbits.val[1]), 4);
  3379. uint8x16_t shifted = vshrq_n_u8(qhbits.val[0], 2);
  3380. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3381. shifted = vshrq_n_u8(qhbits.val[1], 2);
  3382. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3383. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  3384. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  3385. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2])), m32s);
  3386. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3])), m32s);
  3387. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0]));
  3388. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1]));
  3389. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[2], m4b), q6h.val[2]));
  3390. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[3], m4b), q6h.val[3]));
  3391. #if defined(__ARM_FEATURE_DOTPROD)
  3392. isum += vaddvq_s32(vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  3393. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  3394. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  3395. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  3396. scale += 4;
  3397. #else
  3398. int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  3399. vmull_s8(vget_high_s8(q6bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  3400. int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  3401. vmull_s8(vget_high_s8(q6bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  3402. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1];
  3403. scale += 2;
  3404. int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  3405. vmull_s8(vget_high_s8(q6bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  3406. int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  3407. vmull_s8(vget_high_s8(q6bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  3408. isum += vaddvq_s16(p2) * scale[0] + vaddvq_s16(p3) * scale[1];
  3409. scale += 2;
  3410. #endif
  3411. q8bytes = vld1q_s8_x4(q8); q8 += 64;
  3412. shifted = vshrq_n_u8(qhbits.val[0], 4);
  3413. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3414. shifted = vshrq_n_u8(qhbits.val[1], 4);
  3415. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3416. shifted = vshrq_n_u8(qhbits.val[0], 6);
  3417. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3418. shifted = vshrq_n_u8(qhbits.val[1], 6);
  3419. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3420. //q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0])), m32s);
  3421. //q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1])), m32s);
  3422. //q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2])), m32s);
  3423. //q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3])), m32s);
  3424. q6bytes.val[0] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[0]));
  3425. q6bytes.val[1] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[1]));
  3426. q6bytes.val[2] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[2], 4), q6h.val[2]));
  3427. q6bytes.val[3] = vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[3], 4), q6h.val[3]));
  3428. #if defined(__ARM_FEATURE_DOTPROD)
  3429. isum += vaddvq_s32(vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  3430. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  3431. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  3432. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  3433. scale += 4;
  3434. //for (int l = 0; l < 4; ++l) {
  3435. // const int32x4_t p = vdotq_s32(vzero, q6bytes.val[l], q8bytes.val[l]);
  3436. // isum += vaddvq_s32(p) * *scale++;
  3437. //}
  3438. #else
  3439. p0 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  3440. vmull_s8(vget_high_s8(q6bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  3441. p1 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  3442. vmull_s8(vget_high_s8(q6bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  3443. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1];
  3444. scale += 2;
  3445. p2 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  3446. vmull_s8(vget_high_s8(q6bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  3447. p3 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  3448. vmull_s8(vget_high_s8(q6bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  3449. isum += vaddvq_s16(p2) * scale[0] + vaddvq_s16(p3) * scale[1];
  3450. scale += 2;
  3451. #endif
  3452. }
  3453. //sum += isum * d_all * y[i].d;
  3454. sum += d_all * y[i].d * (isum - 32 * isum_mins);
  3455. }
  3456. *s = sum;
  3457. #elif defined __AVX2__
  3458. const __m256i m4 = _mm256_set1_epi8(0xF);
  3459. const __m256i m2 = _mm256_set1_epi8(3);
  3460. const __m256i m32s = _mm256_set1_epi8(32);
  3461. __m256 acc = _mm256_setzero_ps();
  3462. for (int i = 0; i < nb; ++i) {
  3463. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  3464. const uint8_t * restrict q4 = x[i].ql;
  3465. const uint8_t * restrict qh = x[i].qh;
  3466. const int8_t * restrict q8 = y[i].qs;
  3467. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3468. __m256i sumi = _mm256_setzero_si256();
  3469. int is = 0;
  3470. for (int j = 0; j < QK_K/128; ++j) {
  3471. const __m128i scale_0 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 0));
  3472. const __m128i scale_1 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 1));
  3473. const __m128i scale_2 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 2));
  3474. const __m128i scale_3 = _mm_shuffle_epi8(scales, get_scale_shuffle(is + 3));
  3475. is += 4;
  3476. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  3477. const __m256i q4bits2 = _mm256_loadu_si256((const __m256i*)q4); q4 += 32;
  3478. const __m256i q4bitsH = _mm256_loadu_si256((const __m256i*)qh); qh += 32;
  3479. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(q4bitsH, m2), 4);
  3480. const __m256i q4h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 2), m2), 4);
  3481. const __m256i q4h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 4), m2), 4);
  3482. const __m256i q4h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q4bitsH, 6), m2), 4);
  3483. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  3484. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(q4bits2, m4), q4h_1);
  3485. const __m256i q4_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_2);
  3486. const __m256i q4_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits2, 4), m4), q4h_3);
  3487. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3488. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3489. const __m256i q8_2 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3490. const __m256i q8_3 = _mm256_loadu_si256((const __m256i*)q8); q8 += 32;
  3491. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  3492. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  3493. __m256i q8s_2 = _mm256_maddubs_epi16(m32s, q8_2);
  3494. __m256i q8s_3 = _mm256_maddubs_epi16(m32s, q8_3);
  3495. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  3496. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  3497. __m256i p16_2 = _mm256_maddubs_epi16(q4_2, q8_2);
  3498. __m256i p16_3 = _mm256_maddubs_epi16(q4_3, q8_3);
  3499. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  3500. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  3501. p16_2 = _mm256_sub_epi16(p16_2, q8s_2);
  3502. p16_3 = _mm256_sub_epi16(p16_3, q8s_3);
  3503. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  3504. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  3505. p16_2 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_2), p16_2);
  3506. p16_3 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_3), p16_3);
  3507. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  3508. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_2, p16_3));
  3509. }
  3510. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3511. }
  3512. *s = hsum_float_8(acc);
  3513. #elif defined __AVX__
  3514. const __m128i m4 = _mm_set1_epi8(0xF);
  3515. const __m128i m3 = _mm_set1_epi8(3);
  3516. const __m128i m32s = _mm_set1_epi8(32);
  3517. const __m128i m2 = _mm_set1_epi8(2);
  3518. __m256 acc = _mm256_setzero_ps();
  3519. for (int i = 0; i < nb; ++i) {
  3520. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  3521. const uint8_t * restrict q4 = x[i].ql;
  3522. const uint8_t * restrict qh = x[i].qh;
  3523. const int8_t * restrict q8 = y[i].qs;
  3524. const __m128i scales = _mm_loadu_si128((const __m128i*)x[i].scales);
  3525. __m128i sumi_0 = _mm_setzero_si128();
  3526. __m128i sumi_1 = _mm_setzero_si128();
  3527. __m128i shuffle = _mm_set_epi64x(0x0101010101010101, 0x0000000000000000);
  3528. for (int j = 0; j < QK_K/128; ++j) {
  3529. const __m128i q4bitsH_0 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  3530. const __m128i q4bitsH_1 = _mm_loadu_si128((const __m128i*)qh); qh += 16;
  3531. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH_0, m3), 4);
  3532. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(q4bitsH_1, m3), 4);
  3533. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 2), m3), 4);
  3534. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 2), m3), 4);
  3535. const __m128i q4h_4 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 4), m3), 4);
  3536. const __m128i q4h_5 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 4), m3), 4);
  3537. const __m128i q4h_6 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_0, 6), m3), 4);
  3538. const __m128i q4h_7 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH_1, 6), m3), 4);
  3539. const __m128i q4bits1_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  3540. const __m128i q4bits1_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  3541. const __m128i q4bits2_0 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  3542. const __m128i q4bits2_1 = _mm_loadu_si128((const __m128i*)q4); q4 += 16;
  3543. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(q4bits1_0, m4), q4h_0);
  3544. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(q4bits1_1, m4), q4h_1);
  3545. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(q4bits2_0, m4), q4h_2);
  3546. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(q4bits2_1, m4), q4h_3);
  3547. const __m128i q4_4 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_0, 4), m4), q4h_4);
  3548. const __m128i q4_5 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits1_1, 4), m4), q4h_5);
  3549. const __m128i q4_6 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_0, 4), m4), q4h_6);
  3550. const __m128i q4_7 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(q4bits2_1, 4), m4), q4h_7);
  3551. const __m128i q8_0 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3552. const __m128i q8_1 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3553. const __m128i q8_2 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3554. const __m128i q8_3 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3555. const __m128i q8_4 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3556. const __m128i q8_5 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3557. const __m128i q8_6 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3558. const __m128i q8_7 = _mm_loadu_si128((const __m128i*)q8); q8 += 16;
  3559. __m128i q8s_0 = _mm_maddubs_epi16(m32s, q8_0);
  3560. __m128i q8s_1 = _mm_maddubs_epi16(m32s, q8_1);
  3561. __m128i q8s_2 = _mm_maddubs_epi16(m32s, q8_2);
  3562. __m128i q8s_3 = _mm_maddubs_epi16(m32s, q8_3);
  3563. __m128i q8s_4 = _mm_maddubs_epi16(m32s, q8_4);
  3564. __m128i q8s_5 = _mm_maddubs_epi16(m32s, q8_5);
  3565. __m128i q8s_6 = _mm_maddubs_epi16(m32s, q8_6);
  3566. __m128i q8s_7 = _mm_maddubs_epi16(m32s, q8_7);
  3567. __m128i p16_0 = _mm_maddubs_epi16(q4_0, q8_0);
  3568. __m128i p16_1 = _mm_maddubs_epi16(q4_1, q8_1);
  3569. __m128i p16_2 = _mm_maddubs_epi16(q4_2, q8_2);
  3570. __m128i p16_3 = _mm_maddubs_epi16(q4_3, q8_3);
  3571. __m128i p16_4 = _mm_maddubs_epi16(q4_4, q8_4);
  3572. __m128i p16_5 = _mm_maddubs_epi16(q4_5, q8_5);
  3573. __m128i p16_6 = _mm_maddubs_epi16(q4_6, q8_6);
  3574. __m128i p16_7 = _mm_maddubs_epi16(q4_7, q8_7);
  3575. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  3576. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  3577. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  3578. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  3579. p16_4 = _mm_sub_epi16(p16_4, q8s_4);
  3580. p16_5 = _mm_sub_epi16(p16_5, q8s_5);
  3581. p16_6 = _mm_sub_epi16(p16_6, q8s_6);
  3582. p16_7 = _mm_sub_epi16(p16_7, q8s_7);
  3583. const __m128i scale_0 = _mm_shuffle_epi8(scales, shuffle);
  3584. shuffle = _mm_add_epi8(shuffle, m2);
  3585. const __m128i scale_1 = _mm_shuffle_epi8(scales, shuffle);
  3586. shuffle = _mm_add_epi8(shuffle, m2);
  3587. const __m128i scale_2 = _mm_shuffle_epi8(scales, shuffle);
  3588. shuffle = _mm_add_epi8(shuffle, m2);
  3589. const __m128i scale_3 = _mm_shuffle_epi8(scales, shuffle);
  3590. shuffle = _mm_add_epi8(shuffle, m2);
  3591. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  3592. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  3593. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  3594. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  3595. p16_4 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_2), p16_4);
  3596. p16_5 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_2, scale_2)), p16_5);
  3597. p16_6 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_3), p16_6);
  3598. p16_7 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_3, scale_3)), p16_7);
  3599. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  3600. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  3601. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_4, p16_6));
  3602. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_5, p16_7));
  3603. }
  3604. __m256i sumi = MM256_SET_M128I(sumi_1, sumi_0);
  3605. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi)), acc);
  3606. }
  3607. *s = hsum_float_8(acc);
  3608. #elif defined __riscv_v_intrinsic
  3609. float sumf = 0;
  3610. for (int i = 0; i < nb; ++i) {
  3611. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  3612. const uint8_t * restrict q6 = x[i].ql;
  3613. const uint8_t * restrict qh = x[i].qh;
  3614. const int8_t * restrict q8 = y[i].qs;
  3615. const int8_t * restrict scale = x[i].scales;
  3616. size_t vl;
  3617. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3618. int sum_t = 0;
  3619. int is = 0;
  3620. for (int j = 0; j < QK_K/128; ++j) {
  3621. vl = 32;
  3622. // load qh
  3623. vuint8m1_t qh_x = __riscv_vle8_v_u8m1(qh, vl);
  3624. // load Q6
  3625. vuint8m1_t q6_0 = __riscv_vle8_v_u8m1(q6, vl);
  3626. vuint8m1_t q6_1 = __riscv_vle8_v_u8m1(q6+32, vl);
  3627. vuint8m1_t q6a_0 = __riscv_vand_vx_u8m1(q6_0, 0x0F, vl);
  3628. vuint8m1_t q6a_1 = __riscv_vand_vx_u8m1(q6_1, 0x0F, vl);
  3629. vuint8m1_t q6s_0 = __riscv_vsrl_vx_u8m1(q6_0, 0x04, vl);
  3630. vuint8m1_t q6s_1 = __riscv_vsrl_vx_u8m1(q6_1, 0x04, vl);
  3631. vuint8m1_t qh_0 = __riscv_vand_vx_u8m1(qh_x, 0x03, vl);
  3632. vuint8m1_t qh_1 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x2, vl), 0x03 , vl);
  3633. vuint8m1_t qh_2 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x4, vl), 0x03 , vl);
  3634. vuint8m1_t qh_3 = __riscv_vand_vx_u8m1(__riscv_vsrl_vx_u8m1(qh_x, 0x6, vl), 0x03 , vl);
  3635. vuint8m1_t qhi_0 = __riscv_vor_vv_u8m1(q6a_0, __riscv_vsll_vx_u8m1(qh_0, 0x04, vl), vl);
  3636. vuint8m1_t qhi_1 = __riscv_vor_vv_u8m1(q6a_1, __riscv_vsll_vx_u8m1(qh_1, 0x04, vl), vl);
  3637. vuint8m1_t qhi_2 = __riscv_vor_vv_u8m1(q6s_0, __riscv_vsll_vx_u8m1(qh_2, 0x04, vl), vl);
  3638. vuint8m1_t qhi_3 = __riscv_vor_vv_u8m1(q6s_1, __riscv_vsll_vx_u8m1(qh_3, 0x04, vl), vl);
  3639. vint8m1_t a_0 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_0), 32, vl);
  3640. vint8m1_t a_1 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_1), 32, vl);
  3641. vint8m1_t a_2 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_2), 32, vl);
  3642. vint8m1_t a_3 = __riscv_vsub_vx_i8m1(__riscv_vreinterpret_v_u8m1_i8m1(qhi_3), 32, vl);
  3643. // load Q8 and take product
  3644. vint16m2_t va_q_0 = __riscv_vwmul_vv_i16m2(a_0, __riscv_vle8_v_i8m1(q8, vl), vl);
  3645. vint16m2_t va_q_1 = __riscv_vwmul_vv_i16m2(a_1, __riscv_vle8_v_i8m1(q8+32, vl), vl);
  3646. vint16m2_t va_q_2 = __riscv_vwmul_vv_i16m2(a_2, __riscv_vle8_v_i8m1(q8+64, vl), vl);
  3647. vint16m2_t va_q_3 = __riscv_vwmul_vv_i16m2(a_3, __riscv_vle8_v_i8m1(q8+96, vl), vl);
  3648. vl = 16;
  3649. vint32m2_t vaux_0 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 0), scale[is+0], vl);
  3650. vint32m2_t vaux_1 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_0, 1), scale[is+1], vl);
  3651. vint32m2_t vaux_2 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 0), scale[is+2], vl);
  3652. vint32m2_t vaux_3 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_1, 1), scale[is+3], vl);
  3653. vint32m2_t vaux_4 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 0), scale[is+4], vl);
  3654. vint32m2_t vaux_5 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_2, 1), scale[is+5], vl);
  3655. vint32m2_t vaux_6 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 0), scale[is+6], vl);
  3656. vint32m2_t vaux_7 = __riscv_vwmul_vx_i32m2(__riscv_vget_v_i16m2_i16m1(va_q_3, 1), scale[is+7], vl);
  3657. vint32m1_t isum0 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_0, vaux_1, vl), vzero, vl);
  3658. vint32m1_t isum1 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_2, vaux_3, vl), isum0, vl);
  3659. vint32m1_t isum2 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_4, vaux_5, vl), isum1, vl);
  3660. vint32m1_t isum3 = __riscv_vredsum_vs_i32m2_i32m1(__riscv_vadd_vv_i32m2(vaux_6, vaux_7, vl), isum2, vl);
  3661. sum_t += __riscv_vmv_x_s_i32m1_i32(isum3);
  3662. q6 += 64; qh += 32; q8 += 128; is=8;
  3663. }
  3664. sumf += d * sum_t;
  3665. }
  3666. *s = sumf;
  3667. #else
  3668. int8_t aux8[QK_K];
  3669. int16_t aux16[8];
  3670. float sums [8];
  3671. int32_t aux32[8];
  3672. memset(sums, 0, 8*sizeof(float));
  3673. float sumf = 0;
  3674. for (int i = 0; i < nb; ++i) {
  3675. const uint8_t * restrict q4 = x[i].ql;
  3676. const uint8_t * restrict qh = x[i].qh;
  3677. const int8_t * restrict q8 = y[i].qs;
  3678. memset(aux32, 0, 8*sizeof(int32_t));
  3679. int8_t * restrict a = aux8;
  3680. for (int j = 0; j < QK_K; j += 128) {
  3681. for (int l = 0; l < 32; ++l) {
  3682. a[l + 0] = (int8_t)((q4[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  3683. a[l + 32] = (int8_t)((q4[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  3684. a[l + 64] = (int8_t)((q4[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  3685. a[l + 96] = (int8_t)((q4[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  3686. }
  3687. a += 128;
  3688. q4 += 64;
  3689. qh += 32;
  3690. }
  3691. a = aux8;
  3692. int is = 0;
  3693. for (int j = 0; j < QK_K/16; ++j) {
  3694. int scale = x[i].scales[is++];
  3695. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3696. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  3697. q8 += 8; a += 8;
  3698. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3699. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  3700. q8 += 8; a += 8;
  3701. }
  3702. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  3703. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  3704. }
  3705. for (int l = 0; l < 8; ++l) sumf += sums[l];
  3706. *s = sumf;
  3707. #endif
  3708. }
  3709. #else
  3710. void ggml_vec_dot_q6_K_q8_K(const int n, float * restrict s, const void * restrict vx, const void * restrict vy) {
  3711. assert(n % QK_K == 0);
  3712. const block_q6_K * restrict x = vx;
  3713. const block_q8_K * restrict y = vy;
  3714. const int nb = n / QK_K;
  3715. #ifdef __ARM_NEON
  3716. float sum = 0;
  3717. const uint8x16_t m4b = vdupq_n_u8(0xF);
  3718. const int8x16_t m32s = vdupq_n_s8(32);
  3719. #if defined(__ARM_FEATURE_DOTPROD)
  3720. const int32x4_t vzero = vdupq_n_s32(0);
  3721. #endif
  3722. const uint8x16_t mone = vdupq_n_u8(3);
  3723. int8x16x4_t q6bytes;
  3724. uint8x16x4_t q6h;
  3725. for (int i = 0; i < nb; ++i) {
  3726. const float d_all = (float)x[i].d;
  3727. const uint8_t * restrict q6 = x[i].ql;
  3728. const uint8_t * restrict qh = x[i].qh;
  3729. const int8_t * restrict q8 = y[i].qs;
  3730. const int8_t * restrict scale = x[i].scales;
  3731. int32_t isum = 0;
  3732. uint8x16_t qhbits = vld1q_u8(qh);
  3733. uint8x16x2_t q6bits = vld1q_u8_x2(q6);
  3734. int8x16x4_t q8bytes = vld1q_s8_x4(q8);
  3735. q6h.val[0] = vshlq_n_u8(vandq_u8(mone, qhbits), 4);
  3736. uint8x16_t shifted = vshrq_n_u8(qhbits, 2);
  3737. q6h.val[1] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3738. shifted = vshrq_n_u8(qhbits, 4);
  3739. q6h.val[2] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3740. shifted = vshrq_n_u8(qhbits, 6);
  3741. q6h.val[3] = vshlq_n_u8(vandq_u8(mone, shifted), 4);
  3742. q6bytes.val[0] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[0], m4b), q6h.val[0])), m32s);
  3743. q6bytes.val[1] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vandq_u8(q6bits.val[1], m4b), q6h.val[1])), m32s);
  3744. q6bytes.val[2] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[0], 4), q6h.val[2])), m32s);
  3745. q6bytes.val[3] = vsubq_s8(vreinterpretq_s8_u8(vorrq_u8(vshrq_n_u8(q6bits.val[1], 4), q6h.val[3])), m32s);
  3746. #if defined(__ARM_FEATURE_DOTPROD)
  3747. isum += vaddvq_s32(vdotq_s32(vzero, q6bytes.val[0], q8bytes.val[0])) * scale[0] +
  3748. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[1], q8bytes.val[1])) * scale[1] +
  3749. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[2], q8bytes.val[2])) * scale[2] +
  3750. vaddvq_s32(vdotq_s32(vzero, q6bytes.val[3], q8bytes.val[3])) * scale[3];
  3751. #else
  3752. int16x8_t p0 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[0]), vget_low_s8 (q8bytes.val[0])),
  3753. vmull_s8(vget_high_s8(q6bytes.val[0]), vget_high_s8(q8bytes.val[0])));
  3754. int16x8_t p1 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[1]), vget_low_s8 (q8bytes.val[1])),
  3755. vmull_s8(vget_high_s8(q6bytes.val[1]), vget_high_s8(q8bytes.val[1])));
  3756. isum += vaddvq_s16(p0) * scale[0] + vaddvq_s16(p1) * scale[1];
  3757. int16x8_t p2 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[2]), vget_low_s8 (q8bytes.val[2])),
  3758. vmull_s8(vget_high_s8(q6bytes.val[2]), vget_high_s8(q8bytes.val[2])));
  3759. int16x8_t p3 = vaddq_s16(vmull_s8(vget_low_s8 (q6bytes.val[3]), vget_low_s8 (q8bytes.val[3])),
  3760. vmull_s8(vget_high_s8(q6bytes.val[3]), vget_high_s8(q8bytes.val[3])));
  3761. isum += vaddvq_s16(p2) * scale[2] + vaddvq_s16(p3) * scale[3];
  3762. #endif
  3763. sum += isum * d_all * y[i].d;
  3764. }
  3765. *s = sum;
  3766. #elif defined __AVX2__
  3767. const __m256i m4 = _mm256_set1_epi8(0xF);
  3768. const __m256i m2 = _mm256_set1_epi8(3);
  3769. const __m256i m32s = _mm256_set1_epi8(32);
  3770. __m256 acc = _mm256_setzero_ps();
  3771. for (int i = 0; i < nb; ++i) {
  3772. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  3773. const uint8_t * restrict q4 = x[i].ql;
  3774. const uint8_t * restrict qh = x[i].qh;
  3775. const int8_t * restrict q8 = y[i].qs;
  3776. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  3777. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  3778. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  3779. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  3780. __m256i sumi = _mm256_setzero_si256();
  3781. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  3782. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  3783. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  3784. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  3785. const __m256i q4h_0 = _mm256_slli_epi16(_mm256_and_si256(MM256_SET_M128I(_mm_srli_epi16(q4bitsH, 2), q4bitsH), m2), 4);
  3786. 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);
  3787. const __m256i q4_0 = _mm256_or_si256(_mm256_and_si256(q4bits1, m4), q4h_0);
  3788. const __m256i q4_1 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q4bits1, 4), m4), q4h_1);
  3789. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3790. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3791. __m256i q8s_0 = _mm256_maddubs_epi16(m32s, q8_0);
  3792. __m256i q8s_1 = _mm256_maddubs_epi16(m32s, q8_1);
  3793. __m256i p16_0 = _mm256_maddubs_epi16(q4_0, q8_0);
  3794. __m256i p16_1 = _mm256_maddubs_epi16(q4_1, q8_1);
  3795. p16_0 = _mm256_sub_epi16(p16_0, q8s_0);
  3796. p16_1 = _mm256_sub_epi16(p16_1, q8s_1);
  3797. p16_0 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_0), p16_0);
  3798. p16_1 = _mm256_madd_epi16(_mm256_cvtepi8_epi16(scale_1), p16_1);
  3799. sumi = _mm256_add_epi32(sumi, _mm256_add_epi32(p16_0, p16_1));
  3800. acc = _mm256_fmadd_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(sumi), acc);
  3801. }
  3802. *s = hsum_float_8(acc);
  3803. #elif defined __AVX__
  3804. const __m128i m4 = _mm_set1_epi8(0xF);
  3805. const __m128i m2 = _mm_set1_epi8(3);
  3806. const __m128i m32s = _mm_set1_epi8(32);
  3807. __m256 acc = _mm256_setzero_ps();
  3808. for (int i = 0; i < nb; ++i) {
  3809. const float d = y[i].d * ggml_fp16_to_fp32(x[i].d);
  3810. const uint8_t * restrict q4 = x[i].ql;
  3811. const uint8_t * restrict qh = x[i].qh;
  3812. const int8_t * restrict q8 = y[i].qs;
  3813. const __m64 scales_1 = _mm_set1_pi8(x[i].scales[0]);
  3814. const __m64 scales_2 = _mm_set1_pi8(x[i].scales[1]);
  3815. const __m64 scales_3 = _mm_set1_pi8(x[i].scales[2]);
  3816. const __m64 scales_4 = _mm_set1_pi8(x[i].scales[3]);
  3817. __m128i sumi_0 = _mm_setzero_si128();
  3818. __m128i sumi_1 = _mm_setzero_si128();
  3819. const __m128i scale_0 = _mm_set_epi64(scales_2, scales_1);
  3820. const __m128i scale_1 = _mm_set_epi64(scales_4, scales_3);
  3821. const __m256i q4bits1 = _mm256_loadu_si256((const __m256i*)q4);
  3822. const __m128i q4bitsH = _mm_loadu_si128((const __m128i*)qh);
  3823. const __m128i q4h_0 = _mm_slli_epi16(_mm_and_si128(q4bitsH, m2), 4);
  3824. const __m128i q4h_1 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 2), m2), 4);
  3825. const __m128i q4h_2 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 4), m2), 4);
  3826. const __m128i q4h_3 = _mm_slli_epi16(_mm_and_si128(_mm_srli_epi16(q4bitsH, 6), m2), 4);
  3827. const __m128i q4_0 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 0), m4), q4h_0);
  3828. const __m128i q4_1 = _mm_or_si128(_mm_and_si128(_mm256_extractf128_si256(q4bits1, 1), m4), q4h_1);
  3829. const __m128i q4_2 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 0), 4), m4), q4h_2);
  3830. const __m128i q4_3 = _mm_or_si128(_mm_and_si128(_mm_srli_epi16(_mm256_extractf128_si256(q4bits1, 1), 4), m4), q4h_3);
  3831. const __m256i q8_0 = _mm256_loadu_si256((const __m256i*)(q8+ 0));
  3832. const __m256i q8_1 = _mm256_loadu_si256((const __m256i*)(q8+32));
  3833. __m128i q8s_0 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 0));
  3834. __m128i q8s_1 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_0, 1));
  3835. __m128i q8s_2 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 0));
  3836. __m128i q8s_3 = _mm_maddubs_epi16(m32s, _mm256_extractf128_si256(q8_1, 1));
  3837. __m128i p16_0 = _mm_maddubs_epi16(q4_0, _mm256_extractf128_si256(q8_0, 0));
  3838. __m128i p16_1 = _mm_maddubs_epi16(q4_1, _mm256_extractf128_si256(q8_0, 1));
  3839. __m128i p16_2 = _mm_maddubs_epi16(q4_2, _mm256_extractf128_si256(q8_1, 0));
  3840. __m128i p16_3 = _mm_maddubs_epi16(q4_3, _mm256_extractf128_si256(q8_1, 1));
  3841. p16_0 = _mm_sub_epi16(p16_0, q8s_0);
  3842. p16_1 = _mm_sub_epi16(p16_1, q8s_1);
  3843. p16_2 = _mm_sub_epi16(p16_2, q8s_2);
  3844. p16_3 = _mm_sub_epi16(p16_3, q8s_3);
  3845. p16_0 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_0), p16_0);
  3846. p16_1 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_0, scale_0)), p16_1);
  3847. p16_2 = _mm_madd_epi16(_mm_cvtepi8_epi16(scale_1), p16_2);
  3848. p16_3 = _mm_madd_epi16(_mm_cvtepi8_epi16(_mm_unpackhi_epi64(scale_1, scale_1)), p16_3);
  3849. sumi_0 = _mm_add_epi32(sumi_0, _mm_add_epi32(p16_0, p16_2));
  3850. sumi_1 = _mm_add_epi32(sumi_1, _mm_add_epi32(p16_1, p16_3));
  3851. acc = _mm256_add_ps(_mm256_mul_ps(_mm256_broadcast_ss(&d), _mm256_cvtepi32_ps(MM256_SET_M128I(sumi_1, sumi_0))), acc);
  3852. }
  3853. *s = hsum_float_8(acc);
  3854. #elif defined __riscv_v_intrinsic
  3855. float sumf = 0;
  3856. for (int i = 0; i < nb; ++i) {
  3857. const float d_all = (float)x[i].d;
  3858. const uint8_t * restrict q6 = x[i].ql;
  3859. const uint8_t * restrict qh = x[i].qh;
  3860. const int8_t * restrict q8 = y[i].qs;
  3861. const int8_t * restrict scale = x[i].scales;
  3862. int32_t isum = 0;
  3863. size_t vl = 16;
  3864. vint32m1_t vzero = __riscv_vmv_v_x_i32m1(0, 1);
  3865. // load Q6
  3866. vuint8mf2_t q6_0 = __riscv_vle8_v_u8mf2(q6, vl);
  3867. vuint8mf2_t q6_1 = __riscv_vle8_v_u8mf2(q6+16, vl);
  3868. // load qh
  3869. vuint8mf2_t qh_x = __riscv_vle8_v_u8mf2(qh, vl);
  3870. vuint8mf2_t qh0 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  3871. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  3872. vuint8mf2_t qh1 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  3873. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  3874. vuint8mf2_t qh2 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  3875. qh_x = __riscv_vsrl_vx_u8mf2(qh_x, 0x2, vl);
  3876. vuint8mf2_t qh3 = __riscv_vsll_vx_u8mf2(__riscv_vand_vx_u8mf2(qh_x, 0x3, vl), 0x4, vl);
  3877. vuint8mf2_t q6h_0 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_0, 0xF, vl), qh0, vl);
  3878. vuint8mf2_t q6h_1 = __riscv_vor_vv_u8mf2(__riscv_vand_vx_u8mf2(q6_1, 0xF, vl), qh1, vl);
  3879. vuint8mf2_t q6h_2 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_0, 0x4, vl), qh2, vl);
  3880. vuint8mf2_t q6h_3 = __riscv_vor_vv_u8mf2(__riscv_vsrl_vx_u8mf2(q6_1, 0x4, vl), qh3, vl);
  3881. vint8mf2_t q6v_0 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_0), 32, vl);
  3882. vint8mf2_t q6v_1 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_1), 32, vl);
  3883. vint8mf2_t q6v_2 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_2), 32, vl);
  3884. vint8mf2_t q6v_3 = __riscv_vsub_vx_i8mf2(__riscv_vreinterpret_v_u8mf2_i8mf2(q6h_3), 32, vl);
  3885. // load Q8 and take product
  3886. vint16m1_t p0 = __riscv_vwmul_vv_i16m1(q6v_0, __riscv_vle8_v_i8mf2(q8, vl), vl);
  3887. vint16m1_t p1 = __riscv_vwmul_vv_i16m1(q6v_1, __riscv_vle8_v_i8mf2(q8+16, vl), vl);
  3888. vint16m1_t p2 = __riscv_vwmul_vv_i16m1(q6v_2, __riscv_vle8_v_i8mf2(q8+32, vl), vl);
  3889. vint16m1_t p3 = __riscv_vwmul_vv_i16m1(q6v_3, __riscv_vle8_v_i8mf2(q8+48, vl), vl);
  3890. vint32m1_t vs_0 = __riscv_vwredsum_vs_i16m1_i32m1(p0, vzero, vl);
  3891. vint32m1_t vs_1 = __riscv_vwredsum_vs_i16m1_i32m1(p1, vzero, vl);
  3892. vint32m1_t vs_2 = __riscv_vwredsum_vs_i16m1_i32m1(p2, vzero, vl);
  3893. vint32m1_t vs_3 = __riscv_vwredsum_vs_i16m1_i32m1(p3, vzero, vl);
  3894. isum += __riscv_vmv_x_s_i32m1_i32(vs_0) * scale[0];
  3895. isum += __riscv_vmv_x_s_i32m1_i32(vs_1) * scale[1];
  3896. isum += __riscv_vmv_x_s_i32m1_i32(vs_2) * scale[2];
  3897. isum += __riscv_vmv_x_s_i32m1_i32(vs_3) * scale[3];
  3898. sumf += isum * d_all * y[i].d;
  3899. }
  3900. *s = sumf;
  3901. #else
  3902. int8_t aux8[QK_K];
  3903. int16_t aux16[8];
  3904. float sums [8];
  3905. int32_t aux32[8];
  3906. memset(sums, 0, 8*sizeof(float));
  3907. float sumf = 0;
  3908. for (int i = 0; i < nb; ++i) {
  3909. const uint8_t * restrict q4 = x[i].ql;
  3910. const uint8_t * restrict qh = x[i].qh;
  3911. const int8_t * restrict q8 = y[i].qs;
  3912. memset(aux32, 0, 8*sizeof(int32_t));
  3913. int8_t * restrict a = aux8;
  3914. for (int l = 0; l < 16; ++l) {
  3915. a[l+ 0] = (int8_t)((q4[l+ 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32;
  3916. a[l+16] = (int8_t)((q4[l+16] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32;
  3917. a[l+32] = (int8_t)((q4[l+ 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32;
  3918. a[l+48] = (int8_t)((q4[l+16] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32;
  3919. }
  3920. int is = 0;
  3921. for (int j = 0; j < QK_K/16; ++j) {
  3922. int scale = x[i].scales[is++];
  3923. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3924. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  3925. q8 += 8; a += 8;
  3926. for (int l = 0; l < 8; ++l) aux16[l] = q8[l] * a[l];
  3927. for (int l = 0; l < 8; ++l) aux32[l] += scale * aux16[l];
  3928. q8 += 8; a += 8;
  3929. }
  3930. const float d = ggml_fp16_to_fp32(x[i].d) * y[i].d;
  3931. for (int l = 0; l < 8; ++l) sums[l] += d * aux32[l];
  3932. }
  3933. for (int l = 0; l < 8; ++l) sumf += sums[l];
  3934. *s = sumf;
  3935. #endif
  3936. }
  3937. #endif