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 / gemv_noshuffle_q4_k_f32_tiled.cl
| 1 | // Tiled-wide q4_K GEMV for the long-vocab lm_head/embed (decode path). |
| 2 | // |
| 3 | // Pairs with kernel_convert_block_q4_k_tiled_ns (cvt.cl): the weights are laid |
| 4 | // out CANONICALLY (4-bit code in element order e in [0,256)) and TILED by 64 |
| 5 | // output rows so the 64-thread lane group coalesces every weight load. Both the |
| 6 | // pack (convert) and the unpack (here) are owned by us -> correct by |
| 7 | // construction vs the reference ggml q4_K dequant. Same structure as the q6_K |
| 8 | // tiled GEMV; the only differences are the 4-bit dequant and the q4_K |
| 9 | // scale/min decode (get_scale_min_k4 from the packed 12-byte block). |
| 10 | // |
| 11 | // One work-item produces one output row. WG = {64 lanes, 4 subgroups}: the 64 |
| 12 | // lanes cover the 64 rows of one tile (coalesced uint4 reads), the 4 subgroups |
| 13 | // split the K-blocks and reduce through __local at the end. Weights read from |
| 14 | // __global (lm_head is streamed once per token; texture cache caps it below the |
| 15 | // coalesced-global rate). |
| 16 | |
| 17 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 18 | |
| 19 | #ifdef cl_qcom_reqd_sub_group_size |
| 20 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 21 | #define ADRENO_GPU 1 |
| 22 | #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) |
| 23 | #endif |
| 24 | |
| 25 | #define QK_K 256 |
| 26 | #define NSUBGROUPS 4 |
| 27 | #define TILE_ROWS 64 |
| 28 | |
| 29 | // Decode one q4_K sub-block scale + min from the packed 12-byte block. |
| 30 | // Identical to the o4 kernel's helper (masks hard-coded: d6=0x3F, d4=0x0F, hi2=0xC0). |
| 31 | inline void q4k_scale_min(int j, __global const uchar * q, uchar * d, uchar * m) { |
| 32 | if (j < 4) { |
| 33 | *d = q[j] & 0x3F; |
| 34 | *m = q[j+4] & 0x3F; |
| 35 | } else { |
| 36 | *d = (q[j+4] & 0x0F) | ((q[j-4] & 0xC0) >> 2); |
| 37 | *m = ((q[j+4] >> 4) & 0x0F) | ((q[j] & 0xC0) >> 2); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | #if defined(ADRENO_GPU) |
| 42 | REQD_SUBGROUP_SIZE_64 |
| 43 | #endif |
| 44 | kernel void kernel_gemv_noshuffle_q4_k_f32_tiled( |
| 45 | __global uint4 * src0_q, // tiled: 8 uint4 granules / superblock (4-bit codes) |
| 46 | __global half * src0_d, // tiled: 1 half / superblock |
| 47 | __global half * src0_dm, // tiled: 1 half / superblock |
| 48 | __global uchar * src0_s, // tiled: 12 bytes / superblock (packed scales) |
| 49 | read_only image1d_buffer_t src1, // activation (RGBA f32) |
| 50 | global float * dst, |
| 51 | ulong offsetd, |
| 52 | int ne00, |
| 53 | int ne01 |
| 54 | ) { |
| 55 | int grp = get_local_id(1); // subgroup index 0..3 (splits K) |
| 56 | int row = get_global_id(0); // output row along ne01 |
| 57 | int rt = row / TILE_ROWS; |
| 58 | int rit = row % TILE_ROWS; |
| 59 | |
| 60 | int nb = ne00 / QK_K; // superblocks per row |
| 61 | |
| 62 | float acc = 0.0f; |
| 63 | |
| 64 | for (int sb = grp; sb < nb; sb += NSUBGROUPS) { |
| 65 | int tile_blk = rt * nb + sb; // ne02 == 1 for lm_head/embed |
| 66 | |
| 67 | float dval = (float)src0_d [tile_blk * TILE_ROWS + rit]; |
| 68 | float dmval = (float)src0_dm[tile_blk * TILE_ROWS + rit]; |
| 69 | |
| 70 | // decode the 8 sub-block (scale, min) pairs |
| 71 | __global uchar * sc = src0_s + (tile_blk * TILE_ROWS + rit) * 12; |
| 72 | float scale[8], minv[8]; |
| 73 | #pragma unroll |
| 74 | for (int is = 0; is < 8; ++is) { |
| 75 | uchar sd, sm; |
| 76 | q4k_scale_min(is, sc, &sd, &sm); |
| 77 | scale[is] = dval * (float)sd; |
| 78 | minv[is] = dmval * (float)sm; |
| 79 | } |
| 80 | |
| 81 | // 32 uints of 4-bit codes (8 codes/uint), e-order |
| 82 | uint q[32]; |
| 83 | #pragma unroll |
| 84 | for (int g = 0; g < 8; ++g) { |
| 85 | uint4 v = src0_q[(tile_blk * 8 + g) * TILE_ROWS + rit]; |
| 86 | q[g*4+0] = v.x; q[g*4+1] = v.y; q[g*4+2] = v.z; q[g*4+3] = v.w; |
| 87 | } |
| 88 | |
| 89 | // dequant 256 codes in canonical e-order, MAC with activation. |
| 90 | int act_base = sb * 64; // activation float4 pixel base (256/4) |
| 91 | #pragma unroll |
| 92 | for (int e4 = 0; e4 < 64; ++e4) { |
| 93 | float4 a = read_imagef(src1, act_base + e4); |
| 94 | #pragma unroll |
| 95 | for (int t = 0; t < 4; ++t) { |
| 96 | int e = e4 * 4 + t; |
| 97 | uint code = (q[e >> 3] >> ((e & 7) * 4)) & 0xF; |
| 98 | int is = e >> 5; // sub-block index = e/32 |
| 99 | float av = (t == 0) ? a.x : (t == 1) ? a.y : (t == 2) ? a.z : a.w; |
| 100 | acc += ((float)code * scale[is] - minv[is]) * av; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // reduce across the NSUBGROUPS subgroups (same rit, different K-subset) |
| 106 | local float reduce_lm[NSUBGROUPS * TILE_ROWS]; |
| 107 | reduce_lm[grp * TILE_ROWS + rit] = acc; |
| 108 | barrier(CLK_LOCAL_MEM_FENCE); |
| 109 | |
| 110 | if (grp == 0) { |
| 111 | float total = reduce_lm[0 * TILE_ROWS + rit] |
| 112 | + reduce_lm[1 * TILE_ROWS + rit] |
| 113 | + reduce_lm[2 * TILE_ROWS + rit] |
| 114 | + reduce_lm[3 * TILE_ROWS + rit]; |
| 115 | dst = (global float*)((global char*)dst + offsetd); |
| 116 | dst[row] = total; |
| 117 | } |
| 118 | } |