Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cpu/tiled/tiled-kernel.h # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cpu / tiled / tiled-kernel.h` 180 lines ยท 9547 bytes Raw Blame History 1 #pragma once 2 3 // Tiled matmul kernel API: tile structs, kernel definitions 4 5 // Currently only optimized for x86, new architectures should implement: 6 // tiled_run_microtile: 16x16 microkernel 7 // tiled_repack_src0: Optional repack/recalculation of src0, per macrotile 8 // tiled_repack_src1: Optional repack/recalculation of src1, per microtile-band 9 // bit unpacking routines: tiled_unpk_nib4, tiled_unpk_2bit, tiled_unpk_or 10 // LUT value expansion routines: tiled_lut8, tiled_unpk_sign32, tiled_unpk_tern8 11 12 #define GGML_COMMON_DECL_CPP 13 #include "ggml-common.h" 14 15 #include 16 #include 17 18 #if defined(__AVX2__) 19 #include 20 #endif 21 22 #define TILED_TILE_K 256 // one QK_K block 23 #define TILED_TILE_ROWS 256 // max window rows, ragged at edges 24 #define TILED_MICRO 16 // microtile edge (also the bsums code-sum granularity) 25 #define TILED_WS_SLOT (512 * 1024) // per-thread workspace slot, a clean 512KB (multiple of 64B) 26 27 // src0 tile: weight side, shared by all formats. 28 // scales/mins are sized for the max subblock count (SUBBLK=16); 29 // SUBBLK=32 formats index at stride 8 and leave the slack unused. 30 struct tiled_tile_src0 { 31 static constexpr int NB_MAX = TILED_TILE_K / 16; // max subblocks per 256-elem block 32 33 alignas(64) uint8_t q[TILED_TILE_ROWS * TILED_TILE_K]; // unsigned quants, widened to uint8 34 float d[TILED_TILE_ROWS]; // One d from each input block, widened to f32 35 float dmin[TILED_TILE_ROWS]; // dmin from each input block (if applicable), widened to F32 36 int32_t scales[TILED_TILE_ROWS * NB_MAX]; // per-subblock scale, stored as int32_t 37 int32_t mins[TILED_TILE_ROWS * NB_MAX]; // per-subblock min, used when HAS_MIN 38 }; 39 40 // src1 tile: built from q8_K (wdata) 41 struct tiled_tile_src1 { 42 // q8 codes, one byte per element. Note for VNNI these are reshaped + transposed to be suitable for dpbusd. 43 alignas(64) int8_t q[TILED_TILE_ROWS * TILED_TILE_K]; 44 // per-16 code sums from q8_k (int16), widened to int32 so the kernels load them directly, no per-use cvt 45 alignas(64) int32_t bsums[(TILED_TILE_K / 16) * TILED_TILE_ROWS]; 46 // f32 (not f16): q8_k stores fp16, the unpack converts once 47 float d[TILED_TILE_ROWS]; 48 }; 49 50 // per-thread workspace: all tiled state lives here, allocated in wdata (one slot per thread) 51 struct tiled_ws { 52 tiled_tile_src0 src0; 53 tiled_tile_src1 src1; 54 alignas(64) float acc[TILED_TILE_ROWS * TILED_TILE_ROWS]; 55 }; 56 57 static_assert(sizeof(tiled_ws) <= TILED_WS_SLOT, "tiled workspace exceeds the 512KB per-thread slot"); 58 // the slot base is 64B-aligned and TILED_WS_SLOT is a multiple of 64B, so each per-thread slot 59 // is 64B-aligned; these pin the tile fields and the acc buffer at aligned offsets within a slot 60 static_assert(offsetof(tiled_ws, src0) % 64 == 0, "src0 not 64B-aligned in the workspace"); 61 static_assert(offsetof(tiled_ws, src1) % 64 == 0, "src1 not 64B-aligned in the workspace"); 62 static_assert(offsetof(tiled_ws, acc) % 64 == 0, "acc not 64B-aligned in the workspace"); 63 64 // unpack primitives for reading quants, defined as inline here to keep arch-specific code in kernel.h/.cpp 65 // If this section gets too hairy later, we can break up into separate includes. 66 #if defined(__AVX2__) 67 // packed 4-bit codes -> low nibbles (lo) + high nibbles (hi) 68 inline void tiled_unpk_nib4(const uint8_t * src, uint8_t * lo, uint8_t * hi) { 69 const __m256i v = _mm256_loadu_si256((const __m256i *) src); 70 // mask before the lane shift so bits do not cross byte boundaries 71 _mm256_storeu_si256((__m256i *) lo, _mm256_and_si256(v, _mm256_set1_epi8(0x0F))); 72 _mm256_storeu_si256((__m256i *) hi, _mm256_srli_epi32(_mm256_and_si256(v, _mm256_set1_epi8((int8_t) 0xF0)), 4)); 73 } 74 // 2-bit values at bit offset S 75 template inline void tiled_unpk_2bit(const uint8_t * src, uint8_t * dst) { 76 _mm256_storeu_si256((__m256i *) dst, _mm256_and_si256( 77 _mm256_srli_epi32(_mm256_loadu_si256((const __m256i *) src), S), _mm256_set1_epi8(0x03))); 78 } 79 // OR the M-bit value at bit offset S of src into bit offset D of dst 80 template 81 inline void tiled_unpk_or(uint8_t * dst, const uint8_t * src) { 82 const __m256i v = _mm256_slli_epi32(_mm256_and_si256( 83 _mm256_srli_epi32(_mm256_loadu_si256((const __m256i *) src), S), _mm256_set1_epi8((uint8_t) M)), D); 84 _mm256_storeu_si256((__m256i *) dst, _mm256_or_si256(_mm256_loadu_si256((const __m256i *) dst), v)); 85 } 86 87 88 // Unpacking kernels for IQ quants 89 90 // LUT value expansion for the LUT-based formats (iq4_xs, iq grids): the bit unpackers 91 // above give the indices, these expand 8/16 of them to widened codes in one pass 92 // 16-entry byte LUT: dst[j] = lut[src[j]] (16 bytes) 93 inline void tiled_lut8(const uint8_t * lut, const uint8_t * src, uint8_t * dst) { 94 _mm_storeu_si128((__m128i *) dst, _mm_shuffle_epi8(_mm_loadu_si128((const __m128i *) lut), 95 _mm_loadu_si128((const __m128i *) src))); 96 } 97 98 99 // 32 grid magnitudes (4 x 64-bit groups g0..g3, 8 values each) + 4 sign bytes 100 // (byte l signs values 8*l .. 8*l+7) -> codes stored as (value + 128) 101 inline void tiled_unpk_sign32(uint64_t g0, uint64_t g1, uint64_t g2, uint64_t g3, 102 const uint8_t signs[4], uint8_t * dst32) { 103 const __m256i v = _mm256_set_epi64x((int64_t) g3, (int64_t) g2, (int64_t) g1, (int64_t) g0); 104 const __m256i sv = _mm256_shuffle_epi8(_mm256_set1_epi32((int32_t) (signs[0] | signs[1] << 8 | signs[2] << 16 | signs[3] << 24)), 105 _mm256_setr_epi8(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 106 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3)); 107 const __m256i sel = _mm256_set1_epi64x((int64_t) 0x8040201008040201ULL); 108 // 0xFF in every lane whose sign bit is set; GFNI does the and+compare in one instruction 109 #ifdef __GFNI__ 110 const __m256i mask = _mm256_gf2p8affine_epi64_epi8(sel, sv, 0); 111 #else 112 const __m256i mask = _mm256_cmpeq_epi8(_mm256_and_si256(sv, sel), sel); 113 #endif 114 // v ^ mask - mask negates the signed lanes (v elsewhere); + 128 gives the biased code 115 const __m256i sgn = _mm256_sub_epi8(_mm256_xor_si256(v, mask), mask); 116 _mm256_storeu_si256((__m256i *) dst32, _mm256_add_epi8(sgn, _mm256_set1_epi8((int8_t) 128))); 117 } 118 // 8 ternary grid bytes (0 = 0, 1 = +1, 0xFF = -1): dst[j] = 128 + delta + 8 * (int8_t) src[j] 119 inline void tiled_unpk_tern8(const uint8_t * src, int8_t delta, uint8_t * dst) { 120 const __m128i v = _mm_cvtepi8_epi16(_mm_loadl_epi64((const __m128i *) src)); 121 const __m128i p = _mm_add_epi16(_mm_slli_epi16(v, 3), _mm_set1_epi16(128 + (int) delta)); 122 _mm_storel_epi64((__m128i *) dst, _mm_packus_epi16(p, _mm_setzero_si128())); 123 } 124 #else 125 126 // Scalar definitions for unpackers. 127 128 129 inline void tiled_unpk_nib4(const uint8_t * src, uint8_t * lo, uint8_t * hi) { 130 for (int l = 0; l < 32; l++) { lo[l] = (uint8_t) (src[l] & 0xF); hi[l] = (uint8_t) (src[l] >> 4); } 131 } 132 template 133 inline void tiled_unpk_2bit(const uint8_t * src, uint8_t * dst) { 134 for (int l = 0; l < 32; l++) { dst[l] = (uint8_t) ((src[l] >> S) & 3); } 135 } 136 template 137 inline void tiled_unpk_or(uint8_t * dst, const uint8_t * src) { 138 for (int l = 0; l < 32; l++) { dst[l] = (uint8_t) (dst[l] | (((src[l] >> S) & M) << D)); } 139 } 140 inline void tiled_lut8(const uint8_t * lut, const uint8_t * src, uint8_t * dst) { 141 for (int j = 0; j < 16; j++) { dst[j] = lut[src[j]]; } 142 } 143 inline void tiled_unpk_sign32(uint64_t g0, uint64_t g1, uint64_t g2, uint64_t g3, 144 const uint8_t signs[4], uint8_t * dst32) { 145 const uint64_t g[4] = { g0, g1, g2, g3 }; 146 for (int l = 0; l < 4; l++) { 147 const uint8_t * v = (const uint8_t *) &g[l]; 148 const uint8_t s = signs[l]; 149 for (int j = 0; j < 8; j++) { 150 dst32[8 * l + j] = (s & (1 << j)) ? (uint8_t) (128 - v[j]) : (uint8_t) (128 + v[j]); 151 } 152 } 153 } 154 // 8 ternary grid bytes (0 = 0, 1 = +1, 0xFF = -1): dst[j] = 128 + delta + 8 * (int8_t) src[j] 155 inline void tiled_unpk_tern8(const uint8_t * src, int8_t delta, uint8_t * dst) { 156 for (int j = 0; j < 8; j++) { dst[j] = (uint8_t) (128 + (int) delta + 8 * (int8_t) src[j]); } 157 } 158 #endif 159 160 // Accumulate one 16x16 microtile (src0 rows [i0, i0+16), src1 cols [j0, j0+16)) 161 // over one 256-K slab held in the tiles into a j-major float buffer 162 // (row width buf_stride): buf[i*buf_stride + j] += partial. 163 // SUBBLK/HAS_MIN/BIAS are the src0 format constants (see tiled_tile_src0). 164 // ACTBIAS (AVX2 only): the activation is pre-biased +128 by tiled_repack_src1, 165 // num_k = K-blocks per row: the tile holds num_k slabs at row stride num_k*256 166 // (Default case is num_k=1, 256x256 tiles, we go to longer num_k to improve memory bandwidth when num_rows is small) 167 template 168 void tiled_run_microtile(const tiled_tile_src0 & src0, const tiled_tile_src1 & src1, 169 int i0, int j0, int num_k, int slab, float * buf, int buf_stride); 170 171 // Optional repack, if profitable for the kernel. 172 // Repacks one 16-row band of src1 codes, called by driver as we reach each 16-row band in outer loop 173 void tiled_repack_src1(tiled_tile_src1 * src1, int row0, int num_k, bool bias); 174 175 // Optional repack, if profitable for the kernel 176 // Repack the entire src0 panel in place, called by the driver immediately after dequant 177 template 178 void tiled_repack_src0(tiled_tile_src0 * tile, int n_rows, int num_k, int BIAS, bool corr); 179 180