interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-opencl / kernels / gemv_noshuffle_q6_k_f32_tiled.cl

196 lines · 8111 bytes Raw Blame History
1// Tiled-wide q6_K GEMV for the long-vocab lm_head/embed (decode path).
2//
3// Pairs with kernel_convert_block_q6_k_tiled_ns (cvt.cl): the weights are laid
4// out CANONICALLY (6-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 construction
7// against the reference ggml q6_K dequant, no bit-interleave reverse-engineering.
8//
9// One work-item produces one output row. A work-group is {64 lanes, 4 subgroups}:
10// the 64 lanes cover the 64 rows of one tile (coalesced reads), the 4 subgroups
11// split the K-blocks and reduce through __local at the end.
12//
13// Weights are read from __global (coalesced) rather than image1d_buffer: the
14// lm_head is read once per token with no reuse, and the Adreno texture cache
15// caps such a streaming read well below the coalesced-global rate
16// (see opencl_q6k_gemv_o4_shipped / x2-90 roofline notes).
17
18#pragma OPENCL EXTENSION cl_khr_fp16 : enable
19
20#ifdef cl_qcom_reqd_sub_group_size
21#pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable
22#define ADRENO_GPU 1
23#define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half")))
24#endif
25
26#define NSUBGROUPS 4
27#define TILE_ROWS 64
28
29#if defined(ADRENO_GPU)
30REQD_SUBGROUP_SIZE_64
31#endif
32kernel void kernel_gemv_noshuffle_q6_K_f32_tiled(
33 __global uint4 * src0_ql, // tiled: 8 uint4 granules / superblock
34 __global uint4 * src0_qh, // tiled: 4 uint4 granules / superblock
35 __global char * src0_s, // tiled: 16 chars / superblock
36 __global half * src0_d, // tiled: 1 half / superblock
37 read_only image1d_buffer_t src1, // activation (RGBA f32)
38 global float * dst,
39 ulong offsetd,
40 int ne00,
41 int ne01
42) {
43 int grp = get_local_id(1); // subgroup index 0..3 (splits K)
44 int row = get_global_id(0); // output row along ne01
45 int rt = row / TILE_ROWS;
46 int rit = row % TILE_ROWS;
47
48 int nb = ne00 / 256; // superblocks per row
49
50 float acc = 0.0f;
51
52 for (int sb = grp; sb < nb; sb += NSUBGROUPS) {
53 int tile_blk = rt * nb + sb; // ne02 == 1 for lm_head/embed
54
55 // d + 16 scales for this (row, superblock)
56 float dval = (float)src0_d[tile_blk * TILE_ROWS + rit];
57 __global char * sc = src0_s + (tile_blk * TILE_ROWS + rit) * 16;
58
59 // 32 ql-uints (8 codes/uint) + 16 qh-uints (16 codes/uint)
60 uint ql[32];
61 uint qh[16];
62 #pragma unroll
63 for (int g = 0; g < 8; ++g) {
64 uint4 v = src0_ql[(tile_blk * 8 + g) * TILE_ROWS + rit];
65 ql[g*4+0] = v.x; ql[g*4+1] = v.y; ql[g*4+2] = v.z; ql[g*4+3] = v.w;
66 }
67 #pragma unroll
68 for (int g = 0; g < 4; ++g) {
69 uint4 v = src0_qh[(tile_blk * 4 + g) * TILE_ROWS + rit];
70 qh[g*4+0] = v.x; qh[g*4+1] = v.y; qh[g*4+2] = v.z; qh[g*4+3] = v.w;
71 }
72
73 // dequant 256 codes in canonical e-order, MAC with activation.
74 int act_base = sb * 64; // activation float4 pixel base (256/4)
75 #pragma unroll
76 for (int e4 = 0; e4 < 64; ++e4) {
77 float4 a = read_imagef(src1, act_base + e4);
78 #pragma unroll
79 for (int t = 0; t < 4; ++t) {
80 int e = e4 * 4 + t;
81 uint low4 = (ql[e >> 3] >> ((e & 7) * 4)) & 0xF;
82 uint hi2 = (qh[e >> 4] >> ((e & 15) * 2)) & 0x3;
83 int code = (int)(low4 | (hi2 << 4)) - 32;
84 int sidx = ((e >> 7) << 3) + (((e >> 5) & 3) << 1) + ((e >> 4) & 1);
85 float scale = (float)sc[sidx] * dval;
86 float av = (t == 0) ? a.x : (t == 1) ? a.y : (t == 2) ? a.z : a.w;
87 acc += (float)code * scale * av;
88 }
89 }
90 }
91
92 // reduce across the NSUBGROUPS subgroups (same rit, different K-subset)
93 local float reduce_lm[NSUBGROUPS * TILE_ROWS];
94 reduce_lm[grp * TILE_ROWS + rit] = acc;
95 barrier(CLK_LOCAL_MEM_FENCE);
96
97 if (grp == 0) {
98 float total = reduce_lm[0 * TILE_ROWS + rit]
99 + reduce_lm[1 * TILE_ROWS + rit]
100 + reduce_lm[2 * TILE_ROWS + rit]
101 + reduce_lm[3 * TILE_ROWS + rit];
102 dst = (global float*)((global char*)dst + offsetd);
103 dst[row] = total;
104 }
105}
106
107// Multi-column (N=3) variant of the tiled q6_K decode GEMV, for the speculative/
108// MTP VERIFY lm_head/embed (ne1=3 = 2 drafts + 1 bonus). Identical tiled weight
109// layout + unpack as the ne1=1 kernel above; each WI computes 3 output columns,
110// streaming the (large) lm_head weight ONCE per superblock and reusing it across
111// the 3 verify activation columns (dequant once per code, MAC into 3 accs). This
112// is the lm_head analogue of the per-layer mc3 GEMV; the multiply order matches
113// the ne1=1 kernel, so each column is byte-identical to a standalone tiled GEMV.
114#if defined(ADRENO_GPU)
115REQD_SUBGROUP_SIZE_64
116#endif
117kernel void kernel_gemv_noshuffle_q6_K_f32_tiled_mc3(
118 __global uint4 * src0_ql,
119 __global uint4 * src0_qh,
120 __global char * src0_s,
121 __global half * src0_d,
122 read_only image1d_buffer_t src1,
123 global float * dst,
124 ulong offsetd,
125 int ne00,
126 int ne01
127) {
128 int grp = get_local_id(1);
129 int row = get_global_id(0);
130 int rt = row / TILE_ROWS;
131 int rit = row % TILE_ROWS;
132
133 int nb = ne00 / 256;
134 int col_stride = ne00 / 4; // activation float4 pixels per column
135
136 float acc0 = 0.0f, acc1 = 0.0f, acc2 = 0.0f;
137
138 for (int sb = grp; sb < nb; sb += NSUBGROUPS) {
139 int tile_blk = rt * nb + sb;
140
141 float dval = (float)src0_d[tile_blk * TILE_ROWS + rit];
142 __global char * sc = src0_s + (tile_blk * TILE_ROWS + rit) * 16;
143
144 uint ql[32];
145 uint qh[16];
146 #pragma unroll
147 for (int g = 0; g < 8; ++g) {
148 uint4 v = src0_ql[(tile_blk * 8 + g) * TILE_ROWS + rit];
149 ql[g*4+0] = v.x; ql[g*4+1] = v.y; ql[g*4+2] = v.z; ql[g*4+3] = v.w;
150 }
151 #pragma unroll
152 for (int g = 0; g < 4; ++g) {
153 uint4 v = src0_qh[(tile_blk * 4 + g) * TILE_ROWS + rit];
154 qh[g*4+0] = v.x; qh[g*4+1] = v.y; qh[g*4+2] = v.z; qh[g*4+3] = v.w;
155 }
156
157 int act_base = sb * 64;
158 #pragma unroll
159 for (int e4 = 0; e4 < 64; ++e4) {
160 float4 a0 = read_imagef(src1, 0*col_stride + act_base + e4);
161 float4 a1 = read_imagef(src1, 1*col_stride + act_base + e4);
162 float4 a2 = read_imagef(src1, 2*col_stride + act_base + e4);
163 #pragma unroll
164 for (int t = 0; t < 4; ++t) {
165 int e = e4 * 4 + t;
166 uint low4 = (ql[e >> 3] >> ((e & 7) * 4)) & 0xF;
167 uint hi2 = (qh[e >> 4] >> ((e & 15) * 2)) & 0x3;
168 int code = (int)(low4 | (hi2 << 4)) - 32;
169 int sidx = ((e >> 7) << 3) + (((e >> 5) & 3) << 1) + ((e >> 4) & 1);
170 float w = (float)code * ((float)sc[sidx] * dval); // dequant+scale once
171 float av0 = (t == 0) ? a0.x : (t == 1) ? a0.y : (t == 2) ? a0.z : a0.w;
172 float av1 = (t == 0) ? a1.x : (t == 1) ? a1.y : (t == 2) ? a1.z : a1.w;
173 float av2 = (t == 0) ? a2.x : (t == 1) ? a2.y : (t == 2) ? a2.z : a2.w;
174 acc0 += w * av0;
175 acc1 += w * av1;
176 acc2 += w * av2;
177 }
178 }
179 }
180
181 local float4 reduce_lm[NSUBGROUPS * TILE_ROWS];
182 reduce_lm[grp * TILE_ROWS + rit] = (float4)(acc0, acc1, acc2, 0.0f);
183 barrier(CLK_LOCAL_MEM_FENCE);
184
185 if (grp == 0) {
186 float4 total = reduce_lm[0 * TILE_ROWS + rit]
187 + reduce_lm[1 * TILE_ROWS + rit]
188 + reduce_lm[2 * TILE_ROWS + rit]
189 + reduce_lm[3 * TILE_ROWS + rit];
190 dst = (global float*)((global char*)dst + offsetd);
191 // dst column-major [ne01 rows x 3 cols]: (row, col) at col*ne01 + row
192 dst[0*ne01 + row] = total.x;
193 dst[1*ne01 + row] = total.y;
194 dst[2*ne01 + row] = total.z;
195 }
196}