ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-opencl / kernels / gemm_noshuffle_q6_k_f32_tiled.cl
| 1 | // Batched (N>1) q6_K GEMM over the 64-row-TILED canonical layout produced by |
| 2 | // kernel_convert_block_q6_k_tiled_ns (cvt.cl). Companion to the decode kernel |
| 3 | // kernel_gemv_noshuffle_q6_K_f32_tiled: SAME pack, SAME canonical e-order |
| 4 | // dequant (correct by construction vs reference ggml q6_K), extended to N output |
| 5 | // columns. Makes the batched lm_head/embed (perplexity, spec-decode verify, |
| 6 | // batched serving) correct on GPU while keeping the tiled convert the fast decode |
| 7 | // GEMV depends on. |
| 8 | // |
| 9 | // One work-item owns one output ROW for a block of BN columns. A work-group is |
| 10 | // {64 lanes, NTILES subgroups} = NTILES*64 rows; the global z dimension tiles the |
| 11 | // N columns by BN. Each work-item computes its row's FULL K (no K-split, so no |
| 12 | // cross-subgroup reduction), which lets the whole work-group share one staged |
| 13 | // activation block: |
| 14 | // |
| 15 | // __local activation staging — the BN columns of the current superblock (BN*256 |
| 16 | // floats) are loaded into __local once per superblock, cooperatively by all |
| 17 | // NTILES*64 work-items, then every row reads its activation from __local. This |
| 18 | // removes the ~Nrows-fold redundant image reads of the first version (each lane |
| 19 | // re-read the activation), which made the batched GEMM ~2x slower than the plain |
| 20 | // noshuffle GEMM. |
| 21 | // |
| 22 | // Weights are read from __global (coalesced) — matching the decode kernel; the |
| 23 | // lm_head weight is streamed with little reuse where coalesced global beats the |
| 24 | // Adreno texture cache. |
| 25 | |
| 26 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 27 | |
| 28 | #ifdef cl_qcom_reqd_sub_group_size |
| 29 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 30 | #define ADRENO_GPU 1 |
| 31 | #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) |
| 32 | #endif |
| 33 | |
| 34 | #define NTILES 4 // 64-row tiles per work-group (NTILES*64 = 256 rows) |
| 35 | #define TILE_ROWS 64 |
| 36 | #define BN 16 // output columns handled per work-group (global z step) |
| 37 | #define WG_THREADS (NTILES * TILE_ROWS) |
| 38 | |
| 39 | #if defined(ADRENO_GPU) |
| 40 | REQD_SUBGROUP_SIZE_64 |
| 41 | #endif |
| 42 | kernel void kernel_gemm_noshuffle_q6_K_f32_tiled( |
| 43 | __global uint4 * src0_ql, // tiled: 8 uint4 granules / superblock |
| 44 | __global uint4 * src0_qh, // tiled: 4 uint4 granules / superblock |
| 45 | __global char * src0_s, // tiled: 16 chars / superblock |
| 46 | __global half * src0_d, // tiled: 1 half / superblock |
| 47 | read_only image1d_buffer_t src1, // activation [ne00, ne11] f32 (RGBA), column-major |
| 48 | global float * dst, |
| 49 | ulong offsetd, |
| 50 | int ne00, |
| 51 | int ne01, |
| 52 | int ne11 |
| 53 | ) { |
| 54 | int rit = get_local_id(0); // 0..63 (lane within a tile; coalesces weight loads) |
| 55 | int sg = get_local_id(1); // 0..NTILES-1 |
| 56 | int lid = sg * TILE_ROWS + rit; // 0..WG_THREADS-1 (flat local id) |
| 57 | int row = get_group_id(0) * WG_THREADS + lid; |
| 58 | int rt = row / TILE_ROWS; // global 64-row tile index |
| 59 | int col0 = get_global_id(2) * BN; // first output column of this block |
| 60 | |
| 61 | int nb = ne00 / 256; // superblocks per row |
| 62 | int act_col_stride = ne00 / 4; // activation float4 pixels per column |
| 63 | |
| 64 | const bool row_ok = row < ne01; |
| 65 | |
| 66 | // staged activation: BN columns x 256 elements for the current superblock |
| 67 | __local float lact[BN * 256]; |
| 68 | |
| 69 | float acc[BN]; |
| 70 | #pragma unroll |
| 71 | for (int j = 0; j < BN; ++j) acc[j] = 0.0f; |
| 72 | |
| 73 | for (int sb = 0; sb < nb; ++sb) { |
| 74 | // cooperatively stage BN columns' 256 activation elements (= BN*64 float4) |
| 75 | for (int p = lid; p < BN * 64; p += WG_THREADS) { |
| 76 | int j = p >> 6; // column within the BN block (p / 64) |
| 77 | int e4 = p & 63; // element-quad within the column (p % 64) |
| 78 | int c = col0 + j; |
| 79 | float4 v = (c < ne11) |
| 80 | ? read_imagef(src1, c * act_col_stride + sb * 64 + e4) |
| 81 | : (float4)(0.0f); |
| 82 | lact[p * 4 + 0] = v.x; |
| 83 | lact[p * 4 + 1] = v.y; |
| 84 | lact[p * 4 + 2] = v.z; |
| 85 | lact[p * 4 + 3] = v.w; // lact[j*256 + e], e = e4*4 + t |
| 86 | } |
| 87 | barrier(CLK_LOCAL_MEM_FENCE); |
| 88 | |
| 89 | if (row_ok) { |
| 90 | int tile_blk = rt * nb + sb; // ne02 == 1 for lm_head/embed |
| 91 | |
| 92 | float dval = (float)src0_d[tile_blk * TILE_ROWS + rit]; |
| 93 | __global char * sc = src0_s + (tile_blk * TILE_ROWS + rit) * 16; |
| 94 | |
| 95 | uint ql[32]; |
| 96 | uint qh[16]; |
| 97 | #pragma unroll |
| 98 | for (int g = 0; g < 8; ++g) { |
| 99 | uint4 v = src0_ql[(tile_blk * 8 + g) * TILE_ROWS + rit]; |
| 100 | ql[g*4+0] = v.x; ql[g*4+1] = v.y; ql[g*4+2] = v.z; ql[g*4+3] = v.w; |
| 101 | } |
| 102 | #pragma unroll |
| 103 | for (int g = 0; g < 4; ++g) { |
| 104 | uint4 v = src0_qh[(tile_blk * 4 + g) * TILE_ROWS + rit]; |
| 105 | qh[g*4+0] = v.x; qh[g*4+1] = v.y; qh[g*4+2] = v.z; qh[g*4+3] = v.w; |
| 106 | } |
| 107 | |
| 108 | // NOTE: the e loop (256) is deliberately NOT unrolled. Fully unrolling |
| 109 | // 256*BN MACs overflows the in-process Adreno compiler (host stack |
| 110 | // overflow at clBuildProgram, same class as the FA DK=512 OOM). |
| 111 | for (int e = 0; e < 256; ++e) { |
| 112 | uint low4 = (ql[e >> 3] >> ((e & 7) * 4)) & 0xF; |
| 113 | uint hi2 = (qh[e >> 4] >> ((e & 15) * 2)) & 0x3; |
| 114 | int code = (int)(low4 | (hi2 << 4)) - 32; |
| 115 | int sidx = ((e >> 7) << 3) + (((e >> 5) & 3) << 1) + ((e >> 4) & 1); |
| 116 | float cs = (float)code * (float)sc[sidx] * dval; |
| 117 | #pragma unroll |
| 118 | for (int j = 0; j < BN; ++j) { |
| 119 | acc[j] += cs * lact[j * 256 + e]; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | barrier(CLK_LOCAL_MEM_FENCE); |
| 124 | } |
| 125 | |
| 126 | if (row_ok) { |
| 127 | dst = (global float*)((global char*)dst + offsetd); |
| 128 | #pragma unroll |
| 129 | for (int j = 0; j < BN; ++j) { |
| 130 | int c = col0 + j; |
| 131 | if (c < ne11) { |
| 132 | dst[(ulong)c * ne01 + row] = acc[j]; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |