-
Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathmmq.cpp
2522 lines (2147 loc) · 105 KB
/
mmq.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#if defined(__GNUC__)
#pragma GCC diagnostic ignored "-Wpedantic"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#endif
#include "amx.h"
#include "mmq.h"
#include "ggml-impl.h"
#include "ggml-cpu-impl.h"
#include "ggml-cpu-quants.h"
#include "ggml-quants.h"
#include <algorithm>
#include <type_traits>
#if defined(__gnu_linux__)
#include <sys/syscall.h>
#include <unistd.h>
#endif
#if defined(_OPENMP)
#include <omp.h>
#endif
#if (defined(_WIN32) || defined(_WIN64))
#define RESTRICT __restrict
#else
#define RESTRICT __restrict__
#endif
#if (defined(_WIN32) || defined(_WIN64))
#define ALWAYS_INLINE __forceinline
#elif __has_attribute(always_inline) || defined(__GNUC__)
#define ALWAYS_INLINE __attribute__((__always_inline__)) inline
#else
#define ALWAYS_INLINE inline
#endif
#if defined(__AMX_INT8__) && defined(__AVX512VNNI__)
namespace {
// Forced unrolling
template <int n>
struct Unroll {
template <typename Func, typename... Args>
ALWAYS_INLINE void operator()(const Func& f, Args... args) const {
Unroll<n - 1>{}(f, args...);
f(std::integral_constant<int, n - 1>{}, args...);
}
};
template <>
struct Unroll<1> {
template <typename Func, typename... Args>
ALWAYS_INLINE void operator()(const Func& f, Args... args) const {
f(std::integral_constant<int, 0>{}, args...);
}
};
// type traits
template <typename T> struct PackedTypes {};
template <> struct PackedTypes<block_q4_0> { using type = int8_t; };
template <> struct PackedTypes<block_q4_1> { using type = uint8_t; };
template <> struct PackedTypes<block_q8_0> { using type = int8_t; };
template <typename T> using packed_B_type = typename PackedTypes<T>::type;
template <typename T>
struct do_compensate : std::integral_constant<bool,
std::is_same<T, block_q8_0>::value> {};
template <typename T>
struct do_unpack : std::integral_constant<bool,
std::is_same<T, block_q4_0>::value ||
std::is_same<T, block_q4_1>::value> {};
template <typename T>
struct is_type_qkk : std::integral_constant<bool,
std::is_same<T, block_q4_K>::value ||
std::is_same<T, block_q5_K>::value ||
std::is_same<T, block_q6_K>::value ||
std::is_same<T, block_iq4_xs>::value> {};
#define GGML_DISPATCH_FLOATING_TYPES(TYPE, ...) \
[&] { \
switch (TYPE) { \
case GGML_TYPE_F16: { \
using type = ggml_fp16_t; \
constexpr int blck_size = 16; \
return __VA_ARGS__(); \
} \
case GGML_TYPE_BF16: { \
using type = ggml_bf16_t; \
constexpr int blck_size = 32; \
return __VA_ARGS__(); \
} \
default: \
fprintf(stderr, "Unsupported floating data type\n"); \
} \
}()
#define GGML_DISPATCH_QTYPES(QT, ...) \
[&] { \
switch (QT) { \
case GGML_TYPE_Q4_0: { \
using type = block_q4_0; \
using vec_dot_type = block_q8_0; \
constexpr int blck_size = QK4_0; \
return __VA_ARGS__(); \
} \
case GGML_TYPE_Q4_1: { \
using type = block_q4_1; \
using vec_dot_type = block_q8_1; \
constexpr int blck_size = QK4_1; \
return __VA_ARGS__(); \
} \
case GGML_TYPE_Q8_0: { \
using type = block_q8_0; \
using vec_dot_type = block_q8_0; \
constexpr int blck_size = QK8_0; \
return __VA_ARGS__(); \
} \
case GGML_TYPE_Q4_K: { \
using type = block_q4_K; \
using vec_dot_type = block_q8_K; \
constexpr int blck_size = QK_K; \
return __VA_ARGS__(); \
} \
case GGML_TYPE_Q5_K: { \
using type = block_q5_K; \
using vec_dot_type = block_q8_K; \
constexpr int blck_size = QK_K; \
return __VA_ARGS__(); \
} \
case GGML_TYPE_Q6_K: { \
using type = block_q6_K; \
using vec_dot_type = block_q8_K; \
constexpr int blck_size = QK_K; \
return __VA_ARGS__(); \
} \
case GGML_TYPE_IQ4_XS: { \
using type = block_iq4_xs; \
using vec_dot_type = block_q8_K; \
constexpr int blck_size = QK_K; \
return __VA_ARGS__(); \
} \
default: \
fprintf(stderr, "Unsupported quantized data type: %d\n", int(TYPE)); \
} \
}()
#define GGML_DISPATCH_BOOL(BOOL_V, BOOL_NAME, ...) \
[&] { \
if (BOOL_V) { \
constexpr bool BOOL_NAME = true; \
return __VA_ARGS__(); \
} else { \
constexpr bool BOOL_NAME = false; \
return __VA_ARGS__(); \
} \
}()
// define amx tile config data structure
struct tile_config_t{
uint8_t palette_id = 0;
uint8_t start_row = 0;
uint8_t reserved_0[14] = {0};
uint16_t colsb[16] = {0};
uint8_t rows[16] = {0};
};
// Notes: amx tile config
//
// Typically, TMUL calculates A and B of size 16 x 64 containing INT8 values,
// and accumulate the result to a 16 x 16 matrix C containing INT32 values,
//
// As many GGUF quantized types as `block_size` of 32, so a 16-16-32 config is used
// instead of the normally used 16-16-64 config.
//
// Block A: {16, 32}, dtype = int8_t
// Block B: {16, 32}, dtype = uint8_t/int8_t
// Block C: {16, 16}, dtype = int32_t
//
// Block B needs to be prepacked to vnni format before feeding into TMUL:
// packed_B: from {n, k} to {k/vnni_blk, n, vnni_blck}, viewed in 2d, we get {8, 64}
//
// Therefore, we get tileconfig:
// A B C
// rows 16 8 16
// colsb 32 64 16
//
// For tile distribution, follow a 2-2-4 pattern, e.g. A used TMM2-TMM3, B used TMM0-TMM1,
// C used TMM4-TMM7:
// B TMM0 B TMM1
// A TMM2 C TMM4 C TMM6
// A TMM3 C TMM5 C TMM7
//
// Each `amx` kernel handles 4 blocks at a time: 2MB * 2NB, when m < 2 * BLOCK_M, unpack A
// will be needed.
//
// Here another commonly used pattern 1-3-3 is skipped, as it is mostly used when m <=16;
// and the sinlge batch gemm (m=1) has a special fast path with `avx512-vnni`.
//
// ref: https://www.intel.com/content/www/us/en/developer/articles/code-sample/
// advanced-matrix-extensions-intrinsics-functions.html
//
#define TC_CONFIG_TILE(i, r, cb) tc.rows[i] = r; tc.colsb[i] = cb
void ggml_tile_config_init(void) {
static thread_local bool is_first_time = true;
if (!is_first_time) {
return;
}
static thread_local tile_config_t tc;
tile_config_t current_tc;
_tile_storeconfig(¤t_tc);
// load only when config changes
if (tc.palette_id == 0 || (memcmp(¤t_tc.colsb, &tc.colsb, sizeof(uint16_t) * 8) != 0 &&
memcmp(¤t_tc.rows, &tc.rows, sizeof(uint8_t) * 8) != 0)) {
tc.palette_id = 1;
tc.start_row = 0;
TC_CONFIG_TILE(TMM0, 8, 64);
TC_CONFIG_TILE(TMM1, 8, 64);
TC_CONFIG_TILE(TMM2, 16, 32);
TC_CONFIG_TILE(TMM3, 16, 32);
TC_CONFIG_TILE(TMM4, 16, 64);
TC_CONFIG_TILE(TMM5, 16, 64);
TC_CONFIG_TILE(TMM6, 16, 64);
TC_CONFIG_TILE(TMM7, 16, 64);
_tile_loadconfig(&tc);
}
is_first_time = false;
}
// we need an extra 16 * 4B (TILE_N * int32_t) for each NB/KB block for compensation.
// See the notes `s8s8 igemm compensation in avx512-vnni` for detail.
template <typename TB>
int get_tile_size() {
int tile_size = TILE_N * sizeof(TB);
if (do_compensate<TB>::value) {
tile_size += TILE_N * sizeof(int32_t);
}
if (std::is_same<TB, block_q4_K>::value ||
std::is_same<TB, block_q5_K>::value) {
tile_size += TILE_N * 4;
}
if (std::is_same<TB, block_iq4_xs>::value) {
tile_size += TILE_N * 2;
}
return tile_size;
}
template <typename TB, int BLOCK_K>
int get_row_size(int K) {
int KB = K / BLOCK_K;
int row_size = KB * sizeof(TB);
if (do_compensate<TB>::value) {
row_size += KB * sizeof(int32_t);
}
if (std::is_same<TB, block_q4_K>::value ||
std::is_same<TB, block_q5_K>::value) {
row_size += KB * 4;
}
if (std::is_same<TB, block_iq4_xs>::value) {
row_size += KB * 2;
}
return row_size;
}
// vectorized dtype conversion
inline float FP16_TO_FP32(ggml_half val) {
__m256i v = _mm256_setr_epi16(
val, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
__m512 o = _mm512_cvtph_ps(v);
return _mm512_cvtss_f32(o);
}
inline __m512 FP16_TO_FP32_VEC(ggml_half val) {
__m256i v = _mm256_set1_epi16(val);
return _mm512_cvtph_ps(v);
}
// horizontal reduce
inline float _mm512_reduce_max_ps(const __m512 x) {
__m512 v = x;
__m512 v1 = _mm512_shuffle_f32x4(v, v, 0x4E);
v = _mm512_max_ps(v, v1);
v1 = _mm512_shuffle_f32x4(v, v, 0xB1);
v = _mm512_max_ps(v, v1);
v1 = _mm512_shuffle_ps(v, v, 0x4E);
v = _mm512_max_ps(v, v1);
v1 = _mm512_shuffle_ps(v, v, 0xB1);
v = _mm512_max_ps(v, v1);
return _mm512_cvtss_f32(v);
}
// transpose utils
#define SHUFFLE_EPI32(a, b, mask) \
_mm256_castps_si256(_mm256_shuffle_ps(_mm256_castsi256_ps(a), _mm256_castsi256_ps(b), mask))
inline void transpose_8x8_32bit(__m256i * v, __m256i * v1) {
// unpacking and 32-bit elements
v1[0] = _mm256_unpacklo_epi32(v[0], v[1]);
v1[1] = _mm256_unpackhi_epi32(v[0], v[1]);
v1[2] = _mm256_unpacklo_epi32(v[2], v[3]);
v1[3] = _mm256_unpackhi_epi32(v[2], v[3]);
v1[4] = _mm256_unpacklo_epi32(v[4], v[5]);
v1[5] = _mm256_unpackhi_epi32(v[4], v[5]);
v1[6] = _mm256_unpacklo_epi32(v[6], v[7]);
v1[7] = _mm256_unpackhi_epi32(v[6], v[7]);
// shuffling the 32-bit elements
v[0] = SHUFFLE_EPI32(v1[0], v1[2], 0x44);
v[1] = SHUFFLE_EPI32(v1[0], v1[2], 0xee);
v[2] = SHUFFLE_EPI32(v1[4], v1[6], 0x44);
v[3] = SHUFFLE_EPI32(v1[4], v1[6], 0xee);
v[4] = SHUFFLE_EPI32(v1[1], v1[3], 0x44);
v[5] = SHUFFLE_EPI32(v1[1], v1[3], 0xee);
v[6] = SHUFFLE_EPI32(v1[5], v1[7], 0x44);
v[7] = SHUFFLE_EPI32(v1[5], v1[7], 0xee);
// shuffling 128-bit elements
v1[0] = _mm256_permute2f128_si256(v[2], v[0], 0x02);
v1[1] = _mm256_permute2f128_si256(v[3], v[1], 0x02);
v1[2] = _mm256_permute2f128_si256(v[6], v[4], 0x02);
v1[3] = _mm256_permute2f128_si256(v[7], v[5], 0x02);
v1[4] = _mm256_permute2f128_si256(v[2], v[0], 0x13);
v1[5] = _mm256_permute2f128_si256(v[3], v[1], 0x13);
v1[6] = _mm256_permute2f128_si256(v[6], v[4], 0x13);
v1[7] = _mm256_permute2f128_si256(v[7], v[5], 0x13);
}
inline void transpose_16x4_32bit(__m512i * r, __m512i * d) {
static const __m512i index1 = _mm512_set_epi32(
0x0f, 0x0b, 0x07, 0x03,
0x0e, 0x0a, 0x06, 0x02,
0x0d, 0x09, 0x05, 0x01,
0x0c, 0x08, 0x04, 0x00);
d[0] = _mm512_permutexvar_epi32(index1, r[0]);
d[1] = _mm512_permutexvar_epi32(index1, r[1]);
d[2] = _mm512_permutexvar_epi32(index1, r[2]);
d[3] = _mm512_permutexvar_epi32(index1, r[3]);
r[0] = _mm512_shuffle_i32x4(d[0], d[1], 0x44);
r[1] = _mm512_shuffle_i32x4(d[0], d[1], 0xee);
r[2] = _mm512_shuffle_i32x4(d[2], d[3], 0x44);
r[3] = _mm512_shuffle_i32x4(d[2], d[3], 0xee);
d[0] = _mm512_shuffle_i32x4(r[0], r[2], 0x88);
d[1] = _mm512_shuffle_i32x4(r[0], r[2], 0xdd);
d[2] = _mm512_shuffle_i32x4(r[1], r[3], 0x88);
d[3] = _mm512_shuffle_i32x4(r[1], r[3], 0xdd);
}
inline void transpose_16x16_32bit(__m512i * v) {
__m512i v1[16];
v1[0] = _mm512_unpacklo_epi32(v[0], v[1]);
v1[1] = _mm512_unpackhi_epi32(v[0], v[1]);
v1[2] = _mm512_unpacklo_epi32(v[2], v[3]);
v1[3] = _mm512_unpackhi_epi32(v[2], v[3]);
v1[4] = _mm512_unpacklo_epi32(v[4], v[5]);
v1[5] = _mm512_unpackhi_epi32(v[4], v[5]);
v1[6] = _mm512_unpacklo_epi32(v[6], v[7]);
v1[7] = _mm512_unpackhi_epi32(v[6], v[7]);
v1[8] = _mm512_unpacklo_epi32(v[8], v[9]);
v1[9] = _mm512_unpackhi_epi32(v[8], v[9]);
v1[10] = _mm512_unpacklo_epi32(v[10], v[11]);
v1[11] = _mm512_unpackhi_epi32(v[10], v[11]);
v1[12] = _mm512_unpacklo_epi32(v[12], v[13]);
v1[13] = _mm512_unpackhi_epi32(v[12], v[13]);
v1[14] = _mm512_unpacklo_epi32(v[14], v[15]);
v1[15] = _mm512_unpackhi_epi32(v[14], v[15]);
v[0] = _mm512_unpacklo_epi64(v1[0], v1[2]);
v[1] = _mm512_unpackhi_epi64(v1[0], v1[2]);
v[2] = _mm512_unpacklo_epi64(v1[1], v1[3]);
v[3] = _mm512_unpackhi_epi64(v1[1], v1[3]);
v[4] = _mm512_unpacklo_epi64(v1[4], v1[6]);
v[5] = _mm512_unpackhi_epi64(v1[4], v1[6]);
v[6] = _mm512_unpacklo_epi64(v1[5], v1[7]);
v[7] = _mm512_unpackhi_epi64(v1[5], v1[7]);
v[8] = _mm512_unpacklo_epi64(v1[8], v1[10]);
v[9] = _mm512_unpackhi_epi64(v1[8], v1[10]);
v[10] = _mm512_unpacklo_epi64(v1[9], v1[11]);
v[11] = _mm512_unpackhi_epi64(v1[9], v1[11]);
v[12] = _mm512_unpacklo_epi64(v1[12], v1[14]);
v[13] = _mm512_unpackhi_epi64(v1[12], v1[14]);
v[14] = _mm512_unpacklo_epi64(v1[13], v1[15]);
v[15] = _mm512_unpackhi_epi64(v1[13], v1[15]);
v1[0] = _mm512_shuffle_i32x4(v[0], v[4], 0x88);
v1[1] = _mm512_shuffle_i32x4(v[1], v[5], 0x88);
v1[2] = _mm512_shuffle_i32x4(v[2], v[6], 0x88);
v1[3] = _mm512_shuffle_i32x4(v[3], v[7], 0x88);
v1[4] = _mm512_shuffle_i32x4(v[0], v[4], 0xdd);
v1[5] = _mm512_shuffle_i32x4(v[1], v[5], 0xdd);
v1[6] = _mm512_shuffle_i32x4(v[2], v[6], 0xdd);
v1[7] = _mm512_shuffle_i32x4(v[3], v[7], 0xdd);
v1[8] = _mm512_shuffle_i32x4(v[8], v[12], 0x88);
v1[9] = _mm512_shuffle_i32x4(v[9], v[13], 0x88);
v1[10] = _mm512_shuffle_i32x4(v[10], v[14], 0x88);
v1[11] = _mm512_shuffle_i32x4(v[11], v[15], 0x88);
v1[12] = _mm512_shuffle_i32x4(v[8], v[12], 0xdd);
v1[13] = _mm512_shuffle_i32x4(v[9], v[13], 0xdd);
v1[14] = _mm512_shuffle_i32x4(v[10], v[14], 0xdd);
v1[15] = _mm512_shuffle_i32x4(v[11], v[15], 0xdd);
v[0] = _mm512_shuffle_i32x4(v1[0], v1[8], 0x88);
v[1] = _mm512_shuffle_i32x4(v1[1], v1[9], 0x88);
v[2] = _mm512_shuffle_i32x4(v1[2], v1[10], 0x88);
v[3] = _mm512_shuffle_i32x4(v1[3], v1[11], 0x88);
v[4] = _mm512_shuffle_i32x4(v1[4], v1[12], 0x88);
v[5] = _mm512_shuffle_i32x4(v1[5], v1[13], 0x88);
v[6] = _mm512_shuffle_i32x4(v1[6], v1[14], 0x88);
v[7] = _mm512_shuffle_i32x4(v1[7], v1[15], 0x88);
v[8] = _mm512_shuffle_i32x4(v1[0], v1[8], 0xdd);
v[9] = _mm512_shuffle_i32x4(v1[1], v1[9], 0xdd);
v[10] = _mm512_shuffle_i32x4(v1[2], v1[10], 0xdd);
v[11] = _mm512_shuffle_i32x4(v1[3], v1[11], 0xdd);
v[12] = _mm512_shuffle_i32x4(v1[4], v1[12], 0xdd);
v[13] = _mm512_shuffle_i32x4(v1[5], v1[13], 0xdd);
v[14] = _mm512_shuffle_i32x4(v1[6], v1[14], 0xdd);
v[15] = _mm512_shuffle_i32x4(v1[7], v1[15], 0xdd);
}
void quantize_row_q8_K_vnni(const float * RESTRICT x, void * RESTRICT vy, int64_t k) {
assert(k % QK_K == 0);
const int KB = k / QK_K;
constexpr int kVecs = QK_K / 16;
block_q8_K * y = reinterpret_cast<block_q8_K *>(vy);
// hold 16 float vecs from x
__m512 v[kVecs];
// hold the quants vecs
__m512i vq[kVecs / 4];
// hold the packed quants vecs
__m512i vq_packed[kVecs / 4];
const __m512 signBit = _mm512_set1_ps(-0.f);
for (int i = 0; i < KB; ++i) {
// Compute max(abs(e)) for the block
__m512 vamax = _mm512_set1_ps(0.f);
for (int j = 0; j < kVecs; ++j) {
v[j] = _mm512_loadu_ps(x); x += 16;
vamax = _mm512_max_ps(vamax, _mm512_andnot_ps(signBit, v[j]));
}
const float amax = _mm512_reduce_max_ps(vamax);
// Quantize these floats
const float iscale = 127.f / amax;
y[i].d = GGML_FP32_TO_FP16(1 / iscale);
const float id = ( amax != 0.0f ) ? iscale : 0.f;
const __m512 vscale = _mm512_set1_ps(id);
// Apply multiplier and round to nearest integer
for (int j = 0; j < kVecs; ++j) {
v[j] = _mm512_mul_ps(v[j], vscale);
v[j] = _mm512_roundscale_ps(v[j], (_MM_FROUND_TO_NEAREST_INT | _MM_FROUND_NO_EXC));
}
// Pack to epi8 vecs
for (int j = 0; j < kVecs / 4; ++j) {
__m128i q8_0 = _mm512_cvtepi32_epi8(_mm512_cvtps_epi32(v[j * 4 + 0]));
__m128i q8_1 = _mm512_cvtepi32_epi8(_mm512_cvtps_epi32(v[j * 4 + 1]));
__m128i q8_2 = _mm512_cvtepi32_epi8(_mm512_cvtps_epi32(v[j * 4 + 2]));
__m128i q8_3 = _mm512_cvtepi32_epi8(_mm512_cvtps_epi32(v[j * 4 + 3]));
__m256i q8_01 = _mm256_insertf128_si256(_mm256_castsi128_si256(q8_0), (q8_1), 1);
__m256i q8_23 = _mm256_insertf128_si256(_mm256_castsi128_si256(q8_2), (q8_3), 1);
vq[j] = _mm512_inserti32x8(_mm512_castsi256_si512(q8_01), q8_23, 1);
_mm512_storeu_si512((__m512i *)(y[i].qs + j * 64), vq[j]);
}
// Compute the bsums with vnni
transpose_16x4_32bit(vq, vq_packed);
const __m512i one = _mm512_set1_epi8(1);
__m512i sum = _mm512_setzero_si512();
for (int k = 0; k < 4; ++k) {
sum = _mm512_dpbusd_epi32(sum, one, vq_packed[k]);
}
_mm256_storeu_si256((__m256i *)(y[i].bsums), _mm512_cvtepi32_epi16(sum));
}
}
// quantize A from float to `vec_dot_type`
template <typename T>
inline void from_float(const float * x, char * vy, int64_t k);
template <>
inline void from_float<block_q8_0>(const float * x, char * vy, int64_t k) {
quantize_row_q8_0(x, (block_q8_0 *)vy, k);
}
template <>
inline void from_float<block_q8_1>(const float * x, char * vy, int64_t k) {
quantize_row_q8_1(x, (block_q8_1 *)vy, k);
}
template <>
inline void from_float<block_q8_K>(const float * x, char * vy, int64_t k) {
#if 1
// TODO: this is reference impl!
quantize_row_q8_K_ref(x, (block_q8_K *)vy, k);
#else
quantize_row_q8_K_vnni(x, vy, k);
#endif
}
// load A from memory to array when nrows can not fill in whole tile
void unpack_A(int8_t * RESTRICT tile, const block_q8_0 * RESTRICT A, int lda, int nr) {
assert(nr != TILE_M);
for (int m = 0; m < nr; ++m) {
const __m256i v = _mm256_loadu_si256((const __m256i *)(A[m * lda].qs));
_mm256_storeu_si256((__m256i *)(tile + m * TILE_K), v);
}
}
void unpack_A(int8_t * RESTRICT tile, const block_q8_1 * RESTRICT A, int lda, int nr) {
assert(nr != TILE_M);
for (int m = 0; m < nr; ++m) {
const __m256i v = _mm256_loadu_si256((const __m256i *)(A[m * lda].qs));
_mm256_storeu_si256((__m256i *)(tile + m * TILE_K), v);
}
}
template <typename TB>
void unpack_A(int8_t * RESTRICT tile, const block_q8_K * RESTRICT A, int lda, int k, int nr) {
assert(nr <= TILE_M);
for (int m = 0; m < nr; ++m) {
const __m256i v = _mm256_loadu_si256((const __m256i *)(A[m * lda].qs + k * 32));
_mm256_storeu_si256((__m256i *)(tile + m * TILE_K), v);
}
}
template <>
void unpack_A<block_q6_K>(int8_t * RESTRICT tile, const block_q8_K * RESTRICT A, int lda, int k, int nr) {
assert(nr <= TILE_M);
// zero padding k from 16 to 32, so that we don't have to re-config amx
const __m128i zero = _mm_setzero_si128();
for (int m = 0; m < nr; ++m) {
const __m128i v = _mm_loadu_si128((const __m128i *)(A[m * lda].qs + k * 16));
const __m256i r = _mm256_insertf128_si256(_mm256_castsi128_si256(v), zero, 1);
_mm256_storeu_si256((__m256i *)(tile + m * TILE_K), r);
}
}
#define MM256_SET_M128I(a, b) _mm256_insertf128_si256(_mm256_castsi128_si256(b), (a), 1)
inline __m256i bytes_from_nibbles_32(const uint8_t * rsi) {
const __m128i tmp = _mm_loadu_si128((const __m128i *)rsi);
const __m256i bytes = MM256_SET_M128I(_mm_srli_epi16(tmp, 4), tmp);
const __m256i lowMask = _mm256_set1_epi8(0xF);
return _mm256_and_si256(lowMask, bytes);
}
// used for block_q4_K
inline __m512i bytes_from_nibbles_64(const uint8_t * rsi) {
const __m256i tmp = _mm256_loadu_si256((const __m256i *)rsi);
const __m256i lowMask = _mm256_set1_epi8(0xF);
const __m256i q4l = _mm256_and_si256(tmp, lowMask);
const __m256i q4h = _mm256_and_si256(_mm256_srli_epi16(tmp, 4), lowMask);
return _mm512_inserti32x8(_mm512_castsi256_si512(q4l), q4h, 1);
}
// used for block_q5_K
inline __m512i bytes_from_nibbles_64(const uint8_t * qs, const uint8_t * qh, int k) {
const __m256i lowMask = _mm256_set1_epi8(0xF);
__m256i hmask = _mm256_set1_epi8(1);
hmask = _mm256_slli_epi16(hmask, k);
const __m256i q5bits = _mm256_loadu_si256((const __m256i *)qs);
const __m256i hbits = _mm256_loadu_si256((const __m256i *)qh);
const __m256i q5l_0 = _mm256_and_si256(q5bits, lowMask);
const __m256i q5h_0 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), k + 0), 4);
const __m256i q5_0 = _mm256_add_epi8(q5l_0, q5h_0);
hmask = _mm256_slli_epi16(hmask, 1);
const __m256i q5l_1 = _mm256_and_si256(_mm256_srli_epi16(q5bits, 4), lowMask);
const __m256i q5h_1 = _mm256_slli_epi16(_mm256_srli_epi16(_mm256_and_si256(hbits, hmask), k + 1), 4);
const __m256i q5_1 = _mm256_add_epi8(q5l_1, q5h_1);
return _mm512_inserti32x8(_mm512_castsi256_si512(q5_0), q5_1, 1);
}
// used for block_q6_K
inline void bytes_from_nibbles_128(__m512i& r0, __m512i& r1, const uint8_t * qs, const uint8_t * qh) {
const __m256i m4 = _mm256_set1_epi8(0xF);
const __m256i m2 = _mm256_set1_epi8(0x3);
const __m256i q6bits1 = _mm256_loadu_si256((const __m256i *)qs);
const __m256i q6bits2 = _mm256_loadu_si256((const __m256i *)(qs + 32));
const __m256i q6bitsH = _mm256_loadu_si256((const __m256i *)qh);
const __m256i q6h_0 = _mm256_slli_epi16(_mm256_and_si256( q6bitsH, m2), 4);
const __m256i q6h_1 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q6bitsH, 2), m2), 4);
const __m256i q6h_2 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q6bitsH, 4), m2), 4);
const __m256i q6h_3 = _mm256_slli_epi16(_mm256_and_si256(_mm256_srli_epi16(q6bitsH, 6), m2), 4);
const __m256i q6_0 = _mm256_or_si256(_mm256_and_si256(q6bits1, m4), q6h_0);
const __m256i q6_1 = _mm256_or_si256(_mm256_and_si256(q6bits2, m4), q6h_1);
const __m256i q6_2 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q6bits1, 4), m4), q6h_2);
const __m256i q6_3 = _mm256_or_si256(_mm256_and_si256(_mm256_srli_epi16(q6bits2, 4), m4), q6h_3);
r0 = _mm512_inserti32x8(_mm512_castsi256_si512(q6_0), q6_1, 1);
r1 = _mm512_inserti32x8(_mm512_castsi256_si512(q6_2), q6_3, 1);
}
inline __m512i packNibbles(__m512i r0, __m512i r1) {
return _mm512_or_si512(r0, _mm512_slli_epi16(r1, 4));
}
template <typename TB>
inline void pack_qs(void * RESTRICT packed_B, const TB * RESTRICT B, int KB) {
int8_t tmp[8 * 64];
__m256i v[8], v2[8];
for (int n = 0; n < 8; ++n) {
v[n] = bytes_from_nibbles_32(B[n * KB].qs);
}
transpose_8x8_32bit(v, v2);
for (int n = 0; n < 8; ++n) {
_mm256_storeu_si256((__m256i *)(tmp + n * 64), v2[n]);
}
for (int n = 0; n < 8; ++n) {
v[n] = bytes_from_nibbles_32(B[(n + 8) * KB].qs);
}
transpose_8x8_32bit(v, v2);
for (int n = 0; n < 8; ++n) {
_mm256_storeu_si256((__m256i *)(tmp + n * 64 + 32), v2[n]);
}
// pack again with 128 to fully utilize vector length
for (int n = 0; n < 8; n += 2) {
__m512i r0 = _mm512_loadu_si512((const __m512i *)(tmp + n * 64));
__m512i r1 = _mm512_loadu_si512((const __m512i *)(tmp + n * 64 + 64));
__m512i r1r0 = packNibbles(r0, r1);
_mm512_storeu_si512((__m512i *)((char *)packed_B + n * 32), r1r0);
}
}
template <>
inline void pack_qs<block_q8_0>(void * RESTRICT packed_B, const block_q8_0 * RESTRICT B, int KB) {
__m256i v[8], v2[8];
for (int n = 0; n < 8; ++n) {
v[n] = _mm256_loadu_si256((const __m256i *)(B[n * KB].qs));
}
transpose_8x8_32bit(v, v2);
for (int n = 0; n < 8; ++n) {
_mm256_storeu_si256((__m256i *)((char *)packed_B + n * 64), v2[n]);
}
for (int n = 0; n < 8; ++n) {
v[n] = _mm256_loadu_si256((const __m256i *)(B[(n + 8) * KB].qs));
}
transpose_8x8_32bit(v, v2);
for (int n = 0; n < 8; ++n) {
_mm256_storeu_si256((__m256i *)((char *)packed_B + n * 64 + 32), v2[n]);
}
}
template <>
inline void pack_qs<block_q4_K>(void * RESTRICT packed_B, const block_q4_K * RESTRICT B, int KB) {
__m512i v[16];
// QK_K 256 with 8 groups, handle 2 groups at a time
char * pb = (char *)packed_B;
for (int k = 0; k < QK_K / 64; ++k) {
// pack 2 groups { n, g, k} to {g, k/4, 4n}
// e.g. {16, 2, 32} to {2, 8, 64}
for (int n = 0; n < TILE_N; ++n) {
v[n] = bytes_from_nibbles_64(B[n * KB].qs + k * 32);
}
transpose_16x16_32bit(v);
// pack again with 128 to fully utilize vector length
for (int n = 0; n < TILE_N; n += 2) {
_mm512_storeu_si512((__m512i *)pb, packNibbles(v[n], v[n + 1]));
pb += 64;
}
}
}
template <>
inline void pack_qs<block_q5_K>(void * RESTRICT packed_B, const block_q5_K * RESTRICT B, int KB) {
__m512i v[16];
const __m512i lowMask = _mm512_set1_epi8(0xF);
// QK_K 256 with 8 groups, handle 2 groups at a time
char * pb = (char *)packed_B;
char * ph = (char *)packed_B + (QK_K / 2) * TILE_N;
for (int k = 0; k < QK_K / 64; ++k) {
// pack 2 groups { n, g, k} to {g, k/4, 4n}
// e.g. {16, 2, 32} to {2, 8, 64}
for (int n = 0; n < TILE_N; ++n) {
v[n] = bytes_from_nibbles_64(B[n * KB].qs + k * 32, B[n * KB].qh, /* group */2 * k);
}
transpose_16x16_32bit(v);
// 1. pack lower 4bits with 2 groups
for (int n = 0; n < TILE_N; n += 2) {
// get lower 4 bits
const __m512i r0 = _mm512_and_si512(v[n], lowMask);
const __m512i r1 = _mm512_and_si512(v[n + 1], lowMask);
_mm512_storeu_si512((__m512i *)pb, packNibbles(r0, r1)); pb += 64;
}
// 2. pack higher 1bit with 2 groups
const __m512i hmask = _mm512_set1_epi8(0x10);
for (int g = 0; g < 2; ++g) {
__m512i hbits = _mm512_setzero_si512();
hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 8 + 0], hmask), 4));
hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 8 + 1], hmask), 3));
hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 8 + 2], hmask), 2));
hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 8 + 3], hmask), 1));
hbits = _mm512_add_epi8(hbits, _mm512_and_si512(v[g * 8 + 4], hmask) );
hbits = _mm512_add_epi8(hbits, _mm512_slli_epi16(_mm512_and_si512(v[g * 8 + 5], hmask), 1));
hbits = _mm512_add_epi8(hbits, _mm512_slli_epi16(_mm512_and_si512(v[g * 8 + 6], hmask), 2));
hbits = _mm512_add_epi8(hbits, _mm512_slli_epi16(_mm512_and_si512(v[g * 8 + 7], hmask), 3));
_mm512_storeu_si512((__m512i *)ph, hbits); ph += 64;
}
}
}
template <>
inline void pack_qs<block_q6_K>(void * RESTRICT packed_B, const block_q6_K * RESTRICT B, int KB) {
__m512i v[32];
const __m512i lowMask = _mm512_set1_epi8(0xF);
// QK_K 256 with 8 groups, handle 4 groups at a time
char * pb = (char *)packed_B;
char * ph = (char *)packed_B + (QK_K / 2) * TILE_N;
for (int k = 0; k < QK_K / 128; ++k) {
for (int n = 0; n < TILE_N; ++n) {
bytes_from_nibbles_128(v[n], v[n + 16], B[n * KB].ql + k * 64, B[n * KB].qh + k * 32);
}
// top half: group 0,1 or 4,5; bottom half: group 2,3 or 6,7
transpose_16x16_32bit(v);
transpose_16x16_32bit(v + 16);
// 1. pack lower 4bits with 4 groups
for (int n = 0; n < 32; n += 2) {
const __m512i r0 = _mm512_and_si512(v[n], lowMask);
const __m512i r1 = _mm512_and_si512(v[n + 1], lowMask);
_mm512_storeu_si512((__m512i *)pb, packNibbles(r0, r1)); pb += 64;
}
// 2. pack higher 2bit with 4 groups
const __m512i hmask = _mm512_set1_epi8(0x30);
for (int g = 0; g < 8; ++g) {
__m512i hbits = _mm512_setzero_si512();
hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 4 + 0], hmask), 4));
hbits = _mm512_add_epi8(hbits, _mm512_srli_epi16(_mm512_and_si512(v[g * 4 + 1], hmask), 2));
hbits = _mm512_add_epi8(hbits, _mm512_and_si512(v[g * 4 + 2], hmask) );
hbits = _mm512_add_epi8(hbits, _mm512_slli_epi16(_mm512_and_si512(v[g * 4 + 3], hmask), 2));
_mm512_storeu_si512((__m512i *)ph, hbits); ph += 64;
}
}
}
template <>
inline void pack_qs<block_iq4_xs>(void * RESTRICT packed_B, const block_iq4_xs * RESTRICT B, int KB) {
__m512i v[16];
char * pb = (char *)packed_B;
for (int k = 0; k < QK_K / 64; ++k) {
for (int n = 0; n < TILE_N; ++n) {
__m256i r0 = bytes_from_nibbles_32(B[n * KB].qs + k * 32 + 0);
__m256i r1 = bytes_from_nibbles_32(B[n * KB].qs + k * 32 + 16);
v[n] = _mm512_inserti32x8(_mm512_castsi256_si512(r0), r1, 1);
}
transpose_16x16_32bit(v);
// pack again with 128 to fully utilize vector length
for (int n = 0; n < TILE_N; n += 2) {
_mm512_storeu_si512((__m512i *)pb, packNibbles(v[n], v[n + 1]));
pb += 64;
}
}
}
// pack B to vnni formats in 4bits or 8 bits
void pack_B(void * RESTRICT packed_B, const block_q4_0 * RESTRICT B, int KB) {
pack_qs(packed_B, B, KB);
ggml_half * d0 = reinterpret_cast<ggml_half *>((char *)packed_B + TILE_N * TILE_K / 2);
for (int n = 0; n < TILE_N; ++n) {
d0[n] = B[n * KB].d;
}
}
void pack_B(void * RESTRICT packed_B, const block_q4_1 * RESTRICT B, int KB) {
pack_qs(packed_B, B, KB);
ggml_half * d0 = reinterpret_cast<ggml_half *>((char *)packed_B + TILE_N * TILE_K / 2);
ggml_half * m0 = d0 + TILE_N;
for (int n = 0; n < TILE_N; ++n) {
d0[n] = B[n * KB].d;
m0[n] = B[n * KB].m;
}
}
inline void s8s8_compensation(void * RESTRICT packed_B) {
// packed_B layout:
// quants {TILE_N, TILEK} int8_t
// d0 {TILE_N} ggml_half
// comp {TILE_N} int32_t
const int offset = TILE_N * TILE_K + TILE_N * sizeof(ggml_half);
__m512i vcomp = _mm512_setzero_si512();
const __m512i off = _mm512_set1_epi8(static_cast<char>(0x80));
for (int k = 0; k < 8; ++k) {
__m512i vb = _mm512_loadu_si512((const __m512i *)((const char *)packed_B + k * 64));
vcomp = _mm512_dpbusd_epi32(vcomp, off, vb);
}
_mm512_storeu_si512((__m512i *)((char *)(packed_B) + offset), vcomp);
}
void pack_B(void * RESTRICT packed_B, const block_q8_0 * RESTRICT B, int KB) {
pack_qs(packed_B, B, KB);
ggml_half * d0 = reinterpret_cast<ggml_half *>((char *)packed_B + TILE_N * TILE_K);
for (int n = 0; n < TILE_N; ++n) {
d0[n] = B[n * KB].d;
}
s8s8_compensation(packed_B);
}
// convert 8 * {min, scale} from int6 to int8
inline void unpack_mins_and_scales(const uint8_t * scales, uint32_t * utmp) {
const uint32_t kmask1 = 0x3f3f3f3f;
const uint32_t kmask2 = 0x0f0f0f0f;
const uint32_t kmask3 = 0x03030303;
memcpy(utmp, scales, 12);
utmp[3] = ((utmp[2] >> 4) & kmask2) | (((utmp[1] >> 6) & kmask3) << 4);
const uint32_t uaux = utmp[1] & kmask1;
utmp[1] = (utmp[2] & kmask2) | (((utmp[0] >> 6) & kmask3) << 4);
utmp[2] = uaux;
utmp[0] &= kmask1;
}
// packed_B layout:
// quants {8, TILE_N, 16} uint8
// scales {8, TILE_N} uint8
// mins {8, TILE_N} uint8
// d {TILE_N} ggml_half
// dmin {TILE_N} ggml_half
void pack_B(void * RESTRICT packed_B, const block_q4_K * RESTRICT B, int KB) {
pack_qs(packed_B, B, KB);
uint8_t * scales = reinterpret_cast<uint8_t *>((char *)packed_B + (QK_K / 2) * TILE_N);
uint8_t * mins = scales + 8 * TILE_N;
ggml_half * d = reinterpret_cast<ggml_half *>(mins + 8 * TILE_N);
ggml_half * dmin = d + TILE_N;
union {
uint32_t u32[4];
uint8_t u8[16];
} s;
for (int n = 0; n < TILE_N; ++n) {
unpack_mins_and_scales(B[n * KB].scales, s.u32);
for (int k = 0; k < 8; ++k) {
scales[k * TILE_N + n] = s.u8[k];
mins[(k >> 1) * TILE_N * 2 + n * 2 + (k & 0x1)] = s.u8[k + 8];
}
d[n] = B[n * KB].d;
dmin[n] = B[n * KB].dmin;
}
}
// packed_B layout:
// quants {8, TILE_N, 16} uint8
// qh {8, TILE_N, 4} uint8
// scales {8, TILE_N} uint8
// mins {8, TILE_N} uint8
// d {TILE_N} ggml_half
// dmin {TILE_N} ggml_half
void pack_B(void * RESTRICT packed_B, const block_q5_K * RESTRICT B, int KB) {
pack_qs(packed_B, B, KB);
uint8_t * scales = reinterpret_cast<uint8_t *>((char *)packed_B + (QK_K / 2) * TILE_N + (QK_K / 8) * TILE_N);
uint8_t * mins = scales + 8 * TILE_N;
ggml_half * d = reinterpret_cast<ggml_half *>(mins + 8 * TILE_N);
ggml_half * dmin = d + TILE_N;
union {
uint32_t u32[4];
uint8_t u8[16];
} s;
for (int n = 0; n < TILE_N; ++n) {
unpack_mins_and_scales(B[n * KB].scales, s.u32);
for (int k = 0; k < 8; ++k) {
scales[k * TILE_N + n] = s.u8[k];
mins[(k >> 1) * TILE_N * 2 + n * 2 + (k & 0x1)] = s.u8[k + 8];
}
d[n] = B[n * KB].d;
dmin[n] = B[n * KB].dmin;
}
}
// packed_B layout:
// quants {16, TILE_N, 8} uint8
// qh {16, TILE_N, 4} uint8
// scales {16, TILE_N} uint8
// d {TILE_N} ggml_half
void pack_B(void * RESTRICT packed_B, const block_q6_K * RESTRICT B, int KB) {
pack_qs(packed_B, B, KB);
uint8_t * scales = reinterpret_cast<uint8_t *>((char *)packed_B + (QK_K / 2) * TILE_N + (QK_K / 4) * TILE_N);
ggml_half * d = reinterpret_cast<ggml_half *>(scales + 16 * TILE_N);
for (int n = 0; n < TILE_N; ++n) {
const int8_t * ps = B[n * KB].scales;
for (int k = 0; k < 16; ++k) {
scales[k * TILE_N + n] = ps[k];
}
d[n] = B[n * KB].d;
}
}
// packed_B layout:
// quants {8, TILE_N, 16} uint8
// scales {8, TILE_N} int8
// d {TILE_N} ggml_half
void pack_B(void * RESTRICT packed_B, const block_iq4_xs * RESTRICT B, int KB) {
pack_qs(packed_B, B, KB);
int8_t * scales = reinterpret_cast<int8_t *>((char *)packed_B + (QK_K / 2) * TILE_N);
ggml_half * d = reinterpret_cast<ggml_half *>(scales + 8 * TILE_N);
// pack the scales
for (int n = 0; n < TILE_N; ++n) {
uint16_t sh = B[n * KB].scales_h;
for (int k = 0; k < 8; k += 2) {
const int16_t ls1 = ((B[n * KB].scales_l[k / 2] & 0xf) | ((sh << 4) & 0x30)) - 32;
const int16_t ls2 = ((B[n * KB].scales_l[k / 2] >> 4) | ((sh << 2) & 0x30)) - 32;
scales[(k + 0) * TILE_N + n] = ls1;
scales[(k + 1) * TILE_N + n] = ls2;
sh >>= 4;
}
d[n] = B[n * KB].d;
}
}
template<typename TB, typename packed_B_t = packed_B_type<TB>>
void unpack_B(packed_B_t * RESTRICT tile, const void * RESTRICT packed_B) {
GGML_UNUSED(tile);
GGML_UNUSED(packed_B);
}
template <>
void unpack_B<block_q4_0>(int8_t * RESTRICT tile, const void * RESTRICT packed_B) {
const __m512i off = _mm512_set1_epi8(8);
const __m512i lowMask = _mm512_set1_epi8(0xF);
for (int n = 0; n < 8; n += 2) {
__m512i bytes = _mm512_loadu_si512((const __m512i *)((const char *)packed_B + n * 32));
const __m512i r0 = _mm512_sub_epi8(_mm512_and_si512(bytes, lowMask), off);
const __m512i r1 = _mm512_sub_epi8(_mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask), off);
_mm512_storeu_si512((__m512i *)(tile + n * 64 + 0), r0);
_mm512_storeu_si512((__m512i *)(tile + n * 64 + 64), r1);
}
}
template <>
void unpack_B<block_q4_1>(uint8_t * RESTRICT tile, const void * RESTRICT packed_B) {
const __m512i lowMask = _mm512_set1_epi8(0xF);
for (int n = 0; n < 8; n += 2) {
__m512i bytes = _mm512_loadu_si512((const __m512i *)((const char *)packed_B + n * 32));
const __m512i r0 = _mm512_and_si512(bytes, lowMask);
const __m512i r1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
_mm512_storeu_si512((__m512i *)(tile + n * 64 + 0), r0);
_mm512_storeu_si512((__m512i *)(tile + n * 64 + 64), r1);
}
}
// packed_B_t for QKK is int8_t
template <typename TB>
void unpack_B(int8_t * RESTRICT tile, const void * RESTRICT packed_B, int k) {
const int packed_B_group_size = QK_K / 2 * TILE_N / 8;
const char * packed_B_group = (const char *)packed_B + k * packed_B_group_size;
const __m512i lowMask = _mm512_set1_epi8(0xF);
for (int n = 0; n < 8; n += 2) {
__m512i bytes = _mm512_loadu_si512(packed_B_group + n * 32);
const __m512i r0 = _mm512_and_si512(bytes, lowMask);
const __m512i r1 = _mm512_and_si512(_mm512_srli_epi16(bytes, 4), lowMask);
_mm512_storeu_si512((__m512i *)(tile + n * 64 + 0), r0);
_mm512_storeu_si512((__m512i *)(tile + n * 64 + 64), r1);
}
}
template <>
void unpack_B<block_q5_K>(int8_t * RESTRICT tile, const void * RESTRICT packed_B, int k) {
// lower 4bits, stride 256 bytes
const int packed_l4_group_size = QK_K / 2 * TILE_N / 8;