k_quants.c 201 KB

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