mmq.cpp 106 KB

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