mmq.cpp 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #if defined(__GNUC__)
  27. #pragma GCC diagnostic ignored "-Wpedantic"
  28. #pragma GCC diagnostic ignored "-Wunused-local-typedefs"
  29. #endif
  30. #include "amx.h"
  31. #include "mmq.h"
  32. #include "ggml-impl.h"
  33. #include "ggml-cpu-impl.h"
  34. #include "ggml-cpu-quants.h"
  35. #include "ggml-quants.h"
  36. #include <algorithm>
  37. #include <type_traits>
  38. #if defined(__gnu_linux__)
  39. #include <sys/syscall.h>
  40. #include <unistd.h>
  41. #endif
  42. #if (defined(_WIN32) || defined(_WIN64))
  43. #define RESTRICT __restrict
  44. #else
  45. #define RESTRICT __restrict__
  46. #endif
  47. #if (defined(_WIN32) || defined(_WIN64))
  48. #define ALWAYS_INLINE __forceinline
  49. #elif __has_attribute(always_inline) || defined(__GNUC__)
  50. #define ALWAYS_INLINE __attribute__((__always_inline__)) inline
  51. #else
  52. #define ALWAYS_INLINE inline
  53. #endif
  54. #if defined(__AMX_INT8__) && defined(__AVX512VNNI__)
  55. namespace {
  56. // Forced unrolling
  57. template <int n>
  58. struct Unroll {
  59. template <typename Func, typename... Args>
  60. ALWAYS_INLINE void operator()(const Func& f, Args... args) const {
  61. Unroll<n - 1>{}(f, args...);
  62. f(std::integral_constant<int, n - 1>{}, args...);
  63. }
  64. };
  65. template <>
  66. struct Unroll<1> {
  67. template <typename Func, typename... Args>
  68. ALWAYS_INLINE void operator()(const Func& f, Args... args) const {
  69. f(std::integral_constant<int, 0>{}, args...);
  70. }
  71. };
  72. // type traits
  73. template <typename T> struct PackedTypes {};
  74. template <> struct PackedTypes<block_q4_0> { using type = int8_t; };
  75. template <> struct PackedTypes<block_q4_1> { using type = uint8_t; };
  76. template <> struct PackedTypes<block_q8_0> { using type = int8_t; };
  77. template <typename T> using packed_B_type = typename PackedTypes<T>::type;
  78. template <typename T>
  79. struct do_compensate : std::integral_constant<bool,
  80. std::is_same<T, block_q8_0>::value> {};
  81. template <typename T>
  82. struct do_unpack : std::integral_constant<bool,
  83. std::is_same<T, block_q4_0>::value ||
  84. std::is_same<T, block_q4_1>::value> {};
  85. template <typename T>
  86. struct is_type_qkk : std::integral_constant<bool,
  87. std::is_same<T, block_q4_K>::value ||
  88. std::is_same<T, block_q5_K>::value ||
  89. std::is_same<T, block_q6_K>::value ||
  90. std::is_same<T, block_iq4_xs>::value> {};
  91. #define GGML_DISPATCH_FLOATING_TYPES(TYPE, ...) \
  92. [&] { \
  93. switch (TYPE) { \
  94. case GGML_TYPE_F16: { \
  95. using type = ggml_fp16_t; \
  96. constexpr int blck_size = 16; \
  97. return __VA_ARGS__(); \
  98. } \
  99. case GGML_TYPE_BF16: { \
  100. using type = ggml_bf16_t; \
  101. constexpr int blck_size = 32; \
  102. return __VA_ARGS__(); \
  103. } \
  104. default: \
  105. fprintf(stderr, "Unsupported floating data type\n"); \
  106. } \
  107. }()
  108. #define GGML_DISPATCH_QTYPES(QT, ...) \
  109. [&] { \
  110. switch (QT) { \
  111. case GGML_TYPE_Q4_0: { \
  112. using type = block_q4_0; \
  113. using vec_dot_type = block_q8_0; \
  114. constexpr int blck_size = QK4_0; \
  115. return __VA_ARGS__(); \
  116. } \
  117. case GGML_TYPE_Q4_1: { \
  118. using type = block_q4_1; \
  119. using vec_dot_type = block_q8_1; \
  120. constexpr int blck_size = QK4_1; \
  121. return __VA_ARGS__(); \
  122. } \
  123. case GGML_TYPE_Q8_0: { \
  124. using type = block_q8_0; \
  125. using vec_dot_type = block_q8_0; \
  126. constexpr int blck_size = QK8_0; \
  127. return __VA_ARGS__(); \
  128. } \
  129. case GGML_TYPE_Q4_K: { \
  130. using type = block_q4_K; \
  131. using vec_dot_type = block_q8_K; \
  132. constexpr int blck_size = QK_K; \
  133. return __VA_ARGS__(); \
  134. } \
  135. case GGML_TYPE_Q5_K: { \
  136. using type = block_q5_K; \
  137. using vec_dot_type = block_q8_K; \
  138. constexpr int blck_size = QK_K; \
  139. return __VA_ARGS__(); \
  140. } \
  141. case GGML_TYPE_Q6_K: { \
  142. using type = block_q6_K; \
  143. using vec_dot_type = block_q8_K; \
  144. constexpr int blck_size = QK_K; \
  145. return __VA_ARGS__(); \
  146. } \
  147. case GGML_TYPE_IQ4_XS: { \
  148. using type = block_iq4_xs; \
  149. using vec_dot_type = block_q8_K; \
  150. constexpr int blck_size = QK_K; \
  151. return __VA_ARGS__(); \
  152. } \
  153. default: \
  154. fprintf(stderr, "Unsupported quantized data type: %d\n", int(TYPE)); \
  155. } \
  156. }()
  157. #define GGML_DISPATCH_BOOL(BOOL_V, BOOL_NAME, ...) \
  158. [&] { \
  159. if (BOOL_V) { \
  160. constexpr bool BOOL_NAME = true; \
  161. return __VA_ARGS__(); \
  162. } else { \
  163. constexpr bool BOOL_NAME = false; \
  164. return __VA_ARGS__(); \
  165. } \
  166. }()
  167. // define amx tile config data structure
  168. struct tile_config_t{
  169. uint8_t palette_id = 0;
  170. uint8_t start_row = 0;
  171. uint8_t reserved_0[14] = {0};
  172. uint16_t colsb[16] = {0};
  173. uint8_t rows[16] = {0};
  174. };
  175. // Notes: amx tile config
  176. //
  177. // Typically, TMUL calculates A and B of size 16 x 64 containing INT8 values,
  178. // and accumulate the result to a 16 x 16 matrix C containing INT32 values,
  179. //
  180. // As many GGUF quantized types as `block_size` of 32, so a 16-16-32 config is used
  181. // instead of the normally used 16-16-64 config.
  182. //
  183. // Block A: {16, 32}, dtype = int8_t
  184. // Block B: {16, 32}, dtype = uint8_t/int8_t
  185. // Block C: {16, 16}, dtype = int32_t
  186. //
  187. // Block B needs to be prepacked to vnni format before feeding into TMUL:
  188. // packed_B: from {n, k} to {k/vnni_blk, n, vnni_blck}, viewed in 2d, we get {8, 64}
  189. //
  190. // Therefore, we get tileconfig:
  191. // A B C
  192. // rows 16 8 16
  193. // colsb 32 64 16
  194. //
  195. // For tile distribution, follow a 2-2-4 pattern, e.g. A used TMM2-TMM3, B used TMM0-TMM1,
  196. // C used TMM4-TMM7:
  197. // B TMM0 B TMM1
  198. // A TMM2 C TMM4 C TMM6
  199. // A TMM3 C TMM5 C TMM7
  200. //
  201. // Each `amx` kernel handles 4 blocks at a time: 2MB * 2NB, when m < 2 * BLOCK_M, unpack A
  202. // will be needed.
  203. //
  204. // Here another commonly used pattern 1-3-3 is skipped, as it is mostly used when m <=16;
  205. // and the sinlge batch gemm (m=1) has a special fast path with `avx512-vnni`.
  206. //
  207. // ref: https://www.intel.com/content/www/us/en/developer/articles/code-sample/
  208. // advanced-matrix-extensions-intrinsics-functions.html
  209. //
  210. #define TC_CONFIG_TILE(i, r, cb) tc.rows[i] = r; tc.colsb[i] = cb
  211. void ggml_tile_config_init(void) {
  212. static thread_local bool is_first_time = true;
  213. if (!is_first_time) {
  214. return;
  215. }
  216. static thread_local tile_config_t tc;
  217. tile_config_t current_tc;
  218. _tile_storeconfig(&current_tc);
  219. // load only when config changes
  220. if (tc.palette_id == 0 || (memcmp(&current_tc.colsb, &tc.colsb, sizeof(uint16_t) * 8) != 0 &&
  221. memcmp(&current_tc.rows, &tc.rows, sizeof(uint8_t) * 8) != 0)) {
  222. tc.palette_id = 1;
  223. tc.start_row = 0;
  224. TC_CONFIG_TILE(TMM0, 8, 64);
  225. TC_CONFIG_TILE(TMM1, 8, 64);
  226. TC_CONFIG_TILE(TMM2, 16, 32);
  227. TC_CONFIG_TILE(TMM3, 16, 32);
  228. TC_CONFIG_TILE(TMM4, 16, 64);
  229. TC_CONFIG_TILE(TMM5, 16, 64);
  230. TC_CONFIG_TILE(TMM6, 16, 64);
  231. TC_CONFIG_TILE(TMM7, 16, 64);
  232. _tile_loadconfig(&tc);
  233. }
  234. is_first_time = false;
  235. }
  236. // we need an extra 16 * 4B (TILE_N * int32_t) for each NB/KB block for compensation.
  237. // See the notes `s8s8 igemm compensation in avx512-vnni` for detail.
  238. template <typename TB>
  239. int get_tile_size() {
  240. int tile_size = TILE_N * sizeof(TB);
  241. if (do_compensate<TB>::value) {
  242. tile_size += TILE_N * sizeof(int32_t);
  243. }
  244. if (std::is_same<TB, block_q4_K>::value ||
  245. std::is_same<TB, block_q5_K>::value) {
  246. tile_size += TILE_N * 4;
  247. }
  248. if (std::is_same<TB, block_iq4_xs>::value) {
  249. tile_size += TILE_N * 2;
  250. }
  251. return tile_size;
  252. }
  253. template <typename TB, int BLOCK_K>
  254. int get_row_size(int K) {
  255. int KB = K / BLOCK_K;
  256. int row_size = KB * sizeof(TB);
  257. if (do_compensate<TB>::value) {
  258. row_size += KB * sizeof(int32_t);
  259. }
  260. if (std::is_same<TB, block_q4_K>::value ||
  261. std::is_same<TB, block_q5_K>::value) {
  262. row_size += KB * 4;
  263. }
  264. if (std::is_same<TB, block_iq4_xs>::value) {
  265. row_size += KB * 2;
  266. }
  267. return row_size;
  268. }
  269. // vectorized dtype conversion
  270. inline float FP16_TO_FP32(ggml_half val) {
  271. __m256i v = _mm256_setr_epi16(
  272. val, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  273. __m512 o = _mm512_cvtph_ps(v);
  274. return _mm512_cvtss_f32(o);
  275. }
  276. inline __m512 FP16_TO_FP32_VEC(ggml_half val) {
  277. __m256i v = _mm256_set1_epi16(val);
  278. return _mm512_cvtph_ps(v);
  279. }
  280. // horizontal reduce
  281. inline float _mm512_reduce_max_ps(const __m512 x) {
  282. __m512 v = x;
  283. __m512 v1 = _mm512_shuffle_f32x4(v, v, 0x4E);
  284. v = _mm512_max_ps(v, v1);
  285. v1 = _mm512_shuffle_f32x4(v, v, 0xB1);
  286. v = _mm512_max_ps(v, v1);
  287. v1 = _mm512_shuffle_ps(v, v, 0x4E);
  288. v = _mm512_max_ps(v, v1);
  289. v1 = _mm512_shuffle_ps(v, v, 0xB1);
  290. v = _mm512_max_ps(v, v1);
  291. return _mm512_cvtss_f32(v);
  292. }
  293. // transpose utils
  294. #define SHUFFLE_EPI32(a, b, mask) \
  295. _mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), mask))
  296. inline void transpose_8x8_32bit(__m256i * v, __m256i * v1) {
  297. // unpacking and 32-bit elements
  298. v1[0] = _mm256_unpacklo_epi32(v[0], v[1]);
  299. v1[1] = _mm256_unpackhi_epi32(v[0], v[1]);
  300. v1[2] = _mm256_unpacklo_epi32(v[2], v[3]);
  301. v1[3] = _mm256_unpackhi_epi32(v[2], v[3]);
  302. v1[4] = _mm256_unpacklo_epi32(v[4], v[5]);
  303. v1[5] = _mm256_unpackhi_epi32(v[4], v[5]);
  304. v1[6] = _mm256_unpacklo_epi32(v[6], v[7]);
  305. v1[7] = _mm256_unpackhi_epi32(v[6], v[7]);
  306. // shuffling the 32-bit elements
  307. v[0] = SHUFFLE_EPI32(v1[0], v1[2], 0x44);
  308. v[1] = SHUFFLE_EPI32(v1[0], v1[2], 0xee);
  309. v[2] = SHUFFLE_EPI32(v1[4], v1[6], 0x44);
  310. v[3] = SHUFFLE_EPI32(v1[4], v1[6], 0xee);
  311. v[4] = SHUFFLE_EPI32(v1[1], v1[3], 0x44);
  312. v[5] = SHUFFLE_EPI32(v1[1], v1[3], 0xee);
  313. v[6] = SHUFFLE_EPI32(v1[5], v1[7], 0x44);
  314. v[7] = SHUFFLE_EPI32(v1[5], v1[7], 0xee);
  315. // shuffling 128-bit elements
  316. v1[0] = _mm256_permute2f128_si256(v[2], v[0], 0x02);
  317. v1[1] = _mm256_permute2f128_si256(v[3], v[1], 0x02);
  318. v1[2] = _mm256_permute2f128_si256(v[6], v[4], 0x02);
  319. v1[3] = _mm256_permute2f128_si256(v[7], v[5], 0x02);
  320. v1[4] = _mm256_permute2f128_si256(v[2], v[0], 0x13);
  321. v1[5] = _mm256_permute2f128_si256(v[3], v[1], 0x13);
  322. v1[6] = _mm256_permute2f128_si256(v[6], v[4], 0x13);
  323. v1[7] = _mm256_permute2f128_si256(v[7], v[5], 0x13);
  324. }
  325. inline void transpose_16x4_32bit(__m512i * r, __m512i * d) {
  326. static const __m512i index1 = _mm512_set_epi32(
  327. 0x0f, 0x0b, 0x07, 0x03,
  328. 0x0e, 0x0a, 0x06, 0x02,
  329. 0x0d, 0x09, 0x05, 0x01,
  330. 0x0c, 0x08, 0x04, 0x00);
  331. d[0] = _mm512_permutexvar_epi32(index1, r[0]);
  332. d[1] = _mm512_permutexvar_epi32(index1, r[1]);
  333. d[2] = _mm512_permutexvar_epi32(index1, r[2]);
  334. d[3] = _mm512_permutexvar_epi32(index1, r[3]);
  335. r[0] = _mm512_shuffle_i32x4(d[0], d[1], 0x44);
  336. r[1] = _mm512_shuffle_i32x4(d[0], d[1], 0xee);
  337. r[2] = _mm512_shuffle_i32x4(d[2], d[3], 0x44);
  338. r[3] = _mm512_shuffle_i32x4(d[2], d[3], 0xee);
  339. d[0] = _mm512_shuffle_i32x4(r[0], r[2], 0x88);
  340. d[1] = _mm512_shuffle_i32x4(r[0], r[2], 0xdd);
  341. d[2] = _mm512_shuffle_i32x4(r[1], r[3], 0x88);
  342. d[3] = _mm512_shuffle_i32x4(r[1], r[3], 0xdd);
  343. }
  344. inline void transpose_16x16_32bit(__m512i * v) {
  345. __m512i v1[16];
  346. v1[0] = _mm512_unpacklo_epi32(v[0], v[1]);
  347. v1[1] = _mm512_unpackhi_epi32(v[0], v[1]);
  348. v1[2] = _mm512_unpacklo_epi32(v[2], v[3]);
  349. v1[3] = _mm512_unpackhi_epi32(v[2], v[3]);
  350. v1[4] = _mm512_unpacklo_epi32(v[4], v[5]);
  351. v1[5] = _mm512_unpackhi_epi32(v[4], v[5]);
  352. v1[6] = _mm512_unpacklo_epi32(v[6], v[7]);
  353. v1[7] = _mm512_unpackhi_epi32(v[6], v[7]);
  354. v1[8] = _mm512_unpacklo_epi32(v[8], v[9]);
  355. v1[9] = _mm512_unpackhi_epi32(v[8], v[9]);
  356. v1[10] = _mm512_unpacklo_epi32(v[10], v[11]);
  357. v1[11] = _mm512_unpackhi_epi32(v[10], v[11]);
  358. v1[12] = _mm512_unpacklo_epi32(v[12], v[13]);
  359. v1[13] = _mm512_unpackhi_epi32(v[12], v[13]);
  360. v1[14] = _mm512_unpacklo_epi32(v[14], v[15]);
  361. v1[15] = _mm512_unpackhi_epi32(v[14], v[15]);
  362. v[0] = _mm512_unpacklo_epi64(v1[0], v1[2]);
  363. v[1] = _mm512_unpackhi_epi64(v1[0], v1[2]);
  364. v[2] = _mm512_unpacklo_epi64(v1[1], v1[3]);
  365. v[3] = _mm512_unpackhi_epi64(v1[1], v1[3]);
  366. v[4] = _mm512_unpacklo_epi64(v1[4], v1[6]);
  367. v[5] = _mm512_unpackhi_epi64(v1[4], v1[6]);
  368. v[6] = _mm512_unpacklo_epi64(v1[5], v1[7]);
  369. v[7] = _mm512_unpackhi_epi64(v1[5], v1[7]);
  370. v[8] = _mm512_unpacklo_epi64(v1[8], v1[10]);
  371. v[9] = _mm512_unpackhi_epi64(v1[8], v1[10]);
  372. v[10] = _mm512_unpacklo_epi64(v1[9], v1[11]);
  373. v[11] = _mm512_unpackhi_epi64(v1[9], v1[11]);
  374. v[12] = _mm512_unpacklo_epi64(v1[12], v1[14]);
  375. v[13] = _mm512_unpackhi_epi64(v1[12], v1[14]);
  376. v[14] = _mm512_unpacklo_epi64(v1[13], v1[15]);
  377. v[15] = _mm512_unpackhi_epi64(v1[13], v1[15]);
  378. v1[0] = _mm512_shuffle_i32x4(v[0], v[4], 0x88);
  379. v1[1] = _mm512_shuffle_i32x4(v[1], v[5], 0x88);
  380. v1[2] = _mm512_shuffle_i32x4(v[2], v[6], 0x88);
  381. v1[3] = _mm512_shuffle_i32x4(v[3], v[7], 0x88);
  382. v1[4] = _mm512_shuffle_i32x4(v[0], v[4], 0xdd);
  383. v1[5] = _mm512_shuffle_i32x4(v[1], v[5], 0xdd);
  384. v1[6] = _mm512_shuffle_i32x4(v[2], v[6], 0xdd);
  385. v1[7] = _mm512_shuffle_i32x4(v[3], v[7], 0xdd);
  386. v1[8] = _mm512_shuffle_i32x4(v[8], v[12], 0x88);
  387. v1[9] = _mm512_shuffle_i32x4(v[9], v[13], 0x88);
  388. v1[10] = _mm512_shuffle_i32x4(v[10], v[14], 0x88);
  389. v1[11] = _mm512_shuffle_i32x4(v[11], v[15], 0x88);
  390. v1[12] = _mm512_shuffle_i32x4(v[8], v[12], 0xdd);
  391. v1[13] = _mm512_shuffle_i32x4(v[9], v[13], 0xdd);
  392. v1[14] = _mm512_shuffle_i32x4(v[10], v[14], 0xdd);
  393. v1[15] = _mm512_shuffle_i32x4(v[11], v[15], 0xdd);
  394. v[0] = _mm512_shuffle_i32x4(v1[0], v1[8], 0x88);
  395. v[1] = _mm512_shuffle_i32x4(v1[1], v1[9], 0x88);
  396. v[2] = _mm512_shuffle_i32x4(v1[2], v1[10], 0x88);
  397. v[3] = _mm512_shuffle_i32x4(v1[3], v1[11], 0x88);
  398. v[4] = _mm512_shuffle_i32x4(v1[4], v1[12], 0x88);
  399. v[5] = _mm512_shuffle_i32x4(v1[5], v1[13], 0x88);
  400. v[6] = _mm512_shuffle_i32x4(v1[6], v1[14], 0x88);
  401. v[7] = _mm512_shuffle_i32x4(v1[7], v1[15], 0x88);
  402. v[8] = _mm512_shuffle_i32x4(v1[0], v1[8], 0xdd);
  403. v[9] = _mm512_shuffle_i32x4(v1[1], v1[9], 0xdd);
  404. v[10] = _mm512_shuffle_i32x4(v1[2], v1[10], 0xdd);
  405. v[11] = _mm512_shuffle_i32x4(v1[3], v1[11], 0xdd);
  406. v[12] = _mm512_shuffle_i32x4(v1[4], v1[12], 0xdd);
  407. v[13] = _mm512_shuffle_i32x4(v1[5], v1[13], 0xdd);
  408. v[14] = _mm512_shuffle_i32x4(v1[6], v1[14], 0xdd);
  409. v[15] = _mm512_shuffle_i32x4(v1[7], v1[15], 0xdd);
  410. }
  411. void quantize_row_q8_K_vnni(const float * RESTRICT x, void * RESTRICT vy, int64_t k) {
  412. assert(k % QK_K == 0);
  413. const int KB = k / QK_K;
  414. constexpr int kVecs = QK_K / 16;
  415. block_q8_K * y = reinterpret_cast<block_q8_K *>(vy);
  416. // hold 16 float vecs from x
  417. __m512 v[kVecs];
  418. // hold the quants vecs
  419. __m512i vq[kVecs / 4];
  420. // hold the packed quants vecs
  421. __m512i vq_packed[kVecs / 4];
  422. const __m512 signBit = _mm512_set1_ps(-0.f);
  423. for (int i = 0; i < KB; ++i) {
  424. // Compute max(abs(e)) for the block
  425. __m512 vamax = _mm512_set1_ps(0.f);
  426. for (int j = 0; j < kVecs; ++j) {
  427. v[j] = _mm512_loadu_ps(x); x += 16;
  428. vamax = _mm512_max_ps(vamax, _mm512_andnot_ps(signBit, v[j]));
  429. }
  430. const float amax = _mm512_reduce_max_ps(vamax);
  431. // Quantize these floats
  432. const float iscale = 127.f / amax;
  433. y[i].d = GGML_FP32_TO_FP16(1 / iscale);
  434. const float id = ( amax != 0.0f ) ? iscale : 0.f;
  435. const __m512 vscale = _mm512_set1_ps(id);
  436. // Apply multiplier and round to nearest integer
  437. for (int j = 0; j < kVecs; ++j) {
  438. v[j] = _mm512_mul_ps(v[j], vscale);
  439. v[j] = _mm512_roundscale_ps(v[j], (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
  440. }
  441. // Pack to epi8 vecs
  442. for (int j = 0; j < kVecs / 4; ++j) {
  443. __m128i q8_0 = _mm512_cvtepi32_epi8(_mm512_cvtps_epi32(v[j * 4 + 0]));
  444. __m128i q8_1 = _mm512_cvtepi32_epi8(_mm512_cvtps_epi32(v[j * 4 + 1]));
  445. __m128i q8_2 = _mm512_cvtepi32_epi8(_mm512_cvtps_epi32(v[j * 4 + 2]));
  446. __m128i q8_3 = _mm512_cvtepi32_epi8(_mm512_cvtps_epi32(v[j * 4 + 3]));
  447. __m256i q8_01 = _mm256_insertf128_si256(_mm256_castsi128_si256(q8_0), (q8_1), 1);
  448. __m256i q8_23 = _mm256_insertf128_si256(_mm256_castsi128_si256(q8_2), (q8_3), 1);
  449. vq[j] = _mm512_inserti32x8(_mm512_castsi256_si512(q8_01), q8_23, 1);
  450. _mm512_storeu_si512((__m512i *)(y[i].qs + j * 64), vq[j]);
  451. }
  452. // Compute the bsums with vnni
  453. transpose_16x4_32bit(vq, vq_packed);
  454. const __m512i one = _mm512_set1_epi8(1);
  455. __m512i sum = _mm512_setzero_si512();
  456. for (int k = 0; k < 4; ++k) {
  457. sum = _mm512_dpbusd_epi32(sum, one, vq_packed[k]);
  458. }
  459. _mm256_storeu_si256((__m256i *)(y[i].bsums), _mm512_cvtepi32_epi16(sum));
  460. }
  461. }
  462. // quantize A from float to `vec_dot_type`
  463. template <typename T>
  464. inline void from_float(const float * x, char * vy, int64_t k);
  465. template <>
  466. inline void from_float<block_q8_0>(const float * x, char * vy, int64_t k) {
  467. quantize_row_q8_0(x, (block_q8_0 *)vy, k);
  468. }
  469. template <>
  470. inline void from_float<block_q8_1>(const float * x, char * vy, int64_t k) {
  471. quantize_row_q8_1(x, (block_q8_1 *)vy, k);
  472. }
  473. template <>
  474. inline void from_float<block_q8_K>(const float * x, char * vy, int64_t k) {
  475. #if 1
  476. // TODO: this is reference impl!
  477. quantize_row_q8_K_ref(x, (block_q8_K *)vy, k);
  478. #else
  479. quantize_row_q8_K_vnni(x, vy, k);
  480. #endif
  481. }
  482. // load A from memory to array when nrows can not fill in whole tile
  483. void unpack_A(int8_t * RESTRICT tile, const block_q8_0 * RESTRICT A, int lda, int nr) {
  484. assert(nr != TILE_M);
  485. for (int m = 0; m < nr; ++m) {
  486. const __m256i v = _mm256_loadu_si256((const __m256i *)(A[m * lda].qs));
  487. _mm256_storeu_si256((__m256i *)(tile + m * TILE_K), v);
  488. }
  489. }
  490. void unpack_A(int8_t * RESTRICT tile, const block_q8_1 * RESTRICT A, int lda, int nr) {
  491. assert(nr != TILE_M);
  492. for (int m = 0; m < nr; ++m) {
  493. const __m256i v = _mm256_loadu_si256((const __m256i *)(A[m * lda].qs));
  494. _mm256_storeu_si256((__m256i *)(tile + m * TILE_K), v);
  495. }
  496. }
  497. template <typename TB>
  498. void unpack_A(int8_t * RESTRICT tile, const block_q8_K * RESTRICT A, int lda, int k, int nr) {
  499. assert(nr <= TILE_M);
  500. for (int m = 0; m < nr; ++m) {
  501. const __m256i v = _mm256_loadu_si256((const __m256i *)(A[m * lda].qs + k * 32));
  502. _mm256_storeu_si256((__m256i *)(tile + m * TILE_K), v);
  503. }
  504. }
  505. template <>
  506. void unpack_A<block_q6_K>(int8_t * RESTRICT tile, const block_q8_K * RESTRICT A, int lda, int k, int nr) {
  507. assert(nr <= TILE_M);
  508. // zero padding k from 16 to 32, so that we don't have to re-config amx
  509. const __m128i zero = _mm_setzero_si128();
  510. for (int m = 0; m < nr; ++m) {
  511. const __m128i v = _mm_loadu_si128((const __m128i *)(A[m * lda].qs + k * 16));
  512. const __m256i r = _mm256_insertf128_si256(_mm256_castsi128_si256(v), zero, 1);
  513. _mm256_storeu_si256((__m256i *)(tile + m * TILE_K), r);
  514. }
  515. }
  516. #define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
  517. inline __m256i bytes_from_nibbles_32(const uint8_t * rsi) {
  518. const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
  519. const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
  520. const __m256i lowMask = _mm256_set1_epi8(0xF);
  521. return _mm256_and_si256(lowMask, bytes);
  522. }
  523. // used for block_q4_K
  524. inline __m512i bytes_from_nibbles_64(const uint8_t * rsi) {
  525. const __m256i tmp = _mm256_loadu_si256((const __m256i *)rsi);
  526. const __m256i lowMask = _mm256_set1_epi8(0xF);
  527. const __m256i q4l = _mm256_and_si256(tmp, lowMask);
  528. const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(tmp, 4), lowMask);
  529. return _mm512_inserti32x8(_mm512_castsi256_si512(q4l), q4h, 1);
  530. }
  531. // used for block_q5_K
  532. inline __m512i bytes_from_nibbles_64(const uint8_t * qs, const uint8_t * qh, int k) {
  533. const __m256i lowMask = _mm256_set1_epi8(0xF);
  534. __m256i hmask = _mm256_set1_epi8(1);
  535. hmask = _mm256_slli_epi16(hmask, k);
  536. const __m256i q5bits = _mm256_loadu_si256((const __m256i *)qs);
  537. const __m256i hbits = _mm256_loadu_si256((const __m256i *)qh);
  538. const __m256i q5l_0 = _mm256_and_si256(q5bits, lowMask);
  539. const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), k + 0), 4);
  540. const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
  541. hmask = _mm256_slli_epi16(hmask, 1);
  542. const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), lowMask);
  543. const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), k + 1), 4);
  544. const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
  545. return _mm512_inserti32x8(_mm512_castsi256_si512(q5_0), q5_1, 1);
  546. }
  547. // used for block_q6_K
  548. inline void bytes_from_nibbles_128(__m512i& r0, __m512i& r1, const uint8_t * qs, const uint8_t * qh) {
  549. const __m256i m4 = _mm256_set1_epi8(0xF);
  550. const __m256i m2 = _mm256_set1_epi8(0x3);
  551. const __m256i q6bits1 = _mm256_loadu_si256((const __m256i *)qs);
  552. const __m256i q6bits2 = _mm256_loadu_si256((const __m256i *)(qs + 32));
  553. const __m256i q6bitsH = _mm256_loadu_si256((const __m256i *)qh);
  554. const __m256i q6h_0 = _mm256_slli_epi16(_mm256_and_si256( q6bitsH, m2), 4);
  555. const __m256i q6h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q6bitsH, 2), m2), 4);
  556. const __m256i q6h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q6bitsH, 4), m2), 4);
  557. const __m256i q6h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q6bitsH, 6), m2), 4);
  558. const __m256i q6_0 = _mm256_or_si256(_mm256_and_si256(q6bits1, m4), q6h_0);
  559. const __m256i q6_1 = _mm256_or_si256(_mm256_and_si256(q6bits2, m4), q6h_1);
  560. const __m256i q6_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q6bits1, 4), m4), q6h_2);
  561. const __m256i q6_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q6bits2, 4), m4), q6h_3);
  562. r0 = _mm512_inserti32x8(_mm512_castsi256_si512(q6_0), q6_1, 1);
  563. r1 = _mm512_inserti32x8(_mm512_castsi256_si512(q6_2), q6_3, 1);
  564. }
  565. inline __m512i packNibbles(__m512i r0, __m512i r1) {
  566. return _mm512_or_si512(r0, _mm512_slli_epi16(r1, 4));
  567. }
  568. template <typename TB>
  569. inline void pack_qs(void * RESTRICT packed_B, const TB * RESTRICT B, int KB) {
  570. int8_t tmp[8 * 64];
  571. __m256i v[8], v2[8];
  572. for (int n = 0; n < 8; ++n) {
  573. v[n] = bytes_from_nibbles_32(B[n * KB].qs);
  574. }
  575. transpose_8x8_32bit(v, v2);
  576. for (int n = 0; n < 8; ++n) {
  577. _mm256_storeu_si256((__m256i *)(tmp + n * 64), v2[n]);
  578. }
  579. for (int n = 0; n < 8; ++n) {
  580. v[n] = bytes_from_nibbles_32(B[(n + 8) * KB].qs);
  581. }
  582. transpose_8x8_32bit(v, v2);
  583. for (int n = 0; n < 8; ++n) {
  584. _mm256_storeu_si256((__m256i *)(tmp + n * 64 + 32), v2[n]);
  585. }
  586. // pack again with 128 to fully utilize vector length
  587. for (int n = 0; n < 8; n += 2) {
  588. __m512i r0 = _mm512_loadu_si512((const __m512i *)(tmp + n * 64));
  589. __m512i r1 = _mm512_loadu_si512((const __m512i *)(tmp + n * 64 + 64));
  590. __m512i r1r0 = packNibbles(r0, r1);
  591. _mm512_storeu_si512((__m512i *)((char *)packed_B + n * 32), r1r0);
  592. }
  593. }
  594. template <>
  595. inline void pack_qs<block_q8_0>(void * RESTRICT packed_B, const block_q8_0 * RESTRICT B, int KB) {
  596. __m256i v[8], v2[8];
  597. for (int n = 0; n < 8; ++n) {
  598. v[n] = _mm256_loadu_si256((const __m256i *)(B[n * KB].qs));
  599. }
  600. transpose_8x8_32bit(v, v2);
  601. for (int n = 0; n < 8; ++n) {
  602. _mm256_storeu_si256((__m256i *)((char *)packed_B + n * 64), v2[n]);
  603. }
  604. for (int n = 0; n < 8; ++n) {
  605. v[n] = _mm256_loadu_si256((const __m256i *)(B[(n + 8) * KB].qs));
  606. }
  607. transpose_8x8_32bit(v, v2);
  608. for (int n = 0; n < 8; ++n) {
  609. _mm256_storeu_si256((__m256i *)((char *)packed_B + n * 64 + 32), v2[n]);
  610. }
  611. }
  612. template <>
  613. inline void pack_qs<block_q4_K>(void * RESTRICT packed_B, const block_q4_K * RESTRICT B, int KB) {
  614. __m512i v[16];
  615. // QK_K 256 with 8 groups, handle 2 groups at a time
  616. char * pb = (char *)packed_B;
  617. for (int k = 0; k < QK_K / 64; ++k) {
  618. // pack 2 groups { n, g, k} to {g, k/4, 4n}
  619. // e.g. {16, 2, 32} to {2, 8, 64}
  620. for (int n = 0; n < TILE_N; ++n) {
  621. v[n] = bytes_from_nibbles_64(B[n * KB].qs + k * 32);
  622. }
  623. transpose_16x16_32bit(v);
  624. // pack again with 128 to fully utilize vector length
  625. for (int n = 0; n < TILE_N; n += 2) {
  626. _mm512_storeu_si512((__m512i *)pb, packNibbles(v[n], v[n + 1]));
  627. pb += 64;
  628. }
  629. }
  630. }
  631. template <>
  632. inline void pack_qs<block_q5_K>(void * RESTRICT packed_B, const block_q5_K * RESTRICT B, int KB) {
  633. __m512i v[16];
  634. const __m512i lowMask = _mm512_set1_epi8(0xF);
  635. // QK_K 256 with 8 groups, handle 2 groups at a time
  636. char * pb = (char *)packed_B;
  637. char * ph = (char *)packed_B + (QK_K / 2) * TILE_N;
  638. for (int k = 0; k < QK_K / 64; ++k) {
  639. // pack 2 groups { n, g, k} to {g, k/4, 4n}
  640. // e.g. {16, 2, 32} to {2, 8, 64}
  641. for (int n = 0; n < TILE_N; ++n) {
  642. v[n] = bytes_from_nibbles_64(B[n * KB].qs + k * 32, B[n * KB].qh, /* group */2 * k);
  643. }
  644. transpose_16x16_32bit(v);
  645. // 1. pack lower 4bits with 2 groups
  646. for (int n = 0; n < TILE_N; n += 2) {
  647. // get lower 4 bits
  648. const __m512i r0 = _mm512_and_si512(v[n], lowMask);
  649. const __m512i r1 = _mm512_and_si512(v[n + 1], lowMask);
  650. _mm512_storeu_si512((__m512i *)pb, packNibbles(r0, r1)); pb += 64;
  651. }
  652. // 2. pack higher 1bit with 2 groups
  653. const __m512i hmask = _mm512_set1_epi8(0x10);
  654. for (int g = 0; g < 2; ++g) {
  655. __m512i hbits = _mm512_setzero_si512();
  656. hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 8 + 0], hmask), 4));
  657. hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 8 + 1], hmask), 3));
  658. hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 8 + 2], hmask), 2));
  659. hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 8 + 3], hmask), 1));
  660. hbits = _mm512_add_epi8(hbits, _mm512_and_si512(v[g * 8 + 4], hmask) );
  661. hbits = _mm512_add_epi8(hbits, _mm512_slli_epi16(_mm512_and_si512(v[g * 8 + 5], hmask), 1));
  662. hbits = _mm512_add_epi8(hbits, _mm512_slli_epi16(_mm512_and_si512(v[g * 8 + 6], hmask), 2));
  663. hbits = _mm512_add_epi8(hbits, _mm512_slli_epi16(_mm512_and_si512(v[g * 8 + 7], hmask), 3));
  664. _mm512_storeu_si512((__m512i *)ph, hbits); ph += 64;
  665. }
  666. }
  667. }
  668. template <>
  669. inline void pack_qs<block_q6_K>(void * RESTRICT packed_B, const block_q6_K * RESTRICT B, int KB) {
  670. __m512i v[32];
  671. const __m512i lowMask = _mm512_set1_epi8(0xF);
  672. // QK_K 256 with 8 groups, handle 4 groups at a time
  673. char * pb = (char *)packed_B;
  674. char * ph = (char *)packed_B + (QK_K / 2) * TILE_N;
  675. for (int k = 0; k < QK_K / 128; ++k) {
  676. for (int n = 0; n < TILE_N; ++n) {
  677. bytes_from_nibbles_128(v[n], v[n + 16], B[n * KB].ql + k * 64, B[n * KB].qh + k * 32);
  678. }
  679. // top half: group 0,1 or 4,5; bottom half: group 2,3 or 6,7
  680. transpose_16x16_32bit(v);
  681. transpose_16x16_32bit(v + 16);
  682. // 1. pack lower 4bits with 4 groups
  683. for (int n = 0; n < 32; n += 2) {
  684. const __m512i r0 = _mm512_and_si512(v[n], lowMask);
  685. const __m512i r1 = _mm512_and_si512(v[n + 1], lowMask);
  686. _mm512_storeu_si512((__m512i *)pb, packNibbles(r0, r1)); pb += 64;
  687. }
  688. // 2. pack higher 2bit with 4 groups
  689. const __m512i hmask = _mm512_set1_epi8(0x30);
  690. for (int g = 0; g < 8; ++g) {
  691. __m512i hbits = _mm512_setzero_si512();
  692. hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 4 + 0], hmask), 4));
  693. hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 4 + 1], hmask), 2));
  694. hbits = _mm512_add_epi8(hbits, _mm512_and_si512(v[g * 4 + 2], hmask) );
  695. hbits = _mm512_add_epi8(hbits, _mm512_slli_epi16(_mm512_and_si512(v[g * 4 + 3], hmask), 2));
  696. _mm512_storeu_si512((__m512i *)ph, hbits); ph += 64;
  697. }
  698. }
  699. }
  700. template <>
  701. inline void pack_qs<block_iq4_xs>(void * RESTRICT packed_B, const block_iq4_xs * RESTRICT B, int KB) {
  702. __m512i v[16];
  703. char * pb = (char *)packed_B;
  704. for (int k = 0; k < QK_K / 64; ++k) {
  705. for (int n = 0; n < TILE_N; ++n) {
  706. __m256i r0 = bytes_from_nibbles_32(B[n * KB].qs + k * 32 + 0);
  707. __m256i r1 = bytes_from_nibbles_32(B[n * KB].qs + k * 32 + 16);
  708. v[n] = _mm512_inserti32x8(_mm512_castsi256_si512(r0), r1, 1);
  709. }
  710. transpose_16x16_32bit(v);
  711. // pack again with 128 to fully utilize vector length
  712. for (int n = 0; n < TILE_N; n += 2) {
  713. _mm512_storeu_si512((__m512i *)pb, packNibbles(v[n], v[n + 1]));
  714. pb += 64;
  715. }
  716. }
  717. }
  718. // pack B to vnni formats in 4bits or 8 bits
  719. void pack_B(void * RESTRICT packed_B, const block_q4_0 * RESTRICT B, int KB) {
  720. pack_qs(packed_B, B, KB);
  721. ggml_half * d0 = reinterpret_cast<ggml_half *>((char *)packed_B + TILE_N * TILE_K / 2);
  722. for (int n = 0; n < TILE_N; ++n) {
  723. d0[n] = B[n * KB].d;
  724. }
  725. }
  726. void pack_B(void * RESTRICT packed_B, const block_q4_1 * RESTRICT B, int KB) {
  727. pack_qs(packed_B, B, KB);
  728. ggml_half * d0 = reinterpret_cast<ggml_half *>((char *)packed_B + TILE_N * TILE_K / 2);
  729. ggml_half * m0 = d0 + TILE_N;
  730. for (int n = 0; n < TILE_N; ++n) {
  731. d0[n] = B[n * KB].d;
  732. m0[n] = B[n * KB].m;
  733. }
  734. }
  735. inline void s8s8_compensation(void * RESTRICT packed_B) {
  736. // packed_B layout:
  737. // quants {TILE_N, TILEK} int8_t
  738. // d0 {TILE_N} ggml_half
  739. // comp {TILE_N} int32_t
  740. const int offset = TILE_N * TILE_K + TILE_N * sizeof(ggml_half);
  741. __m512i vcomp = _mm512_setzero_si512();
  742. const __m512i off = _mm512_set1_epi8(static_cast<char>(0x80));
  743. for (int k = 0; k < 8; ++k) {
  744. __m512i vb = _mm512_loadu_si512((const __m512i *)((const char *)packed_B + k * 64));
  745. vcomp = _mm512_dpbusd_epi32(vcomp, off, vb);
  746. }
  747. _mm512_storeu_si512((__m512i *)((char *)(packed_B) + offset), vcomp);
  748. }
  749. void pack_B(void * RESTRICT packed_B, const block_q8_0 * RESTRICT B, int KB) {
  750. pack_qs(packed_B, B, KB);
  751. ggml_half * d0 = reinterpret_cast<ggml_half *>((char *)packed_B + TILE_N * TILE_K);
  752. for (int n = 0; n < TILE_N; ++n) {
  753. d0[n] = B[n * KB].d;
  754. }
  755. s8s8_compensation(packed_B);
  756. }
  757. // convert 8 * {min, scale} from int6 to int8
  758. inline void unpack_mins_and_scales(const uint8_t * scales, uint32_t * utmp) {
  759. const uint32_t kmask1 = 0x3f3f3f3f;
  760. const uint32_t kmask2 = 0x0f0f0f0f;
  761. const uint32_t kmask3 = 0x03030303;
  762. memcpy(utmp, scales, 12);
  763. utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
  764. const uint32_t uaux = utmp[1] & kmask1;
  765. utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
  766. utmp[2] = uaux;
  767. utmp[0] &= kmask1;
  768. }
  769. // packed_B layout:
  770. // quants {8, TILE_N, 16} uint8
  771. // scales {8, TILE_N} uint8
  772. // mins {8, TILE_N} uint8
  773. // d {TILE_N} ggml_half
  774. // dmin {TILE_N} ggml_half
  775. void pack_B(void * RESTRICT packed_B, const block_q4_K * RESTRICT B, int KB) {
  776. pack_qs(packed_B, B, KB);
  777. uint8_t * scales = reinterpret_cast<uint8_t *>((char *)packed_B + (QK_K / 2) * TILE_N);
  778. uint8_t * mins = scales + 8 * TILE_N;
  779. ggml_half * d = reinterpret_cast<ggml_half *>(mins + 8 * TILE_N);
  780. ggml_half * dmin = d + TILE_N;
  781. union {
  782. uint32_t u32[4];
  783. uint8_t u8[16];
  784. } s;
  785. for (int n = 0; n < TILE_N; ++n) {
  786. unpack_mins_and_scales(B[n * KB].scales, s.u32);
  787. for (int k = 0; k < 8; ++k) {
  788. scales[k * TILE_N + n] = s.u8[k];
  789. mins[(k >> 1) * TILE_N * 2 + n * 2 + (k & 0x1)] = s.u8[k + 8];
  790. }
  791. d[n] = B[n * KB].d;
  792. dmin[n] = B[n * KB].dmin;
  793. }
  794. }
  795. // packed_B layout:
  796. // quants {8, TILE_N, 16} uint8
  797. // qh {8, TILE_N, 4} uint8
  798. // scales {8, TILE_N} uint8
  799. // mins {8, TILE_N} uint8
  800. // d {TILE_N} ggml_half
  801. // dmin {TILE_N} ggml_half
  802. void pack_B(void * RESTRICT packed_B, const block_q5_K * RESTRICT B, int KB) {
  803. pack_qs(packed_B, B, KB);
  804. uint8_t * scales = reinterpret_cast<uint8_t *>((char *)packed_B + (QK_K / 2) * TILE_N + (QK_K / 8) * TILE_N);
  805. uint8_t * mins = scales + 8 * TILE_N;
  806. ggml_half * d = reinterpret_cast<ggml_half *>(mins + 8 * TILE_N);
  807. ggml_half * dmin = d + TILE_N;
  808. union {
  809. uint32_t u32[4];
  810. uint8_t u8[16];
  811. } s;
  812. for (int n = 0; n < TILE_N; ++n) {
  813. unpack_mins_and_scales(B[n * KB].scales, s.u32);
  814. for (int k = 0; k < 8; ++k) {
  815. scales[k * TILE_N + n] = s.u8[k];
  816. mins[(k >> 1) * TILE_N * 2 + n * 2 + (k & 0x1)] = s.u8[k + 8];
  817. }
  818. d[n] = B[n * KB].d;
  819. dmin[n] = B[n * KB].dmin;
  820. }
  821. }
  822. // packed_B layout:
  823. // quants {16, TILE_N, 8} uint8
  824. // qh {16, TILE_N, 4} uint8
  825. // scales {16, TILE_N} uint8
  826. // d {TILE_N} ggml_half
  827. void pack_B(void * RESTRICT packed_B, const block_q6_K * RESTRICT B, int KB) {
  828. pack_qs(packed_B, B, KB);
  829. uint8_t * scales = reinterpret_cast<uint8_t *>((char *)packed_B + (QK_K / 2) * TILE_N + (QK_K / 4) * TILE_N);
  830. ggml_half * d = reinterpret_cast<ggml_half *>(scales + 16 * TILE_N);
  831. for (int n = 0; n < TILE_N; ++n) {
  832. const int8_t * ps = B[n * KB].scales;
  833. for (int k = 0; k < 16; ++k) {
  834. scales[k * TILE_N + n] = ps[k];
  835. }
  836. d[n] = B[n * KB].d;
  837. }
  838. }
  839. // packed_B layout:
  840. // quants {8, TILE_N, 16} uint8
  841. // scales {8, TILE_N} int8
  842. // d {TILE_N} ggml_half
  843. void pack_B(void * RESTRICT packed_B, const block_iq4_xs * RESTRICT B, int KB) {
  844. pack_qs(packed_B, B, KB);
  845. int8_t * scales = reinterpret_cast<int8_t *>((char *)packed_B + (QK_K / 2) * TILE_N);
  846. ggml_half * d = reinterpret_cast<ggml_half *>(scales + 8 * TILE_N);
  847. // pack the scales
  848. for (int n = 0; n < TILE_N; ++n) {
  849. uint16_t sh = B[n * KB].scales_h;
  850. for (int k = 0; k < 8; k += 2) {
  851. const int16_t ls1 = ((B[n * KB].scales_l[k / 2] & 0xf) | ((sh << 4) & 0x30)) - 32;
  852. const int16_t ls2 = ((B[n * KB].scales_l[k / 2] >> 4) | ((sh << 2) & 0x30)) - 32;
  853. scales[(k + 0) * TILE_N + n] = ls1;
  854. scales[(k + 1) * TILE_N + n] = ls2;
  855. sh >>= 4;
  856. }
  857. d[n] = B[n * KB].d;
  858. }
  859. }
  860. template<typename TB, typename packed_B_t = packed_B_type<TB>>
  861. void unpack_B(packed_B_t * RESTRICT tile, const void * RESTRICT packed_B) {
  862. GGML_UNUSED(tile);
  863. GGML_UNUSED(packed_B);
  864. }
  865. template <>
  866. void unpack_B<block_q4_0>(int8_t * RESTRICT tile, const void * RESTRICT packed_B) {
  867. const __m512i off = _mm512_set1_epi8(8);
  868. const __m512i lowMask = _mm512_set1_epi8(0xF);
  869. for (int n = 0; n < 8; n += 2) {
  870. __m512i bytes = _mm512_loadu_si512((const __m512i *)((const char *)packed_B + n * 32));
  871. const __m512i r0 = _mm512_sub_epi8(_mm512_and_si512(bytes, lowMask), off);
  872. const __m512i r1 = _mm512_sub_epi8(_mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask), off);
  873. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 0), r0);
  874. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 64), r1);
  875. }
  876. }
  877. template <>
  878. void unpack_B<block_q4_1>(uint8_t * RESTRICT tile, const void * RESTRICT packed_B) {
  879. const __m512i lowMask = _mm512_set1_epi8(0xF);
  880. for (int n = 0; n < 8; n += 2) {
  881. __m512i bytes = _mm512_loadu_si512((const __m512i *)((const char *)packed_B + n * 32));
  882. const __m512i r0 = _mm512_and_si512(bytes, lowMask);
  883. const __m512i r1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  884. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 0), r0);
  885. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 64), r1);
  886. }
  887. }
  888. // packed_B_t for QKK is int8_t
  889. template <typename TB>
  890. void unpack_B(int8_t * RESTRICT tile, const void * RESTRICT packed_B, int k) {
  891. const int packed_B_group_size = QK_K / 2 * TILE_N / 8;
  892. const char * packed_B_group = (const char *)packed_B + k * packed_B_group_size;
  893. const __m512i lowMask = _mm512_set1_epi8(0xF);
  894. for (int n = 0; n < 8; n += 2) {
  895. __m512i bytes = _mm512_loadu_si512(packed_B_group + n * 32);
  896. const __m512i r0 = _mm512_and_si512(bytes, lowMask);
  897. const __m512i r1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  898. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 0), r0);
  899. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 64), r1);
  900. }
  901. }
  902. template <>
  903. void unpack_B<block_q5_K>(int8_t * RESTRICT tile, const void * RESTRICT packed_B, int k) {
  904. // lower 4bits, stride 256 bytes
  905. const int packed_l4_group_size = QK_K / 2 * TILE_N / 8;
  906. const char * pb = (const char *)packed_B + k * packed_l4_group_size;
  907. // higher 1bit, stride 64 bytes
  908. const int packed_h1_group_size = QK_K / 8 * TILE_N / 8;
  909. const char * ph = (const char *)packed_B + (QK_K / 2) * TILE_N + k * packed_h1_group_size;
  910. const __m512i hbits = _mm512_loadu_si512(ph);
  911. const __m512i lowMask = _mm512_set1_epi8(0xF);
  912. __m512i hmask0 = _mm512_set1_epi8(0x1);
  913. __m512i hmask1 = _mm512_set1_epi8(0x2);
  914. for (int n = 0; n < 8; n += 2) {
  915. __m512i bytes = _mm512_loadu_si512(pb + n * 32);
  916. __m512i r0 = _mm512_and_si512(bytes, lowMask);
  917. __m512i r1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  918. __m512i h0 = _mm512_slli_epi16(_mm512_srli_epi16(_mm512_and_si512(hbits, hmask0), n), 4);
  919. __m512i h1 = _mm512_slli_epi16(_mm512_srli_epi16(_mm512_and_si512(hbits, hmask1), n + 1), 4);
  920. hmask0 = _mm512_slli_epi16(hmask0, 2);
  921. hmask1 = _mm512_slli_epi16(hmask1, 2);
  922. r0 = _mm512_add_epi8(r0, h0);
  923. r1 = _mm512_add_epi8(r1, h1);
  924. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 0), r0);
  925. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 64), r1);
  926. }
  927. }
  928. template <>
  929. void unpack_B<block_q6_K>(int8_t * RESTRICT tile, const void * RESTRICT packed_B, int k) {
  930. // lower 4bits, stride 128 bytes
  931. const int packed_l4_group_size = QK_K / 2 * TILE_N / 16;
  932. const char * pb = (const char *)packed_B + k * packed_l4_group_size;
  933. // higher 2bits, stride 64 bytes
  934. const int packed_h2_group_size = QK_K / 4 * TILE_N / 16;
  935. const char * ph = (const char *)packed_B + (QK_K / 2) * TILE_N + k * packed_h2_group_size;
  936. const __m512i hbits = _mm512_loadu_si512(ph);
  937. const __m512i off = _mm512_set1_epi8(32);
  938. const __m512i lowMask = _mm512_set1_epi8(0xF);
  939. __m512i hmask0 = _mm512_set1_epi8(0x3); // 0011
  940. __m512i hmask1 = _mm512_set1_epi8(0xC); // 1100
  941. // notes: skip zero padding from row4 to row7 as we have done so in `unpack_A`
  942. __m512i bytes = _mm512_loadu_si512(pb);
  943. __m512i r0 = _mm512_and_si512(bytes, lowMask);
  944. __m512i r1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  945. __m512i h0 = _mm512_slli_epi16(_mm512_and_si512(hbits, hmask0), 4);
  946. __m512i h1 = _mm512_slli_epi16(_mm512_and_si512(hbits, hmask1), 2);
  947. _mm512_storeu_si512((__m512i *)(tile + 0), _mm512_sub_epi8(_mm512_add_epi8(r0, h0), off));
  948. _mm512_storeu_si512((__m512i *)(tile + 64), _mm512_sub_epi8(_mm512_add_epi8(r1, h1), off));
  949. hmask0 = _mm512_slli_epi16(hmask0, 4);
  950. hmask1 = _mm512_slli_epi16(hmask1, 4);
  951. bytes = _mm512_loadu_si512(pb + 64);
  952. r0 = _mm512_and_si512(bytes, lowMask);
  953. r1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  954. h0 = _mm512_and_si512(hbits, hmask0);
  955. h1 = _mm512_srli_epi16(_mm512_and_si512(hbits, hmask1), 2);
  956. _mm512_storeu_si512((__m512i *)(tile + 128), _mm512_sub_epi8(_mm512_add_epi8(r0, h0), off));
  957. _mm512_storeu_si512((__m512i *)(tile + 192), _mm512_sub_epi8(_mm512_add_epi8(r1, h1), off));
  958. }
  959. template <>
  960. void unpack_B<block_iq4_xs>(int8_t * RESTRICT tile, const void * RESTRICT packed_B, int k) {
  961. static const __m512i values128 = _mm512_set_epi8(
  962. 113, 89, 69, 53, 38, 25, 13, 1, -10, -22, -35, -49, -65, -83, -104, -127,
  963. 113, 89, 69, 53, 38, 25, 13, 1, -10, -22, -35, -49, -65, -83, -104, -127,
  964. 113, 89, 69, 53, 38, 25, 13, 1, -10, -22, -35, -49, -65, -83, -104, -127,
  965. 113, 89, 69, 53, 38, 25, 13, 1, -10, -22, -35, -49, -65, -83, -104, -127
  966. );
  967. const int packed_B_group_size = QK_K / 2 * TILE_N / 8;
  968. const char * pb = (const char *)packed_B + k * packed_B_group_size;
  969. const __m512i lowMask = _mm512_set1_epi8(0xF);
  970. for (int n = 0; n < 8; n += 2) {
  971. __m512i bytes = _mm512_loadu_si512(pb + n * 32);
  972. const __m512i r0 = _mm512_shuffle_epi8(values128, _mm512_and_si512(bytes, lowMask));
  973. const __m512i r1 = _mm512_shuffle_epi8(values128, _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask));
  974. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 0), r0);
  975. _mm512_storeu_si512((__m512i *)(tile + n * 64 + 64), r1);
  976. }
  977. }
  978. template <typename TA, typename TB, bool is_acc>
  979. struct acc_C {};
  980. template <bool is_acc>
  981. struct acc_C<block_q8_0, block_q4_0, is_acc> {
  982. static void apply(float * RESTRICT C, int ldc, const int32_t * RESTRICT tile, const block_q8_0 * A, int lda, const void * packed_B, int nr) {
  983. const int offset = TILE_N * TILE_K / 2;
  984. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)((const char *)packed_B + offset)));
  985. for (int m = 0; m < nr; ++m) {
  986. const __m512 vd1 = _mm512_set1_ps(GGML_FP16_TO_FP32(A[m * lda].d));
  987. const __m512 vtile = _mm512_cvtepi32_ps(_mm512_loadu_si512(tile + m * TILE_N));
  988. __m512 vsum;
  989. if (is_acc) {
  990. vsum = _mm512_loadu_ps(C + m * ldc);
  991. } else {
  992. vsum = _mm512_set1_ps(0.f);
  993. }
  994. vsum = _mm512_fmadd_ps(vtile, _mm512_mul_ps(vd0, vd1), vsum);
  995. _mm512_storeu_ps(C + m * ldc, vsum);
  996. }
  997. }
  998. };
  999. template <bool is_acc>
  1000. struct acc_C<block_q8_1, block_q4_1, is_acc> {
  1001. static void apply(float * RESTRICT C, int ldc, const int32_t * RESTRICT tile, const block_q8_1 * A, int lda, const void * packed_B, int nr) {
  1002. const int offset = TILE_N * TILE_K / 2;
  1003. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)((const char *)packed_B + offset)));
  1004. const __m512 vm0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)((const char *)packed_B + offset + TILE_N * sizeof(ggml_half))));
  1005. for (int m = 0; m < nr; ++m) {
  1006. const __m512 vd1 = _mm512_set1_ps(GGML_FP16_TO_FP32(A[m * lda].d));
  1007. const __m512 vs1 = _mm512_set1_ps(GGML_FP16_TO_FP32(A[m * lda].s));
  1008. const __m512 vtile = _mm512_cvtepi32_ps(_mm512_loadu_si512(tile + m * TILE_N));
  1009. __m512 vsum;
  1010. if (is_acc) {
  1011. vsum = _mm512_loadu_ps(C + m * ldc);
  1012. } else {
  1013. vsum = _mm512_set1_ps(0.f);
  1014. }
  1015. vsum = _mm512_fmadd_ps(vtile, _mm512_mul_ps(vd0, vd1), vsum);
  1016. vsum = _mm512_fmadd_ps(vm0, vs1, vsum);
  1017. _mm512_storeu_ps(C + m * ldc, vsum);
  1018. }
  1019. }
  1020. };
  1021. template <bool is_acc>
  1022. struct acc_C<block_q8_0, block_q8_0, is_acc> {
  1023. static void apply(float * RESTRICT C, int ldc, const int32_t * RESTRICT tile, const block_q8_0 * A, int lda, const void * packed_B, int nr) {
  1024. const int offset = TILE_N * TILE_K;
  1025. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)((const char *)packed_B + offset)));
  1026. for (int m = 0; m < nr; ++m) {
  1027. const __m512 vd1 = _mm512_set1_ps(GGML_FP16_TO_FP32(A[m * lda].d));
  1028. const __m512 vtile = _mm512_cvtepi32_ps(_mm512_loadu_si512(tile + m * TILE_N));
  1029. __m512 vsum;
  1030. if (is_acc) {
  1031. vsum = _mm512_loadu_ps(C + m * ldc);
  1032. } else {
  1033. vsum = _mm512_set1_ps(0.f);
  1034. }
  1035. vsum = _mm512_fmadd_ps(vtile, _mm512_mul_ps(vd0, vd1), vsum);
  1036. _mm512_storeu_ps(C + m * ldc, vsum);
  1037. }
  1038. }
  1039. };
  1040. template <bool is_acc>
  1041. struct acc_C<block_q8_K, block_q4_K, is_acc> {
  1042. static void apply(float * RESTRICT C, int ldc, const int32_t * RESTRICT tile, const block_q8_K * A, int lda, const void * packed_B, int nr) {
  1043. const uint8_t * scales = reinterpret_cast<const uint8_t *>((const char *)packed_B + (QK_K / 2) * TILE_N);
  1044. const uint8_t * mins = scales + 8 * TILE_N;
  1045. const ggml_half * d0 = reinterpret_cast<const ggml_half *>(mins + 8 * TILE_N);
  1046. const ggml_half * dmin = d0 + TILE_N;
  1047. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)d0));
  1048. const __m512 vdmin = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)dmin));
  1049. for (int m = 0; m < nr; ++m) {
  1050. const float d1 = A[m * lda].d;
  1051. const __m512 vd = _mm512_mul_ps(_mm512_set1_ps(d1), vd0);
  1052. const __m512 vdm = _mm512_mul_ps(_mm512_set1_ps(-d1), vdmin);
  1053. const __m512 vtile = _mm512_cvtepi32_ps(_mm512_loadu_si512(tile + m * TILE_N));
  1054. __m512 vsum;
  1055. if (is_acc) {
  1056. vsum = _mm512_loadu_ps(C + m * ldc);
  1057. } else {
  1058. vsum = _mm512_set1_ps(0.f);
  1059. }
  1060. const __m256i q8sums = _mm256_loadu_si256((const __m256i *)A[m * lda].bsums);
  1061. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  1062. __m512i acc_m = _mm512_setzero_si512();
  1063. for (int k = 0; k < 4; ++k) {
  1064. __m512i vmask = _mm512_set1_epi32(k);
  1065. __m512i va = _mm512_permutexvar_epi32(vmask, _mm512_castsi128_si512(q8s));
  1066. __m512i vb = _mm512_cvtepi8_epi16(_mm256_loadu_si256((const __m256i *)(mins + k * 32)));
  1067. acc_m = _mm512_dpwssds_epi32(acc_m, va, vb);
  1068. }
  1069. vsum = _mm512_fmadd_ps(vtile, vd, vsum);
  1070. vsum = _mm512_fmadd_ps(_mm512_cvtepi32_ps(acc_m), vdm, vsum);
  1071. _mm512_storeu_ps(C + m * ldc, vsum);
  1072. }
  1073. }
  1074. };
  1075. template <bool is_acc>
  1076. struct acc_C<block_q8_K, block_q5_K, is_acc> {
  1077. static void apply(float * RESTRICT C, int ldc, const int32_t * RESTRICT tile, const block_q8_K * A, int lda, const void * packed_B, int nr) {
  1078. const uint8_t * scales = reinterpret_cast<const uint8_t *>((const char *)packed_B + (QK_K / 2) * TILE_N + (QK_K / 8) * TILE_N);
  1079. const uint8_t * mins = scales + 8 * TILE_N;
  1080. const ggml_half * d0 = reinterpret_cast<const ggml_half *>(mins + 8 * TILE_N);
  1081. const ggml_half * dmin = d0 + TILE_N;
  1082. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)d0));
  1083. const __m512 vdmin = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)dmin));
  1084. for (int m = 0; m < nr; ++m) {
  1085. const float d1 = A[m * lda].d;
  1086. const __m512 vd = _mm512_mul_ps(_mm512_set1_ps(d1), vd0);
  1087. const __m512 vdm = _mm512_mul_ps(_mm512_set1_ps(-d1), vdmin);
  1088. const __m512 vtile = _mm512_cvtepi32_ps(_mm512_loadu_si512(tile + m * TILE_N));
  1089. __m512 vsum;
  1090. if (is_acc) {
  1091. vsum = _mm512_loadu_ps(C + m * ldc);
  1092. } else {
  1093. vsum = _mm512_set1_ps(0.f);
  1094. }
  1095. const __m256i q8sums = _mm256_loadu_si256((const __m256i *)A[m * lda].bsums);
  1096. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  1097. __m512i acc_m = _mm512_setzero_si512();
  1098. for (int k = 0; k < 4; ++k) {
  1099. __m512i vmask = _mm512_set1_epi32(k);
  1100. __m512i va = _mm512_permutexvar_epi32(vmask, _mm512_castsi128_si512(q8s));
  1101. __m512i vb = _mm512_cvtepi8_epi16(_mm256_loadu_si256((const __m256i *)(mins + k * 32)));
  1102. acc_m = _mm512_dpwssds_epi32(acc_m, va, vb);
  1103. }
  1104. vsum = _mm512_fmadd_ps(vtile, vd, vsum);
  1105. vsum = _mm512_fmadd_ps(_mm512_cvtepi32_ps(acc_m), vdm, vsum);
  1106. _mm512_storeu_ps(C + m * ldc, vsum);
  1107. }
  1108. }
  1109. };
  1110. template <bool is_acc>
  1111. struct acc_C<block_q8_K, block_q6_K, is_acc> {
  1112. static void apply(float * RESTRICT C, int ldc, const int32_t * RESTRICT tile, const block_q8_K * A, int lda, const void * packed_B, int nr) {
  1113. const uint8_t * scales = reinterpret_cast<const uint8_t *>((const char *)packed_B + (QK_K / 2) * TILE_N + (QK_K / 4) * TILE_N);
  1114. const ggml_half * d0 = reinterpret_cast<const ggml_half *>(scales + 16 * TILE_N);
  1115. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)d0));
  1116. for (int m = 0; m < nr; ++m) {
  1117. const float d1 = A[m * lda].d;
  1118. const __m512 vd = _mm512_mul_ps(_mm512_set1_ps(d1), vd0);
  1119. const __m512 vtile = _mm512_cvtepi32_ps(_mm512_loadu_si512(tile + m * TILE_N));
  1120. __m512 vsum;
  1121. if (is_acc) {
  1122. vsum = _mm512_loadu_ps(C + m * ldc);
  1123. } else {
  1124. vsum = _mm512_set1_ps(0.f);
  1125. }
  1126. vsum = _mm512_fmadd_ps(vtile, vd, vsum);
  1127. _mm512_storeu_ps(C + m * ldc, vsum);
  1128. }
  1129. }
  1130. };
  1131. template <bool is_acc>
  1132. struct acc_C<block_q8_K, block_iq4_xs, is_acc> {
  1133. static void apply(float * RESTRICT C, int ldc, const int32_t * RESTRICT tile, const block_q8_K * A, int lda, const void * packed_B, int nr) {
  1134. const int8_t * scales = reinterpret_cast<const int8_t *>((const char *)packed_B + (QK_K / 2) * TILE_N);
  1135. const ggml_half * d0 = reinterpret_cast<const ggml_half *>(scales + 8 * TILE_N);
  1136. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)d0));
  1137. for (int m = 0; m < nr; ++m) {
  1138. const float d1 = A[m * lda].d;
  1139. const __m512 vd = _mm512_mul_ps(_mm512_set1_ps(d1), vd0);
  1140. const __m512 vtile = _mm512_cvtepi32_ps(_mm512_loadu_si512(tile + m * TILE_N));
  1141. __m512 vsum;
  1142. if (is_acc) {
  1143. vsum = _mm512_loadu_ps(C + m * ldc);
  1144. } else {
  1145. vsum = _mm512_set1_ps(0.f);
  1146. }
  1147. vsum = _mm512_fmadd_ps(vtile, vd, vsum);
  1148. _mm512_storeu_ps(C + m * ldc, vsum);
  1149. }
  1150. }
  1151. };
  1152. template <typename TB> constexpr int get_quants_size();
  1153. template <> constexpr int get_quants_size<block_q4_K>() { return (QK_K / 2) * TILE_N; }
  1154. template <> constexpr int get_quants_size<block_q5_K>() { return (QK_K / 2) * TILE_N + (QK_K / 8) * TILE_N; }
  1155. template <> constexpr int get_quants_size<block_q6_K>() { return (QK_K / 2) * TILE_N + (QK_K / 4) * TILE_N; }
  1156. template <> constexpr int get_quants_size<block_iq4_xs>() { return (QK_K / 2) * TILE_N; }
  1157. // used for QKK format
  1158. template <typename TB, bool is_acc,
  1159. typename std::enable_if<is_type_qkk<TB>::value, int>::type = 0>
  1160. inline void scale_C(const int32_t * RESTRICT tile, int32_t * RESTRICT sumi, const void * packed_B, int k, int nr) {
  1161. const uint8_t * scales = reinterpret_cast<const uint8_t *>((const char *)packed_B + get_quants_size<TB>());
  1162. const __m512i vscale = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i *)(scales + k * TILE_N)));
  1163. for (int m = 0; m < nr; ++m) {
  1164. __m512i vsumi;
  1165. if (is_acc) {
  1166. vsumi = _mm512_loadu_si512(sumi + m * TILE_N);
  1167. } else {
  1168. vsumi = _mm512_setzero_si512();
  1169. }
  1170. __m512i vtile = _mm512_loadu_si512(tile + m * TILE_N);
  1171. vsumi = _mm512_add_epi32(vsumi, _mm512_mullo_epi32(vtile, vscale));
  1172. _mm512_storeu_si512((__m512i *)(sumi + m * TILE_N), vsumi);
  1173. }
  1174. }
  1175. template <typename TA, typename TB, typename TC, int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1176. struct tinygemm_kernel_avx {
  1177. static void apply(int K, const TA * RESTRICT A, const TB * RESTRICT B, TC * RESTRICT C, int ldc) {
  1178. GGML_UNUSED(K);
  1179. GGML_UNUSED(A);
  1180. GGML_UNUSED(B);
  1181. GGML_UNUSED(C);
  1182. GGML_UNUSED(ldc);
  1183. }
  1184. };
  1185. template <int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1186. struct tinygemm_kernel_avx<float, ggml_fp16_t, float, BLOCK_M, BLOCK_N, BLOCK_K> {
  1187. static void apply(int K, const float * RESTRICT A, const ggml_fp16_t * RESTRICT B, float * RESTRICT C, int ldc) {
  1188. constexpr int ROWS = BLOCK_M;
  1189. constexpr int COLS = BLOCK_N;
  1190. assert(BLOCK_K == 16);
  1191. __m512 va;
  1192. __m512 vb[COLS];
  1193. __m512 vc[ROWS * COLS];
  1194. auto loadc = [&](auto idx) {
  1195. vc[idx] = _mm512_setzero_ps();
  1196. };
  1197. Unroll<ROWS * COLS>{}(loadc);
  1198. auto compute = [&](auto idx, auto k) {
  1199. constexpr int row = idx / COLS;
  1200. constexpr int col = idx % COLS;
  1201. if constexpr (col == 0) {
  1202. va = _mm512_loadu_ps(A + row * K + k);
  1203. }
  1204. if constexpr (row == 0) {
  1205. vb[col] = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(B + col * K + k)));
  1206. }
  1207. vc[idx] = _mm512_fmadd_ps(va, vb[col], vc[idx]);
  1208. };
  1209. for (int k = 0; k < K; k += 16) {
  1210. Unroll<ROWS * COLS>{}(compute, k);
  1211. }
  1212. auto storec = [&](auto idx) {
  1213. constexpr int row = idx / COLS;
  1214. constexpr int col = idx % COLS;
  1215. C[row * ldc + col] = _mm512_reduce_add_ps(vc[idx]);
  1216. };
  1217. Unroll<ROWS * COLS>{}(storec);
  1218. }
  1219. };
  1220. #define LAUNCH_TINYGEMM_KERNEL_AVX(MB_SIZE, NB_SIZE) \
  1221. tinygemm_kernel_avx<float, type, float, MB_SIZE, NB_SIZE, blck_size>::apply( \
  1222. K, (const float *)src1->data + mb_start * K, \
  1223. (const type *)src0->data + nb_start * K, \
  1224. (float *)dst->data + mb_start * ldc + nb_start, ldc);
  1225. // re-organize in the format {NB, KB, TILE_SIZE}:
  1226. #define PACKED_INDEX(n, k, KB, tile_size) (n * KB + k) * tile_size
  1227. template<typename TB, int BLOCK_K>
  1228. void convert_B_packed_format(void * RESTRICT packed_B, const TB * RESTRICT B, int N, int K) {
  1229. const int NB = N / TILE_N;
  1230. const int KB = K / BLOCK_K;
  1231. const int TILE_SIZE = get_tile_size<TB>();
  1232. // parallel on NB should be enough
  1233. parallel_for(NB, [&](int begin, int end) {
  1234. for (int n = begin; n < end; ++n) {
  1235. for (int k = 0; k < KB; ++k) {
  1236. int n0 = n * TILE_N;
  1237. pack_B((char *)packed_B + PACKED_INDEX(n, k, KB, TILE_SIZE), &B[n0 * KB + k], KB);
  1238. }
  1239. }
  1240. });
  1241. }
  1242. template <typename TA, typename TB, typename TC, int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1243. struct tinygemm_kernel_vnni {};
  1244. template <int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1245. struct tinygemm_kernel_vnni<block_q8_0, block_q4_0, float, BLOCK_M, BLOCK_N, BLOCK_K> {
  1246. static void apply(int KB, const void * RESTRICT _A, const void * RESTRICT _B, float * RESTRICT C, int ldc) {
  1247. constexpr int COLS = BLOCK_N / 16;
  1248. const int TILE_SIZE = TILE_N * sizeof(block_q4_0);
  1249. const block_q8_0 * RESTRICT A = static_cast<const block_q8_0 *>(_A);
  1250. const char * RESTRICT B = static_cast<const char *>(_B);
  1251. __m512i va[8];
  1252. __m512 vc[COLS];
  1253. __m512 vd1;
  1254. // sum of offsets, shared across COLS
  1255. //
  1256. // avx512-vnni does not have `_mm512_dpbssd_epi32`,
  1257. // need to transfrom ss to us:
  1258. // a * (b - 8) is equavilent to b * a - 8 * a
  1259. // s u u u s u s
  1260. //
  1261. __m512i vcomp;
  1262. const __m512i off = _mm512_set1_epi8(8);
  1263. const __m512i lowMask = _mm512_set1_epi8(0xF);
  1264. auto loadc = [&](auto col) {
  1265. vc[col] = _mm512_setzero_ps();
  1266. };
  1267. Unroll<COLS>{}(loadc);
  1268. auto compute = [&](auto col, auto i) {
  1269. // load a and compute compensation
  1270. if constexpr (col == 0) {
  1271. const int32_t * a_ptr = reinterpret_cast<const int32_t *>(A[0 * KB + i].qs);
  1272. vcomp = _mm512_setzero_si512();
  1273. for (int k = 0; k < 8; ++k) {
  1274. va[k] = _mm512_set1_epi32(a_ptr[k]);
  1275. vcomp = _mm512_dpbusd_epi32(vcomp, off, va[k]);
  1276. }
  1277. vd1 = _mm512_set1_ps(GGML_FP16_TO_FP32(A[0 * KB + i].d));
  1278. }
  1279. // load b
  1280. __m512i vsum = _mm512_setzero_si512();
  1281. const char * b_ptr = B + PACKED_INDEX(col, i, KB, TILE_SIZE);
  1282. for (int k = 0; k < 8; k += 2) {
  1283. __m512i bytes = _mm512_loadu_si512((const __m512i *)(b_ptr + k * 32));
  1284. __m512i vb0 = _mm512_and_si512(bytes, lowMask);
  1285. vsum = _mm512_dpbusd_epi32(vsum, vb0, va[k + 0]);
  1286. __m512i vb1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  1287. vsum = _mm512_dpbusd_epi32(vsum, vb1, va[k + 1]);
  1288. }
  1289. const int offset = TILE_N * TILE_K / 2;
  1290. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset)));
  1291. vsum = _mm512_sub_epi32(vsum, vcomp);
  1292. vc[col] = _mm512_fmadd_ps(_mm512_cvtepi32_ps(vsum), _mm512_mul_ps(vd0, vd1), vc[col]);
  1293. };
  1294. for (int i = 0; i < KB; ++i) {
  1295. Unroll<COLS>{}(compute, i);
  1296. }
  1297. //store to C
  1298. auto storec = [&](auto col) {
  1299. _mm512_storeu_ps((__m512i*)(C + 0 * ldc + col * 16), vc[col]);
  1300. };
  1301. Unroll<COLS>{}(storec);
  1302. }
  1303. };
  1304. template <int BLOCK_N, int BLOCK_K>
  1305. struct tinygemm_kernel_vnni<block_q8_1, block_q4_1, float, 1, BLOCK_N, BLOCK_K> {
  1306. static void apply(int KB, const void * RESTRICT _A, const void * RESTRICT _B, float * RESTRICT C, int ldc) {
  1307. constexpr int COLS = BLOCK_N / 16;
  1308. const int TILE_SIZE = TILE_N * sizeof(block_q4_1);
  1309. const block_q8_1 * RESTRICT A = static_cast<const block_q8_1 *>(_A);
  1310. const char * RESTRICT B = static_cast<const char *>(_B);
  1311. __m512i va[8];
  1312. __m512i vb[8];
  1313. __m512 vc[COLS];
  1314. __m512 vd1, vs1;
  1315. const __m512i lowMask = _mm512_set1_epi8(0xF);
  1316. auto loadc = [&](auto col) {
  1317. vc[col] = _mm512_setzero_ps();
  1318. };
  1319. Unroll<COLS>{}(loadc);
  1320. auto compute = [&](auto col, auto i) {
  1321. // load a
  1322. if constexpr (col == 0) {
  1323. const int32_t * a_ptr = reinterpret_cast<const int32_t *>(A[0 * KB + i].qs);
  1324. for (int k = 0; k < 8; ++k) {
  1325. va[k] = _mm512_set1_epi32(a_ptr[k]);
  1326. }
  1327. vd1 = _mm512_set1_ps(GGML_FP16_TO_FP32(A[0 * KB + i].d));
  1328. vs1 = _mm512_set1_ps(GGML_FP16_TO_FP32(A[0 * KB + i].s));
  1329. }
  1330. // load b
  1331. const char * b_ptr = B + PACKED_INDEX(col, i, KB, TILE_SIZE);
  1332. for (int k = 0; k < 8; k += 2) {
  1333. __m512i bytes = _mm512_loadu_si512((const __m512i *)(b_ptr + k * 32));
  1334. vb[k + 0] = _mm512_and_si512(bytes, lowMask);
  1335. vb[k + 1] = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  1336. }
  1337. const int offset = TILE_N * TILE_K / 2;
  1338. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset)));
  1339. const __m512 vm0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset + TILE_N * sizeof(ggml_half))));
  1340. __m512i vsum = _mm512_setzero_si512();
  1341. for (int k = 0; k < 8; ++k) {
  1342. vsum = _mm512_dpbusd_epi32(vsum, vb[k], va[k]);
  1343. }
  1344. vc[col] = _mm512_fmadd_ps(_mm512_cvtepi32_ps(vsum), _mm512_mul_ps(vd0, vd1), vc[col]);
  1345. vc[col] = _mm512_fmadd_ps(vm0, vs1, vc[col]);
  1346. };
  1347. for (int i = 0; i < KB; ++i) {
  1348. Unroll<COLS>{}(compute, i);
  1349. }
  1350. //store to C
  1351. auto storec = [&](auto col) {
  1352. _mm512_storeu_ps((__m512i*)(C + 0 * ldc + col * 16), vc[col]);
  1353. };
  1354. Unroll<COLS>{}(storec);
  1355. }
  1356. };
  1357. template <int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1358. struct tinygemm_kernel_vnni<block_q8_0, block_q8_0, float, BLOCK_M, BLOCK_N, BLOCK_K> {
  1359. static void apply(int KB, const void * RESTRICT _A, const void * RESTRICT _B, float * RESTRICT C, int ldc) {
  1360. constexpr int COLS = BLOCK_N / 16;
  1361. const int TILE_SIZE = TILE_N * sizeof(block_q8_0) + TILE_N * sizeof(int32_t);
  1362. const block_q8_0 * RESTRICT A = static_cast<const block_q8_0 *>(_A);
  1363. const char * RESTRICT B = static_cast<const char *>(_B);
  1364. __m512i va[8];
  1365. __m512i vb[8];
  1366. __m512 vc[COLS];
  1367. __m512 vd1;
  1368. // Notes: s8s8 igemm compensation in avx512-vnni
  1369. // change s8s8 to u8s8 with compensate
  1370. // a * b = (a + 128) * b - 128 * b
  1371. // s s u s u s
  1372. //
  1373. // (128 * b is pre-computed when packing B to vnni formats)
  1374. //
  1375. const __m512i off = _mm512_set1_epi8(static_cast<char>(0x80));
  1376. auto loadc = [&](auto col) {
  1377. vc[col] = _mm512_setzero_ps();
  1378. };
  1379. Unroll<COLS>{}(loadc);
  1380. auto compute = [&](auto col, auto i) {
  1381. // load a and add offset 128
  1382. if constexpr (col == 0) {
  1383. const int32_t * a_ptr = reinterpret_cast<const int32_t *>(A[0 * KB + i].qs);
  1384. for (int k = 0; k < 8; ++k) {
  1385. va[k] = _mm512_set1_epi32(a_ptr[k]);
  1386. va[k] = _mm512_add_epi8(va[k], off);
  1387. }
  1388. vd1 = _mm512_set1_ps(GGML_FP16_TO_FP32(A[0 * KB + i].d));
  1389. }
  1390. // load b
  1391. const char * b_ptr = B + PACKED_INDEX(col, i, KB, TILE_SIZE);
  1392. for (int k = 0; k < 8; ++k) {
  1393. vb[k] = _mm512_loadu_si512((const __m512i *)(b_ptr + k * 64));
  1394. }
  1395. const int offset = TILE_N * TILE_K;
  1396. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset)));
  1397. const int offset2 = TILE_N * TILE_K + TILE_N * sizeof(ggml_half);
  1398. const __m512i vcomp = _mm512_loadu_si512((const __m512i *)(b_ptr + offset2));
  1399. __m512i vsum = _mm512_setzero_si512();
  1400. for (int k = 0; k < 8; ++k) {
  1401. vsum = _mm512_dpbusd_epi32(vsum, va[k], vb[k]);
  1402. }
  1403. vsum = _mm512_sub_epi32(vsum, vcomp);
  1404. vc[col] = _mm512_fmadd_ps(_mm512_cvtepi32_ps(vsum), _mm512_mul_ps(vd0, vd1), vc[col]);
  1405. };
  1406. for (int i = 0; i < KB; ++i) {
  1407. Unroll<COLS>{}(compute, i);
  1408. }
  1409. //store to C
  1410. auto storec = [&](auto col) {
  1411. _mm512_storeu_ps((__m512i*)(C + 0 * ldc + col * 16), vc[col]);
  1412. };
  1413. Unroll<COLS>{}(storec);
  1414. }
  1415. };
  1416. template <int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1417. struct tinygemm_kernel_vnni<block_q8_K, block_q4_K, float, BLOCK_M, BLOCK_N, BLOCK_K> {
  1418. static void apply(int KB, const void * RESTRICT _A, const void * RESTRICT _B, float * RESTRICT C, int ldc) {
  1419. constexpr int COLS = BLOCK_N / 16;
  1420. const int TILE_SIZE = TILE_N * sizeof(block_q4_K) + TILE_N * 4;
  1421. const block_q8_K * RESTRICT A = static_cast<const block_q8_K *>(_A);
  1422. const char * RESTRICT B = static_cast<const char *>(_B);
  1423. // a.qs: 8 groups, 32 bytes each group (m256i)
  1424. __m512i va[8];
  1425. // a.bsum: 8 groups, 2 bytes each group (m128i)
  1426. __m512i va_bsum;
  1427. __m512 vc[COLS];
  1428. __m512 vd1;
  1429. // packed_B:
  1430. const int offset_scales = (QK_K / 2) * TILE_N;
  1431. const int offset_mins = (QK_K / 2) * TILE_N + 8 * TILE_N;
  1432. const int offset_d0 = (QK_K / 2) * TILE_N + 16 * TILE_N;
  1433. const int offset_dmin = (QK_K / 2) * TILE_N + 16 * TILE_N + TILE_N * sizeof(ggml_half);
  1434. const __m512i lowMask = _mm512_set1_epi8(0xF);
  1435. auto loadc = [&](auto col) {
  1436. vc[col] = _mm512_setzero_ps();
  1437. };
  1438. Unroll<COLS>{}(loadc);
  1439. // Notes: vnni formats in QK_K
  1440. // a) quants vnni format
  1441. // int8 {k/4, n, 4}, viewed as 2d {k/4, 4n}, k = 32
  1442. // from {16, 32} to {8, 64}
  1443. //
  1444. // b) min vnni format
  1445. // int16 {k/2, n, 2}, viewed as 2d {k/2, 2n}, k = 8
  1446. // from {16, 8} to {4, 32}
  1447. //
  1448. auto compute = [&](auto col, auto i) {
  1449. // load a
  1450. if constexpr (col == 0) {
  1451. for (int k_group = 0; k_group < QK_K / 32; ++k_group) {
  1452. va[k_group] = _mm512_castsi256_si512(_mm256_loadu_si256((const __m256i *)(A[0 * KB + i].qs + k_group * 32)));
  1453. }
  1454. const __m256i q8sums = _mm256_loadu_si256((const __m256i *)A[0 * KB + i].bsums);
  1455. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  1456. va_bsum = _mm512_castsi128_si512(q8s);
  1457. vd1 = _mm512_set1_ps(A[0 * KB + i].d);
  1458. }
  1459. // step 1: accumultate the quants
  1460. __m512i acc = _mm512_setzero_si512();
  1461. const char * b_ptr = B + PACKED_INDEX(col, i, KB, TILE_SIZE);
  1462. const char * b_qs = b_ptr;
  1463. for (int k_group = 0; k_group < QK_K / 32; ++k_group) {
  1464. __m512i vsum = _mm512_setzero_si512();
  1465. for (int k = 0; k < 8; k += 2) {
  1466. __m512i va0 = _mm512_permutexvar_epi32(_mm512_set1_epi32(k + 0), va[k_group]);
  1467. __m512i va1 = _mm512_permutexvar_epi32(_mm512_set1_epi32(k + 1), va[k_group]);
  1468. __m512i bytes = _mm512_loadu_si512((const __m512i *)b_qs);
  1469. __m512i vb0 = _mm512_and_si512(bytes, lowMask);
  1470. vsum = _mm512_dpbusd_epi32(vsum, vb0, va0);
  1471. __m512i vb1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  1472. vsum = _mm512_dpbusd_epi32(vsum, vb1, va1);
  1473. b_qs += 64;
  1474. }
  1475. // vacc += scale * (q8 @ q4)
  1476. const __m512i vscale = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i *)(b_ptr + offset_scales + k_group * TILE_N)));
  1477. acc = _mm512_add_epi32(acc, _mm512_mullo_epi32(vsum, vscale));
  1478. }
  1479. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset_d0)));
  1480. vc[col] = _mm512_fmadd_ps(_mm512_cvtepi32_ps(acc), _mm512_mul_ps(vd0, vd1), vc[col]);
  1481. // step 2: accumulate the mins
  1482. __m512i acc_m = _mm512_setzero_si512();
  1483. for (int k = 0; k < 4; ++k) {
  1484. __m512i vmask = _mm512_set1_epi32(k);
  1485. __m512i va = _mm512_permutexvar_epi32(vmask, va_bsum);
  1486. __m512i vb = _mm512_cvtepi8_epi16(_mm256_loadu_si256((const __m256i *)(b_ptr + offset_mins + k * 32)));
  1487. acc_m = _mm512_dpwssds_epi32(acc_m, va, vb);
  1488. }
  1489. const __m512 vdmin = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset_dmin)));
  1490. vc[col] = _mm512_fnmadd_ps(_mm512_cvtepi32_ps(acc_m), _mm512_mul_ps(vdmin, vd1), vc[col]);
  1491. };
  1492. for (int i = 0; i < KB; ++i) {
  1493. Unroll<COLS>{}(compute, i);
  1494. }
  1495. //store to C
  1496. auto storec = [&](auto col) {
  1497. _mm512_storeu_ps((__m512i*)(C + 0 * ldc + col * 16), vc[col]);
  1498. };
  1499. Unroll<COLS>{}(storec);
  1500. }
  1501. };
  1502. template <int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1503. struct tinygemm_kernel_vnni<block_q8_K, block_q5_K, float, BLOCK_M, BLOCK_N, BLOCK_K> {
  1504. static void apply(int KB, const void * RESTRICT _A, const void * RESTRICT _B, float * RESTRICT C, int ldc) {
  1505. constexpr int COLS = BLOCK_N / 16;
  1506. const int TILE_SIZE = TILE_N * sizeof(block_q5_K) + TILE_N * 4;
  1507. const block_q8_K * RESTRICT A = static_cast<const block_q8_K *>(_A);
  1508. const char * RESTRICT B = static_cast<const char *>(_B);
  1509. // a.qs: 8 groups, 32 bytes each group (m256i)
  1510. __m512i va[8];
  1511. // a.bsum: 8 groups, 2 bytes each group (m128i)
  1512. __m512i va_bsum;
  1513. __m512 vc[COLS];
  1514. __m512 vd1;
  1515. // packed_B:
  1516. const int offset_qh = (QK_K / 2) * TILE_N;
  1517. const int offset_scales = (QK_K / 2) * TILE_N + (QK_K / 8) * TILE_N;
  1518. const int offset_mins = (QK_K / 2) * TILE_N + (QK_K / 8) * TILE_N + 8 * TILE_N;
  1519. const int offset_d0 = (QK_K / 2) * TILE_N + (QK_K / 8) * TILE_N + 16 * TILE_N;
  1520. const int offset_dmin = (QK_K / 2) * TILE_N + (QK_K / 8) * TILE_N + 16 * TILE_N + TILE_N * sizeof(ggml_half);
  1521. const __m512i lowMask = _mm512_set1_epi8(0xF);
  1522. auto loadc = [&](auto col) {
  1523. vc[col] = _mm512_setzero_ps();
  1524. };
  1525. Unroll<COLS>{}(loadc);
  1526. // Q5_K and Q4_K shares the same vnni formats, refer to notes above.
  1527. auto compute = [&](auto col, auto i) {
  1528. // load a
  1529. if constexpr (col == 0) {
  1530. for (int k_group = 0; k_group < QK_K / 32; ++k_group) {
  1531. va[k_group] = _mm512_castsi256_si512(_mm256_loadu_si256((const __m256i *)(A[0 * KB + i].qs + k_group * 32)));
  1532. }
  1533. const __m256i q8sums = _mm256_loadu_si256((const __m256i *)A[0 * KB + i].bsums);
  1534. const __m128i q8s = _mm_hadd_epi16(_mm256_extracti128_si256(q8sums, 0), _mm256_extracti128_si256(q8sums, 1));
  1535. va_bsum = _mm512_castsi128_si512(q8s);
  1536. vd1 = _mm512_set1_ps(A[0 * KB + i].d);
  1537. }
  1538. // step 1: accumultate the quants
  1539. __m512i acc = _mm512_setzero_si512();
  1540. const char * b_ptr = B + PACKED_INDEX(col, i, KB, TILE_SIZE);
  1541. const char * b_qs = b_ptr;
  1542. const char * b_qh = b_ptr + offset_qh;
  1543. for (int k_group = 0; k_group < QK_K / 32; ++k_group) {
  1544. __m512i vsum = _mm512_setzero_si512();
  1545. __m512i hmask0 = _mm512_set1_epi8(0x1);
  1546. __m512i hmask1 = _mm512_set1_epi8(0x2);
  1547. __m512i hbits = _mm512_loadu_si512((const __m512i *)(b_qh + k_group * 64));
  1548. for (int k = 0; k < 8; k += 2) {
  1549. __m512i va0 = _mm512_permutexvar_epi32(_mm512_set1_epi32(k + 0), va[k_group]);
  1550. __m512i va1 = _mm512_permutexvar_epi32(_mm512_set1_epi32(k + 1), va[k_group]);
  1551. __m512i bytes = _mm512_loadu_si512((const __m512i *)b_qs);
  1552. __m512i vb0 = _mm512_and_si512(bytes, lowMask);
  1553. __m512i vb1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  1554. __m512i vh0 = _mm512_slli_epi16(_mm512_srli_epi16(_mm512_and_si512(hbits, hmask0), k), 4);
  1555. __m512i vh1 = _mm512_slli_epi16(_mm512_srli_epi16(_mm512_and_si512(hbits, hmask1), k + 1), 4);
  1556. hmask0 = _mm512_slli_epi16(hmask0, 2);
  1557. hmask1 = _mm512_slli_epi16(hmask1, 2);
  1558. vb0 = _mm512_add_epi8(vb0, vh0);
  1559. vb1 = _mm512_add_epi8(vb1, vh1);
  1560. vsum = _mm512_dpbusd_epi32(vsum, vb0, va0);
  1561. vsum = _mm512_dpbusd_epi32(vsum, vb1, va1);
  1562. b_qs += 64;
  1563. }
  1564. // vacc += scale * (q8 @ q5)
  1565. const __m512i vscale = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i *)(b_ptr + offset_scales + k_group * TILE_N)));
  1566. acc = _mm512_add_epi32(acc, _mm512_mullo_epi32(vsum, vscale));
  1567. }
  1568. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset_d0)));
  1569. vc[col] = _mm512_fmadd_ps(_mm512_cvtepi32_ps(acc), _mm512_mul_ps(vd0, vd1), vc[col]);
  1570. // step 2: accumulate the mins
  1571. __m512i acc_m = _mm512_setzero_si512();
  1572. for (int k = 0; k < 4; ++k) {
  1573. __m512i vmask = _mm512_set1_epi32(k);
  1574. __m512i va = _mm512_permutexvar_epi32(vmask, va_bsum);
  1575. __m512i vb = _mm512_cvtepi8_epi16(_mm256_loadu_si256((const __m256i *)(b_ptr + offset_mins + k * 32)));
  1576. acc_m = _mm512_dpwssds_epi32(acc_m, va, vb);
  1577. }
  1578. const __m512 vdmin = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset_dmin)));
  1579. vc[col] = _mm512_fnmadd_ps(_mm512_cvtepi32_ps(acc_m), _mm512_mul_ps(vdmin, vd1), vc[col]);
  1580. };
  1581. for (int i = 0; i < KB; ++i) {
  1582. Unroll<COLS>{}(compute, i);
  1583. }
  1584. //store to C
  1585. auto storec = [&](auto col) {
  1586. _mm512_storeu_ps((__m512i*)(C + 0 * ldc + col * 16), vc[col]);
  1587. };
  1588. Unroll<COLS>{}(storec);
  1589. }
  1590. };
  1591. template <int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1592. struct tinygemm_kernel_vnni<block_q8_K, block_q6_K, float, BLOCK_M, BLOCK_N, BLOCK_K> {
  1593. static void apply(int KB, const void * RESTRICT _A, const void * RESTRICT _B, float * RESTRICT C, int ldc) {
  1594. constexpr int COLS = BLOCK_N / 16;
  1595. const int TILE_SIZE = TILE_N * sizeof(block_q6_K);
  1596. const block_q8_K * RESTRICT A = static_cast<const block_q8_K *>(_A);
  1597. const char * RESTRICT B = static_cast<const char *>(_B);
  1598. // load the 256 bytes from A to 4 avx512 vectors
  1599. __m512i va[4];
  1600. __m512 vc[COLS];
  1601. __m512 vd1;
  1602. // packed_B:
  1603. const int offset_qh = (QK_K / 2) * TILE_N;
  1604. const int offset_scales = (QK_K / 2) * TILE_N + (QK_K / 4) * TILE_N;
  1605. const int offset_d0 = (QK_K / 2) * TILE_N + (QK_K / 4) * TILE_N + 16 * TILE_N;
  1606. // compensation
  1607. __m512i vcomp;
  1608. const __m512i m32s = _mm512_set1_epi32(32);
  1609. const __m512i lowMask = _mm512_set1_epi8(0xF);
  1610. auto loadc = [&](auto col) {
  1611. vc[col] = _mm512_setzero_ps();
  1612. };
  1613. Unroll<COLS>{}(loadc);
  1614. auto compute = [&](auto col, auto i) {
  1615. if constexpr (col == 0) {
  1616. // load a
  1617. va[0] = _mm512_loadu_si512((const __m512i *)(A[0 * KB + i].qs + 0));
  1618. va[1] = _mm512_loadu_si512((const __m512i *)(A[0 * KB + i].qs + 64));
  1619. va[2] = _mm512_loadu_si512((const __m512i *)(A[0 * KB + i].qs + 128));
  1620. va[3] = _mm512_loadu_si512((const __m512i *)(A[0 * KB + i].qs + 192));
  1621. const __m256i q8sums = _mm256_loadu_si256((const __m256i *)A[0 * KB + i].bsums);
  1622. vcomp = _mm512_mullo_epi32(_mm512_cvtepi16_epi32(q8sums), m32s);
  1623. vd1 = _mm512_set1_ps(A[0 * KB + i].d);
  1624. }
  1625. // accmulate the quants
  1626. __m512i acc = _mm512_setzero_si512();
  1627. const char * b_ptr = B + PACKED_INDEX(col, i, KB, TILE_SIZE);
  1628. const char * b_qs = b_ptr;
  1629. const char * b_qh = b_ptr + offset_qh;
  1630. int mask = 0;
  1631. for (int k_group = 0; k_group < QK_K / 16; ++k_group) {
  1632. int r = k_group >> 2;
  1633. __m512i va0 = _mm512_permutexvar_epi32(_mm512_set1_epi32(mask++), va[r]);
  1634. __m512i va1 = _mm512_permutexvar_epi32(_mm512_set1_epi32(mask++), va[r]);
  1635. __m512i vsum = _mm512_setzero_si512();
  1636. __m512i hmask = _mm512_set1_epi8(0x3);
  1637. __m512i bytes = _mm512_loadu_si512(b_qs);
  1638. __m512i hbits = _mm512_loadu_si512(b_qh);
  1639. __m512i vb0 = _mm512_and_si512(bytes, lowMask);
  1640. __m512i vb1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  1641. __m512i vh0 = _mm512_slli_epi16(_mm512_and_si512(hbits, hmask), 4);
  1642. __m512i vh1 = _mm512_slli_epi16(_mm512_and_si512(hbits, _mm512_slli_epi16(hmask, 2)), 2);
  1643. vb0 = _mm512_add_epi8(vb0, vh0);
  1644. vb1 = _mm512_add_epi8(vb1, vh1);
  1645. vsum = _mm512_dpbusd_epi32(vsum, vb0, va0);
  1646. vsum = _mm512_dpbusd_epi32(vsum, vb1, va1);
  1647. b_qs += 64;
  1648. va0 = _mm512_permutexvar_epi32(_mm512_set1_epi32(mask++), va[r]);
  1649. va1 = _mm512_permutexvar_epi32(_mm512_set1_epi32(mask++), va[r]);
  1650. bytes = _mm512_loadu_si512(b_qs);
  1651. vb0 = _mm512_and_si512(bytes, lowMask);
  1652. vb1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
  1653. vh0 = _mm512_and_si512(hbits, _mm512_slli_epi16(hmask, 4));
  1654. vh1 = _mm512_srli_epi16(_mm512_and_si512(hbits, _mm512_slli_epi16(hmask, 6)), 2);
  1655. vb0 = _mm512_add_epi8(vb0, vh0);
  1656. vb1 = _mm512_add_epi8(vb1, vh1);
  1657. vsum = _mm512_dpbusd_epi32(vsum, vb0, va0);
  1658. vsum = _mm512_dpbusd_epi32(vsum, vb1, va1);
  1659. b_qs += 64;
  1660. b_qh += 64;
  1661. // B * A - 32 * A
  1662. __m512i vmask = _mm512_set1_epi32(k_group);
  1663. vsum = _mm512_sub_epi32(vsum, _mm512_permutexvar_epi32(vmask, vcomp));
  1664. // vacc += scale * (q8 @ q6)
  1665. const __m512i vscale = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i *)(b_ptr + offset_scales + k_group * TILE_N)));
  1666. acc = _mm512_add_epi32(acc, _mm512_mullo_epi32(vsum, vscale));
  1667. }
  1668. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset_d0)));
  1669. vc[col] = _mm512_fmadd_ps(_mm512_cvtepi32_ps(acc), _mm512_mul_ps(vd0, vd1), vc[col]);
  1670. };
  1671. for (int i = 0; i < KB; ++i) {
  1672. Unroll<COLS>{}(compute, i);
  1673. }
  1674. //store to C
  1675. auto storec = [&](int col) {
  1676. _mm512_storeu_ps((__m512i*)(C + 0 * ldc + col * 16), vc[col]);
  1677. };
  1678. Unroll<COLS>{}(storec);
  1679. }
  1680. };
  1681. template <int BLOCK_M, int BLOCK_N, int BLOCK_K>
  1682. struct tinygemm_kernel_vnni<block_q8_K, block_iq4_xs, float, BLOCK_M, BLOCK_N, BLOCK_K> {
  1683. static void apply(int KB, const void * RESTRICT _A, const void * RESTRICT _B, float * RESTRICT C, int ldc) {
  1684. constexpr int COLS = BLOCK_N / 16;
  1685. const int TILE_SIZE = TILE_N * sizeof(block_iq4_xs) + TILE_N * 2;
  1686. const block_q8_K * RESTRICT A = static_cast<const block_q8_K *>(_A);
  1687. const char * RESTRICT B = static_cast<const char *>(_B);
  1688. // load the 256 bytes from A to 4 avx512 vectors
  1689. __m512i va[4];
  1690. __m512 vc[COLS];
  1691. __m512 vd1;
  1692. // packed_B:
  1693. const int offset_scales = (QK_K / 2) * TILE_N ;
  1694. const int offset_d0 = (QK_K / 2) * TILE_N + 8 * TILE_N;
  1695. // compensation
  1696. __m512i vcomp;
  1697. const __m256i m128s = _mm256_set1_epi16(128);
  1698. const __m512i lowMask = _mm512_set1_epi8(0xF);
  1699. const __m512i values128 = _mm512_set_epi8(
  1700. 113, 89, 69, 53, 38, 25, 13, 1, -10, -22, -35, -49, -65, -83, -104, -127,
  1701. 113, 89, 69, 53, 38, 25, 13, 1, -10, -22, -35, -49, -65, -83, -104, -127,
  1702. 113, 89, 69, 53, 38, 25, 13, 1, -10, -22, -35, -49, -65, -83, -104, -127,
  1703. 113, 89, 69, 53, 38, 25, 13, 1, -10, -22, -35, -49, -65, -83, -104, -127
  1704. );
  1705. const __m512i off = _mm512_set1_epi8(static_cast<char>(0x80));
  1706. const __m512i values256 = _mm512_add_epi8(values128, off);
  1707. auto loadc = [&](auto col) {
  1708. vc[col] = _mm512_setzero_ps();
  1709. };
  1710. Unroll<COLS>{}(loadc);
  1711. auto compute = [&](auto col, auto i) {
  1712. if constexpr (col == 0) {
  1713. // load a
  1714. va[0] = _mm512_loadu_si512((const __m512i *)(A[0 * KB + i].qs + 0));
  1715. va[1] = _mm512_loadu_si512((const __m512i *)(A[0 * KB + i].qs + 64));
  1716. va[2] = _mm512_loadu_si512((const __m512i *)(A[0 * KB + i].qs + 128));
  1717. va[3] = _mm512_loadu_si512((const __m512i *)(A[0 * KB + i].qs + 192));
  1718. // compensation: 128 * A
  1719. const __m256i q8sums = _mm256_loadu_si256((const __m256i *)A[0 * KB + i].bsums);
  1720. vcomp = _mm512_castsi256_si512(_mm256_madd_epi16(q8sums, m128s));
  1721. vd1 = _mm512_set1_ps(A[0 * KB + i].d);
  1722. }
  1723. // accmulate the quants
  1724. __m512i acc = _mm512_setzero_si512();
  1725. const char * b_ptr = B + PACKED_INDEX(col, i, KB, TILE_SIZE);
  1726. const char * b_qs = b_ptr;
  1727. int mask = 0;
  1728. for (int k_group = 0; k_group < QK_K / 32; ++k_group) {
  1729. int r = k_group >> 1;
  1730. __m512i vmask = _mm512_set1_epi32(k_group);
  1731. __m512i vsum = _mm512_setzero_si512();
  1732. for (int k = 0; k < 8; k += 2) {
  1733. __m512i va0 = _mm512_permutexvar_epi32(_mm512_set1_epi32(mask++), va[r]);
  1734. __m512i va1 = _mm512_permutexvar_epi32(_mm512_set1_epi32(mask++), va[r]);
  1735. __m512i bytes = _mm512_loadu_si512(b_qs);
  1736. __m512i vb0 = _mm512_shuffle_epi8(values256, _mm512_and_si512(bytes, lowMask));
  1737. __m512i vb1 = _mm512_shuffle_epi8(values256, _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask));
  1738. vsum = _mm512_dpbusd_epi32(vsum, vb0, va0);
  1739. vsum = _mm512_dpbusd_epi32(vsum, vb1, va1);
  1740. b_qs += 64;
  1741. }
  1742. // (B + 128) * A - 128 * A
  1743. vsum = _mm512_sub_epi32(vsum, _mm512_permutexvar_epi32(vmask, vcomp));
  1744. // vacc += scale * (q8 @ q4)
  1745. const __m512i vscale = _mm512_cvtepi8_epi32(_mm_loadu_si128((const __m128i *)(b_ptr + offset_scales + k_group * TILE_N)));
  1746. acc = _mm512_add_epi32(acc, _mm512_mullo_epi32(vsum, vscale));
  1747. }
  1748. const __m512 vd0 = _mm512_cvtph_ps(_mm256_loadu_si256((const __m256i *)(b_ptr + offset_d0)));
  1749. vc[col] = _mm512_fmadd_ps(_mm512_cvtepi32_ps(acc), _mm512_mul_ps(vd0, vd1), vc[col]);
  1750. };
  1751. for (int i = 0; i < KB; ++i) {
  1752. Unroll<COLS>{}(compute, i);
  1753. }
  1754. //store to C
  1755. auto storec = [&](auto col) {
  1756. _mm512_storeu_ps((__m512i*)(C + 0 * ldc + col * 16), vc[col]);
  1757. };
  1758. Unroll<COLS>{}(storec);
  1759. }
  1760. };
  1761. #define LAUNCH_TINYGEMM_KERNEL_VNNI(NB_SIZE) \
  1762. tinygemm_kernel_vnni<vec_dot_type, type, float, 1, NB_SIZE, blck_size>::apply( \
  1763. KB, (const char *)wdata + 0 * row_size_A, \
  1764. (const char *)src0->data + PACKED_INDEX(nb * kTilesN, 0, KB, TILE_SIZE), \
  1765. (float *) dst->data + 0 * N + nb_start, ldc)
  1766. template <typename TA, typename TB, typename TC, int BLOCK_K,
  1767. typename std::enable_if<!is_type_qkk<TB>::value, int>::type = 0>
  1768. void tinygemm_kernel_amx(int M, int N, int KB, const void * RESTRICT _A, const void * RESTRICT _B, TC * RESTRICT C, int ldc) {
  1769. using packed_B_t = packed_B_type<TB>;
  1770. const int TILE_SIZE = get_tile_size<TB>();
  1771. const bool need_unpack = do_unpack<TB>::value;
  1772. GGML_ASSERT(M <= 2 * TILE_M && N == 2 * TILE_N);
  1773. const TA * RESTRICT A = static_cast<const TA *>(_A);
  1774. const char * RESTRICT B = static_cast<const char *>(_B);
  1775. const int m0 = std::min(M, TILE_M);
  1776. const int m1 = std::max(M - TILE_M, 0);
  1777. const int lda = KB * sizeof(TA);
  1778. //const int ldb = KB * sizeof(TB);
  1779. static thread_local packed_B_t Tile0[TILE_N * TILE_K];
  1780. static thread_local packed_B_t Tile1[TILE_N * TILE_K];
  1781. static thread_local int8_t Tile23[TILE_M * TILE_K];
  1782. static thread_local int32_t TileC0[TILE_M * TILE_N * 4];
  1783. static thread_local int32_t TileC1[TILE_M * TILE_N * 4];
  1784. // double buffering C to interleave avx512 and amx
  1785. int32_t * C_cur = TileC0;
  1786. int32_t * C_pre = TileC1;
  1787. auto Tile4 = [&](int32_t * base) { return base; };
  1788. auto Tile5 = [&](int32_t * base) { return base + TILE_M * TILE_N; };
  1789. auto Tile6 = [&](int32_t * base) { return base + 2 * TILE_M * TILE_N; };
  1790. auto Tile7 = [&](int32_t * base) { return base + 3 * TILE_M * TILE_N; };
  1791. if (M == 2 * TILE_M) {
  1792. // i = 0
  1793. const char * B_blk0 = B + PACKED_INDEX(0, 0, KB, TILE_SIZE);
  1794. const char * B_blk1 = B + PACKED_INDEX(1, 0, KB, TILE_SIZE);
  1795. if (need_unpack) {
  1796. unpack_B<TB>(Tile0, B_blk0);
  1797. _tile_loadd(TMM0, Tile0, TILE_N * VNNI_BLK);
  1798. } else {
  1799. _tile_loadd(TMM0, B_blk0, TILE_N * VNNI_BLK);
  1800. }
  1801. _tile_zero(TMM4);
  1802. _tile_loadd(TMM2, A[0].qs, lda);
  1803. _tile_dpbssd(TMM4, TMM2, TMM0);
  1804. _tile_stored(TMM4, Tile4(C_pre), TILE_N * sizeof(int32_t));
  1805. _tile_zero(TMM5);
  1806. _tile_loadd(TMM3, A[TILE_M * KB + 0].qs, lda);
  1807. _tile_dpbssd(TMM5, TMM3, TMM0);
  1808. _tile_stored(TMM5, Tile5(C_pre), TILE_N * sizeof(int32_t));
  1809. if (need_unpack) {
  1810. unpack_B<TB>(Tile1, B_blk0);
  1811. _tile_loadd(TMM1, Tile1, TILE_N * VNNI_BLK);
  1812. } else {
  1813. _tile_loadd(TMM1, B_blk1, TILE_N * VNNI_BLK);
  1814. }
  1815. _tile_zero(TMM6);
  1816. _tile_dpbssd(TMM6, TMM2, TMM1);
  1817. _tile_stored(TMM6, Tile6(C_pre), TILE_N * sizeof(int32_t));
  1818. _tile_zero(TMM7);
  1819. _tile_dpbssd(TMM7, TMM3, TMM1);
  1820. _tile_stored(TMM7, Tile7(C_pre), TILE_N * sizeof(int32_t));
  1821. for (int i = 1; i < KB; ++i) {
  1822. // index of previous iter
  1823. const int ii = i - 1;
  1824. const char * B_blk0 = B + PACKED_INDEX(0, i, KB, TILE_SIZE);
  1825. const char * B_blk1 = B + PACKED_INDEX(1, i, KB, TILE_SIZE);
  1826. GGML_DISPATCH_BOOL(ii > 0, is_acc, [&] {
  1827. if (need_unpack) {
  1828. unpack_B<TB>(Tile0, B_blk0);
  1829. _tile_loadd(TMM0, Tile0, TILE_N * VNNI_BLK);
  1830. } else {
  1831. _tile_loadd(TMM0, B_blk0, TILE_N * VNNI_BLK);
  1832. }
  1833. _tile_zero(TMM4);
  1834. _tile_loadd(TMM2, A[i].qs, lda);
  1835. acc_C<TA, TB, is_acc>::apply(C, ldc, Tile4(C_pre), &A[ii], KB, B + PACKED_INDEX(0, ii, KB, TILE_SIZE), TILE_M);
  1836. _tile_dpbssd(TMM4, TMM2, TMM0);
  1837. _tile_stored(TMM4, Tile4(C_cur), TILE_N * sizeof(int32_t));
  1838. _tile_zero(TMM5);
  1839. _tile_loadd(TMM3, A[TILE_M * KB + i].qs, lda);
  1840. acc_C<TA, TB, is_acc>::apply(C + TILE_M * ldc, ldc, Tile5(C_pre), &A[TILE_M * KB + ii], KB, B + PACKED_INDEX(0, ii, KB, TILE_SIZE), TILE_M);
  1841. _tile_dpbssd(TMM5, TMM3, TMM0);
  1842. _tile_stored(TMM5, Tile5(C_cur), TILE_N * sizeof(int32_t));
  1843. if (need_unpack) {
  1844. unpack_B<TB>(Tile1, B_blk1);
  1845. _tile_loadd(TMM1, Tile1, TILE_N * VNNI_BLK);
  1846. } else {
  1847. _tile_loadd(TMM1, B_blk1, TILE_N * VNNI_BLK);
  1848. }
  1849. _tile_zero(TMM6);
  1850. acc_C<TA, TB, is_acc>::apply(C + TILE_N, ldc, Tile6(C_pre), &A[ii], KB, B + PACKED_INDEX(1, ii, KB, TILE_SIZE), TILE_M);
  1851. _tile_dpbssd(TMM6, TMM2, TMM1);
  1852. _tile_stored(TMM6, Tile6(C_cur), TILE_N * sizeof(int32_t));
  1853. _tile_zero(TMM7);
  1854. acc_C<TA, TB, is_acc>::apply(C + TILE_M * ldc + TILE_N, ldc, Tile7(C_pre), &A[TILE_M * KB + ii], KB, B + PACKED_INDEX(1, ii, KB, TILE_SIZE), TILE_M);
  1855. _tile_dpbssd(TMM7, TMM3, TMM1);
  1856. _tile_stored(TMM7, Tile7(C_cur), TILE_N * sizeof(int32_t));
  1857. std::swap(C_cur, C_pre);
  1858. });
  1859. }
  1860. // final accumulation
  1861. {
  1862. int ii = KB - 1;
  1863. acc_C<TA, TB, true>::apply(C, ldc, Tile4(C_pre), &A[ii], KB, B + PACKED_INDEX(0, ii, KB, TILE_SIZE), TILE_M);
  1864. acc_C<TA, TB, true>::apply(C + TILE_M * ldc, ldc, Tile5(C_pre), &A[TILE_M * KB + ii], KB, B + PACKED_INDEX(0, ii, KB, TILE_SIZE), TILE_M);
  1865. acc_C<TA, TB, true>::apply(C + TILE_N, ldc, Tile6(C_pre), &A[ii], KB, B + PACKED_INDEX(1, ii, KB, TILE_SIZE), TILE_M);
  1866. acc_C<TA, TB, true>::apply(C + TILE_M * ldc + TILE_N, ldc, Tile7(C_pre), &A[TILE_M * KB + ii], KB, B + PACKED_INDEX(1, ii, KB, TILE_SIZE), TILE_M);
  1867. }
  1868. } else {
  1869. for (int i = 0; i < KB; ++i) {
  1870. _tile_zero(TMM4);
  1871. _tile_zero(TMM6);
  1872. if (m1 != 0) {
  1873. _tile_zero(TMM5);
  1874. _tile_zero(TMM7);
  1875. }
  1876. const char * B_blk0 = B + PACKED_INDEX(0, i, KB, TILE_SIZE);
  1877. const char * B_blk1 = B + PACKED_INDEX(1, i, KB, TILE_SIZE);
  1878. if (need_unpack) {
  1879. unpack_B<TB>(Tile0, B_blk0);
  1880. _tile_loadd(TMM0, Tile0, TILE_N * VNNI_BLK);
  1881. } else {
  1882. _tile_loadd(TMM0, B_blk0, TILE_N * VNNI_BLK);
  1883. }
  1884. if (need_unpack) {
  1885. unpack_B<TB>(Tile1, B_blk1);
  1886. _tile_loadd(TMM1, Tile1, TILE_N * VNNI_BLK);
  1887. } else {
  1888. _tile_loadd(TMM1, B_blk1, TILE_N * VNNI_BLK);
  1889. }
  1890. if (m0 == TILE_M) {
  1891. _tile_loadd(TMM2, A[i].qs, lda);
  1892. } else {
  1893. unpack_A(Tile23, &A[i], KB, m0);
  1894. _tile_loadd(TMM2, Tile23, TILE_K);
  1895. }
  1896. _tile_dpbssd(TMM4, TMM2, TMM0);
  1897. _tile_dpbssd(TMM6, TMM2, TMM1);
  1898. _tile_stored(TMM4, Tile4(C_cur), TILE_N * sizeof(int32_t));
  1899. _tile_stored(TMM6, Tile6(C_cur), TILE_N * sizeof(int32_t));
  1900. GGML_DISPATCH_BOOL(i > 0, is_acc, [&] {
  1901. acc_C<TA, TB, is_acc>::apply(C, ldc, Tile4(C_cur), &A[i], KB, B + PACKED_INDEX(0, i, KB, TILE_SIZE), m0);
  1902. acc_C<TA, TB, is_acc>::apply(C + TILE_N, ldc, Tile6(C_cur), &A[i], KB, B + PACKED_INDEX(1, i, KB, TILE_SIZE), m0);
  1903. });
  1904. if (m1 != 0) {
  1905. unpack_A(Tile23, &A[TILE_M * KB + i], KB, m1);
  1906. _tile_loadd(TMM3, Tile23, TILE_K);
  1907. _tile_dpbssd(TMM5, TMM3, TMM0);
  1908. _tile_dpbssd(TMM7, TMM3, TMM1);
  1909. _tile_stored(TMM5, Tile5(C_cur), TILE_N * sizeof(int32_t));
  1910. _tile_stored(TMM7, Tile7(C_cur), TILE_N * sizeof(int32_t));
  1911. GGML_DISPATCH_BOOL(i > 0, is_acc, [&] {
  1912. acc_C<TA, TB, is_acc>::apply(C + TILE_M * ldc, ldc, Tile5(C_cur), &A[TILE_M * KB + i], KB, B + PACKED_INDEX(0, i, KB, TILE_SIZE), m1);
  1913. acc_C<TA, TB, is_acc>::apply(C + TILE_M * ldc + TILE_N, ldc, Tile7(C_cur), &A[TILE_M * KB + i], KB, B + PACKED_INDEX(1, i, KB, TILE_SIZE), m1);
  1914. });
  1915. }
  1916. }
  1917. }
  1918. return;
  1919. }
  1920. template <typename TA, typename TB, typename TC, int BLOCK_K,
  1921. typename std::enable_if<is_type_qkk<TB>::value, int>::type = 0>
  1922. void tinygemm_kernel_amx(int M, int N, int KB, const void * RESTRICT _A, const void * RESTRICT _B, float * RESTRICT C, int ldc) {
  1923. static_assert(std::is_same<TA, block_q8_K>::value);
  1924. const int TILE_SIZE = get_tile_size<TB>();
  1925. GGML_ASSERT(M <= 2 * TILE_M && N == 2 * TILE_N);
  1926. const TA * RESTRICT A = static_cast<const TA *>(_A);
  1927. const char * RESTRICT B = static_cast<const char *>(_B);
  1928. const int m0 = std::min(M, TILE_M);
  1929. const int m1 = std::max(M - TILE_M, 0);
  1930. //const int lda = KB * sizeof(TA);
  1931. static thread_local int8_t Tile0[TILE_N * TILE_K];
  1932. static thread_local int8_t Tile1[TILE_N * TILE_K];
  1933. static thread_local int8_t Tile23[TILE_M * TILE_K];
  1934. // mat mul result for each group
  1935. static thread_local int32_t Tile4[TILE_M * TILE_N];
  1936. static thread_local int32_t Tile5[TILE_M * TILE_N];
  1937. static thread_local int32_t Tile6[TILE_M * TILE_N];
  1938. static thread_local int32_t Tile7[TILE_M * TILE_N];
  1939. // sum of each QK_K block, contains 8 groups, int32
  1940. static thread_local int32_t Sumi4[TILE_M * TILE_N];
  1941. static thread_local int32_t Sumi5[TILE_M * TILE_N];
  1942. static thread_local int32_t Sumi6[TILE_M * TILE_N];
  1943. static thread_local int32_t Sumi7[TILE_M * TILE_N];
  1944. const int k_group_size = std::is_same<TB, block_q6_K>::value ? 16 : 32;
  1945. for (int i = 0; i < KB; ++i) {
  1946. // step 1: accumulate the quants across 8 groups, each group with 32
  1947. for (int k = 0; k < QK_K / k_group_size; ++k) {
  1948. GGML_DISPATCH_BOOL(k > 0, is_acc, [&] {
  1949. _tile_zero(TMM4);
  1950. _tile_zero(TMM6);
  1951. unpack_B<TB>(Tile0, B + PACKED_INDEX(0, i, KB, TILE_SIZE), k);
  1952. _tile_loadd(TMM0, Tile0, TILE_N * VNNI_BLK);
  1953. unpack_B<TB>(Tile1, B + PACKED_INDEX(1, i, KB, TILE_SIZE), k);
  1954. _tile_loadd(TMM1, Tile1, TILE_N * VNNI_BLK);
  1955. unpack_A<TB>(Tile23, &A[i], KB, k, m0);
  1956. _tile_loadd(TMM2, Tile23, TILE_K);
  1957. _tile_dpbssd(TMM4, TMM2, TMM0);
  1958. _tile_dpbssd(TMM6, TMM2, TMM1);
  1959. _tile_stored(TMM4, Tile4, TILE_N * sizeof(int32_t));
  1960. _tile_stored(TMM6, Tile6, TILE_N * sizeof(int32_t));
  1961. scale_C<TB, is_acc>(Tile4, Sumi4, B + PACKED_INDEX(0, i, KB, TILE_SIZE), k, m0);
  1962. scale_C<TB, is_acc>(Tile6, Sumi6, B + PACKED_INDEX(1, i, KB, TILE_SIZE), k, m0);
  1963. if (m1 != 0) {
  1964. _tile_zero(TMM5);
  1965. _tile_zero(TMM7);
  1966. unpack_A<TB>(Tile23, &A[TILE_M * KB + i], KB, k, m1);
  1967. _tile_loadd(TMM3, Tile23, TILE_K);
  1968. _tile_dpbssd(TMM5, TMM3, TMM0);
  1969. _tile_dpbssd(TMM7, TMM3, TMM1);
  1970. _tile_stored(TMM5, Tile5, TILE_N * sizeof(int32_t));
  1971. _tile_stored(TMM7, Tile7, TILE_N * sizeof(int32_t));
  1972. scale_C<TB, is_acc>(Tile5, Sumi5, B + PACKED_INDEX(0, i, KB, TILE_SIZE), k, m1);
  1973. scale_C<TB, is_acc>(Tile7, Sumi7, B + PACKED_INDEX(1, i, KB, TILE_SIZE), k, m1);
  1974. }
  1975. });
  1976. }
  1977. // step 2: accmulate the mins
  1978. GGML_DISPATCH_BOOL(i > 0, is_acc, [&] {
  1979. acc_C<TA, TB, is_acc>::apply(C, ldc, Sumi4, &A[i], KB, B + PACKED_INDEX(0, i, KB, TILE_SIZE), m0);
  1980. acc_C<TA, TB, is_acc>::apply(C + TILE_N, ldc, Sumi6, &A[i], KB, B + PACKED_INDEX(1, i, KB, TILE_SIZE), m0);
  1981. if (m1 != 0) {
  1982. acc_C<TA, TB, is_acc>::apply(C + TILE_M * ldc, ldc, Sumi5, &A[TILE_M * KB + i], KB, B + PACKED_INDEX(0, i, KB, TILE_SIZE), m1);
  1983. acc_C<TA, TB, is_acc>::apply(C + TILE_M * ldc + TILE_N, ldc, Sumi7, &A[TILE_M * KB + i], KB, B + PACKED_INDEX(1, i, KB, TILE_SIZE), m1);
  1984. }
  1985. });
  1986. }
  1987. return;
  1988. }
  1989. } // anonymous namespace
  1990. // get the packed tensor size for quantized weights
  1991. size_t ggml_backend_amx_get_alloc_size(const struct ggml_tensor * tensor) {
  1992. const enum ggml_type TYPE = tensor->type;
  1993. const int K = tensor->ne[0]; // ne0: in_features
  1994. const int N = tensor->ne[1]; // ne1: out_features
  1995. auto get_tensor_size = [&] {
  1996. size_t row_size_B{0};
  1997. GGML_DISPATCH_QTYPES(TYPE, [&] {
  1998. row_size_B = get_row_size<type, blck_size>(K);
  1999. });
  2000. return N * row_size_B;
  2001. };
  2002. if (qtype_has_amx_kernels(TYPE)) {
  2003. return get_tensor_size();
  2004. } else {
  2005. // for f16, bf16 we don't do packing
  2006. return ggml_nbytes(tensor);
  2007. }
  2008. }
  2009. // pack weight to vnni format
  2010. void ggml_backend_amx_convert_weight(struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
  2011. GGML_ASSERT(offset == 0 && size == ggml_nbytes(tensor)); // only full tensor conversion is supported for now
  2012. const enum ggml_type TYPE = tensor->type;
  2013. const int K = tensor->ne[0]; // ne0: in_features
  2014. const int N = tensor->ne[1]; // ne1: out_features
  2015. GGML_DISPATCH_QTYPES(TYPE, [&] {
  2016. convert_B_packed_format<type, blck_size>((void *)((char *)tensor->data + offset), (const type *)data, N, K);
  2017. });
  2018. }
  2019. size_t ggml_backend_amx_desired_wsize(const struct ggml_tensor * dst) {
  2020. struct ggml_tensor * src0 = dst->src[0];
  2021. const enum ggml_type TYPE = src0->type;
  2022. const bool is_floating_type = TYPE == GGML_TYPE_F16;
  2023. if (is_floating_type) {
  2024. return 0;
  2025. }
  2026. const int M = dst->ne[1];
  2027. const int K = src0->ne[0];
  2028. size_t desired_wsize = 0;
  2029. GGML_DISPATCH_QTYPES(TYPE, [&] {
  2030. const size_t row_size_A = K / blck_size * sizeof(vec_dot_type);
  2031. desired_wsize = M * row_size_A;
  2032. });
  2033. return desired_wsize;
  2034. }
  2035. // NB: mixed dtype gemm with Advanced Matrix Extensions (Intel AMX)
  2036. //
  2037. // src0: weight in shape of {N, K}, quantized
  2038. // src1: input in shape of {M, K}, float32
  2039. // dst: output in shape of {M, N}, float32
  2040. //
  2041. // the function performs: dst = src1 @ src0.T
  2042. //
  2043. void ggml_backend_amx_mul_mat(const ggml_compute_params * params, struct ggml_tensor * dst) {
  2044. struct ggml_tensor * src0 = dst->src[0];
  2045. struct ggml_tensor * src1 = dst->src[1];
  2046. const enum ggml_type TYPE = src0->type;
  2047. // f16 only has avx512 kernels for now,
  2048. // amx kernels will be added once 6th gen xeon is released.
  2049. const bool is_floating_type = TYPE == GGML_TYPE_F16;
  2050. const int M = dst->ne[1];
  2051. const int N = dst->ne[0];
  2052. const int K = src0->ne[0];
  2053. const int ldc = dst->nb[1] / dst->nb[0];
  2054. if (is_floating_type) {
  2055. constexpr int BLOCK_M = 4;
  2056. constexpr int BLOCK_N = 6;
  2057. const int MB = div_up(M, BLOCK_M);
  2058. const int NB = div_up(N, BLOCK_N);
  2059. parallel_for_ggml(params, MB * NB, [&](int begin, int end) {
  2060. GGML_DISPATCH_FLOATING_TYPES(TYPE, [&] {
  2061. for (int i = begin; i < end; ++i) {
  2062. int mb = i / NB;
  2063. int nb = i % NB;
  2064. int mb_start = mb * BLOCK_M;
  2065. int mb_size = std::min(BLOCK_M, M - mb_start);
  2066. int nb_start = nb * BLOCK_N;
  2067. int nb_size = std::min(BLOCK_N, N - nb_start);
  2068. switch (mb_size << 4 | nb_size) {
  2069. case 0x12: LAUNCH_TINYGEMM_KERNEL_AVX(1, 2); break;
  2070. case 0x14: LAUNCH_TINYGEMM_KERNEL_AVX(1, 4); break;
  2071. case 0x16: LAUNCH_TINYGEMM_KERNEL_AVX(1, 6); break;
  2072. case 0x22: LAUNCH_TINYGEMM_KERNEL_AVX(2, 2); break;
  2073. case 0x24: LAUNCH_TINYGEMM_KERNEL_AVX(2, 4); break;
  2074. case 0x26: LAUNCH_TINYGEMM_KERNEL_AVX(2, 6); break;
  2075. case 0x32: LAUNCH_TINYGEMM_KERNEL_AVX(3, 2); break;
  2076. case 0x34: LAUNCH_TINYGEMM_KERNEL_AVX(3, 4); break;
  2077. case 0x36: LAUNCH_TINYGEMM_KERNEL_AVX(3, 6); break;
  2078. case 0x42: LAUNCH_TINYGEMM_KERNEL_AVX(4, 2); break;
  2079. case 0x44: LAUNCH_TINYGEMM_KERNEL_AVX(4, 4); break;
  2080. case 0x46: LAUNCH_TINYGEMM_KERNEL_AVX(4, 6); break;
  2081. default: fprintf(stderr, "Unexpected block size!\n");
  2082. }
  2083. }
  2084. });
  2085. });
  2086. return;
  2087. }
  2088. // pointer to work space, used convert A from float to quantized type
  2089. void * wdata = params->wdata;
  2090. //TODO: performance improvement: merge quant A
  2091. if (params->ith == 0) {
  2092. GGML_DISPATCH_QTYPES(TYPE, [&] {
  2093. const size_t row_size_A = K / blck_size * sizeof(vec_dot_type);
  2094. const size_t desired_wsize = M * row_size_A;
  2095. if (params->wsize < desired_wsize) {
  2096. GGML_ABORT("insufficient work space size");
  2097. }
  2098. // Q4_0, Q4_1, Q8_0 handles 1 TILE_K per blck_size
  2099. // Q4_K, Q5_K, Q6_K, IQ4_XS handles 8 TILE_K per blck_size
  2100. GGML_ASSERT(TILE_K == blck_size || TILE_K * 8 == blck_size);
  2101. const float * A_data = static_cast<const float *>(src1->data);
  2102. for (int m = 0; m < M; ++m) {
  2103. from_float<vec_dot_type>(A_data + m * K, (char *)wdata + m * row_size_A, K);
  2104. }
  2105. });
  2106. }
  2107. ggml_barrier(params->threadpool);
  2108. if (M == 1) {
  2109. // MB = 1 and handle 8 tiles in each block
  2110. constexpr int kTilesN = 4;
  2111. constexpr int BLOCK_N = TILE_N * kTilesN;
  2112. const int NB = div_up(N, BLOCK_N);
  2113. parallel_for_ggml(params, NB, [&](int begin, int end) {
  2114. GGML_DISPATCH_QTYPES(TYPE, [&] {
  2115. const int KB = K / blck_size;
  2116. const int TILE_SIZE = get_tile_size<type>();
  2117. const int row_size_A = KB * sizeof(vec_dot_type);
  2118. for (int i = begin; i < end; ++i) {
  2119. int nb = i;
  2120. int nb_start = nb * BLOCK_N;
  2121. int nb_size = std::min(BLOCK_N, N - nb_start); // 32, 64, 96
  2122. switch (nb_size) {
  2123. //case 160: LAUNCH_TINYGEMM_KERNEL_VNNI(160); break;
  2124. case 128: LAUNCH_TINYGEMM_KERNEL_VNNI(128); break;
  2125. case 96: LAUNCH_TINYGEMM_KERNEL_VNNI(96); break;
  2126. case 64: LAUNCH_TINYGEMM_KERNEL_VNNI(64); break;
  2127. case 32: LAUNCH_TINYGEMM_KERNEL_VNNI(32); break;
  2128. default: fprintf(stderr, "Unexpected n block size!\n");
  2129. }
  2130. }
  2131. });
  2132. });
  2133. return;
  2134. }
  2135. // handle 4 tiles at a tile
  2136. constexpr int BLOCK_M = TILE_M * 2;
  2137. constexpr int BLOCK_N = TILE_N * 2;
  2138. const int MB = div_up(M, BLOCK_M);
  2139. const int NB = div_up(N, BLOCK_N);
  2140. parallel_for_ggml(params, MB * NB, [&](int begin, int end) {
  2141. // init tile config for each thread
  2142. ggml_tile_config_init();
  2143. GGML_DISPATCH_QTYPES(TYPE, [&] {
  2144. const int KB = K / blck_size;
  2145. const int TILE_SIZE = get_tile_size<type>();
  2146. const int row_size_A = KB * sizeof(vec_dot_type);
  2147. for (int i = begin; i < end; ++i) {
  2148. int mb = i / NB;
  2149. int nb = i % NB;
  2150. int mb_start = mb * BLOCK_M;
  2151. int mb_size = std::min(BLOCK_M, M - mb_start);
  2152. int nb_start = nb * BLOCK_N;
  2153. int nb_size = BLOCK_N;
  2154. tinygemm_kernel_amx<vec_dot_type, type, float, blck_size>(
  2155. mb_size, nb_size, KB,
  2156. (const char *)wdata + mb_start * row_size_A,
  2157. (const char *)src0->data + PACKED_INDEX(nb * 2, 0, KB, TILE_SIZE),
  2158. (float *) dst->data + mb_start * N + nb_start, ldc);
  2159. }
  2160. });
  2161. });
  2162. }
  2163. #endif // if defined(__AMX_INT8__) && defined(__AVX512VNNI__)