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.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 3 | |
| 4 | #ifdef cl_qcom_reqd_sub_group_size |
| 5 | #pragma OPENCL EXTENSION cl_qcom_reqd_sub_group_size : enable |
| 6 | #define ADRENO_GPU 1 |
| 7 | #define REQD_SUBGROUP_SIZE_128 __attribute__((qcom_reqd_sub_group_size("full"))) |
| 8 | #define REQD_SUBGROUP_SIZE_64 __attribute__((qcom_reqd_sub_group_size("half"))) |
| 9 | #endif |
| 10 | |
| 11 | #ifdef ADRENO_GPU |
| 12 | REQD_SUBGROUP_SIZE_128 |
| 13 | #endif |
| 14 | kernel void kernel_gemm_noshuffle_q6_K_f32( |
| 15 | global const ushort * src0_ql, |
| 16 | global const uchar * src0_qh, |
| 17 | global const ushort * src0_s, |
| 18 | global const half * src0_d, |
| 19 | read_only image1d_buffer_t src1, |
| 20 | global float * dst, |
| 21 | ulong offsetd, |
| 22 | int m, |
| 23 | int n, |
| 24 | int k, |
| 25 | int n_no_padding, |
| 26 | ushort mask_f000, |
| 27 | uchar mask_c0 |
| 28 | ) { |
| 29 | dst = (global float *)( (global char *)dst + offsetd ); |
| 30 | |
| 31 | int m_4 = m >> 2; |
| 32 | int n_4 = n >> 2; |
| 33 | |
| 34 | int gy = get_global_id(0); // n |
| 35 | int gx = get_global_id(1); // m |
| 36 | int gx_2 = gx << 2; |
| 37 | |
| 38 | half8 c0 = 0, c1 = 0, c2 = 0, c3 = 0; |
| 39 | half8 B; |
| 40 | half4 dequantized_weights; |
| 41 | |
| 42 | global const ushort * ptr_ql = src0_ql + gx_2; |
| 43 | global const uchar * ptr_qh = src0_qh + gx_2; |
| 44 | global const ushort * ptr_s = src0_s + gx_2; |
| 45 | global const half * ptr_d = src0_d + gx_2; |
| 46 | |
| 47 | for (int i = 0; i < k; i += 4) { |
| 48 | // load 4x elements (ushort) of ql on M, each ushort contains 4 weights |
| 49 | // 4x ushort correspons to 4 rows on M |
| 50 | ushort4 bits4 = vload4(0, ptr_ql + (i/4)*m); // ql packed in 4s in ushort |
| 51 | uchar4 bits2 = vload4(0, ptr_qh + (i/4)*m); // qh packed in 4s in uchar |
| 52 | |
| 53 | // load 4 consecutive scales |
| 54 | char8 scale_s_8 = as_char8(vload4(0, ptr_s + (i/16/2)*m)); // 1 char scale every 16 elements, packed in 2s |
| 55 | char4 scale_s = ((i/16) % 2) == 0 ? scale_s_8.s0246 : scale_s_8.s1357; // transposed as ushort, 2 blocks |
| 56 | half4 scale_d = vload4(0, ptr_d + (i/256)*m); // 1 half scale every 256 elements |
| 57 | |
| 58 | // j=0 |
| 59 | // load 2x 4 elements of activations on N, corresponding to 8 rows on N |
| 60 | B.s0123 = read_imageh(src1, gy*2 + (i + 0)*n_4 + 0); |
| 61 | B.s4567 = read_imageh(src1, gy*2 + (i + 0)*n_4 + 1); |
| 62 | dequantized_weights.s0 = (convert_half((bits4.s0 & 0x000F) | ((bits2.s0 & 0x03) << 4)) - 32.f) * scale_s.s0 * scale_d.s0; |
| 63 | dequantized_weights.s1 = (convert_half((bits4.s1 & 0x000F) | ((bits2.s1 & 0x03) << 4)) - 32.f) * scale_s.s1 * scale_d.s1; |
| 64 | dequantized_weights.s2 = (convert_half((bits4.s2 & 0x000F) | ((bits2.s2 & 0x03) << 4)) - 32.f) * scale_s.s2 * scale_d.s2; |
| 65 | dequantized_weights.s3 = (convert_half((bits4.s3 & 0x000F) | ((bits2.s3 & 0x03) << 4)) - 32.f) * scale_s.s3 * scale_d.s3; |
| 66 | c0 += B * dequantized_weights.s0; |
| 67 | c1 += B * dequantized_weights.s1; |
| 68 | c2 += B * dequantized_weights.s2; |
| 69 | c3 += B * dequantized_weights.s3; |
| 70 | |
| 71 | // j=1 |
| 72 | B.s0123 = read_imageh(src1, gy*2 + (i + 1)*n_4 + 0); |
| 73 | B.s4567 = read_imageh(src1, gy*2 + (i + 1)*n_4 + 1); |
| 74 | dequantized_weights.s0 = (convert_half((((bits4.s0 & 0x00F0) >> 4) | ((bits2.s0 & 0x0C) << 2))) - 32.f) * scale_s.s0 * scale_d.s0; |
| 75 | dequantized_weights.s1 = (convert_half((((bits4.s1 & 0x00F0) >> 4) | ((bits2.s1 & 0x0C) << 2))) - 32.f) * scale_s.s1 * scale_d.s1; |
| 76 | dequantized_weights.s2 = (convert_half((((bits4.s2 & 0x00F0) >> 4) | ((bits2.s2 & 0x0C) << 2))) - 32.f) * scale_s.s2 * scale_d.s2; |
| 77 | dequantized_weights.s3 = (convert_half((((bits4.s3 & 0x00F0) >> 4) | ((bits2.s3 & 0x0C) << 2))) - 32.f) * scale_s.s3 * scale_d.s3; |
| 78 | c0 += B * dequantized_weights.s0; |
| 79 | c1 += B * dequantized_weights.s1; |
| 80 | c2 += B * dequantized_weights.s2; |
| 81 | c3 += B * dequantized_weights.s3; |
| 82 | |
| 83 | // j=2 |
| 84 | B.s0123 = read_imageh(src1, gy*2 + (i + 2)*n_4 + 0); |
| 85 | B.s4567 = read_imageh(src1, gy*2 + (i + 2)*n_4 + 1); |
| 86 | dequantized_weights.s0 = (convert_half((((bits4.s0 & 0x0F00) >> 8) | (bits2.s0 & 0x30))) - 32.f) * scale_s.s0 * scale_d.s0; |
| 87 | dequantized_weights.s1 = (convert_half((((bits4.s1 & 0x0F00) >> 8) | (bits2.s1 & 0x30))) - 32.f) * scale_s.s1 * scale_d.s1; |
| 88 | dequantized_weights.s2 = (convert_half((((bits4.s2 & 0x0F00) >> 8) | (bits2.s2 & 0x30))) - 32.f) * scale_s.s2 * scale_d.s2; |
| 89 | dequantized_weights.s3 = (convert_half((((bits4.s3 & 0x0F00) >> 8) | (bits2.s3 & 0x30))) - 32.f) * scale_s.s3 * scale_d.s3; |
| 90 | c0 += B * dequantized_weights.s0; |
| 91 | c1 += B * dequantized_weights.s1; |
| 92 | c2 += B * dequantized_weights.s2; |
| 93 | c3 += B * dequantized_weights.s3; |
| 94 | |
| 95 | // j=3 |
| 96 | B.s0123 = read_imageh(src1, gy*2 + (i + 3)*n_4 + 0); |
| 97 | B.s4567 = read_imageh(src1, gy*2 + (i + 3)*n_4 + 1); |
| 98 | dequantized_weights.s0 = (convert_half((((bits4.s0 & mask_f000) >> 12) | ((bits2.s0 & mask_c0) >> 2))) - 32.f) * scale_s.s0 * scale_d.s0; |
| 99 | dequantized_weights.s1 = (convert_half((((bits4.s1 & mask_f000) >> 12) | ((bits2.s1 & mask_c0) >> 2))) - 32.f) * scale_s.s1 * scale_d.s1; |
| 100 | dequantized_weights.s2 = (convert_half((((bits4.s2 & mask_f000) >> 12) | ((bits2.s2 & mask_c0) >> 2))) - 32.f) * scale_s.s2 * scale_d.s2; |
| 101 | dequantized_weights.s3 = (convert_half((((bits4.s3 & mask_f000) >> 12) | ((bits2.s3 & mask_c0) >> 2))) - 32.f) * scale_s.s3 * scale_d.s3; |
| 102 | c0 += B * dequantized_weights.s0; |
| 103 | c1 += B * dequantized_weights.s1; |
| 104 | c2 += B * dequantized_weights.s2; |
| 105 | c3 += B * dequantized_weights.s3; |
| 106 | } |
| 107 | |
| 108 | int idx = (gy<<3)*m + (gx<<2); |
| 109 | |
| 110 | if(idx+3 < m*n_no_padding){ |
| 111 | vstore4((float4)(c0.s0, c1.s0, c2.s0, c3.s0), 0, dst + idx); |
| 112 | idx += m; |
| 113 | } |
| 114 | if(idx+3 < m*n_no_padding){ |
| 115 | vstore4((float4)(c0.s1, c1.s1, c2.s1, c3.s1), 0, dst + idx); |
| 116 | idx += m; |
| 117 | } |
| 118 | if(idx+3 < m*n_no_padding){ |
| 119 | vstore4((float4)(c0.s2, c1.s2, c2.s2, c3.s2), 0, dst + idx); |
| 120 | idx += m; |
| 121 | } |
| 122 | if(idx+3 < m*n_no_padding){ |
| 123 | vstore4((float4)(c0.s3, c1.s3, c2.s3, c3.s3), 0, dst + idx); |
| 124 | idx += m; |
| 125 | } |
| 126 | if(idx+3 < m*n_no_padding){ |
| 127 | vstore4((float4)(c0.s4, c1.s4, c2.s4, c3.s4), 0, dst + idx); |
| 128 | idx += m; |
| 129 | } |
| 130 | if(idx+3 < m*n_no_padding){ |
| 131 | vstore4((float4)(c0.s5, c1.s5, c2.s5, c3.s5), 0, dst + idx); |
| 132 | idx += m; |
| 133 | } |
| 134 | if(idx+3 < m*n_no_padding){ |
| 135 | vstore4((float4)(c0.s6, c1.s6, c2.s6, c3.s6), 0, dst + idx); |
| 136 | idx += m; |
| 137 | } |
| 138 | if(idx+3 < m*n_no_padding){ |
| 139 | vstore4((float4)(c0.s7, c1.s7, c2.s7, c3.s7), 0, dst + idx); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Cooperative-K q6_K GEMM for the small-batch (n_q in [2..8]) path. Same idea |
| 144 | // as the q4_K _cok kernel: WG = (COK_SG lanes x COK_NSG subgroups), each lane |
| 145 | // owns ONE output row (half8 over the 8 padded cols), and the COK_NSG |
| 146 | // subgroups split the K iterations round-robin and combine via a __local |
| 147 | // reduction. Replaces the default 4-row-per-WI tile that walked all of K alone |
| 148 | // (~M/512 WGs + serial reduction) at small n_q. REQD_SUBGROUP_SIZE_64 + |
| 149 | // barrier (never sub_group_reduce at full width on X2). |
| 150 | #define COK_NSG 8 |
| 151 | #define COK_SG 64 |
| 152 | #ifdef ADRENO_GPU |
| 153 | REQD_SUBGROUP_SIZE_64 |
| 154 | #endif |
| 155 | kernel void kernel_gemm_noshuffle_q6_K_f32_cok( |
| 156 | global const ushort * src0_ql, |
| 157 | global const uchar * src0_qh, |
| 158 | global const ushort * src0_s, |
| 159 | global const half * src0_d, |
| 160 | read_only image1d_buffer_t src1, |
| 161 | global float * dst, |
| 162 | ulong offsetd, |
| 163 | int m, |
| 164 | int n, |
| 165 | int k, |
| 166 | int n_no_padding, |
| 167 | ushort mask_f000, |
| 168 | uchar mask_c0 |
| 169 | ) { |
| 170 | dst = (global float *)( (global char *)dst + offsetd ); |
| 171 | |
| 172 | int n_4 = n >> 2; |
| 173 | int gx = get_global_id(0); // output row |
| 174 | int sg = get_local_id(1); // subgroup index (K-split) |
| 175 | int lane = get_local_id(0); // lane within subgroup |
| 176 | |
| 177 | global const ushort * ptr_ql = src0_ql + gx; |
| 178 | global const uchar * ptr_qh = src0_qh + gx; |
| 179 | global const ushort * ptr_s = src0_s + gx; |
| 180 | global const half * ptr_d = src0_d + gx; |
| 181 | |
| 182 | half8 acc = 0; |
| 183 | half8 B; |
| 184 | half dq; |
| 185 | |
| 186 | int num_iter = k >> 2; // k/4 iterations, 4 k-values each |
| 187 | |
| 188 | for (int ib = sg; ib < num_iter; ib += COK_NSG) { |
| 189 | int i = ib << 2; // ib * 4 |
| 190 | |
| 191 | ushort bits4 = ptr_ql[ib * m]; // ql for row gx at this 4-block |
| 192 | uchar bits2 = ptr_qh[ib * m]; // qh |
| 193 | |
| 194 | ushort s_packed = ptr_s[(i >> 5) * m]; // (i/16/2) = i/32 |
| 195 | char2 sc2 = as_char2(s_packed); |
| 196 | char scale_s = (((i >> 4) & 1) == 0) ? sc2.s0 : sc2.s1; // (i/16)%2 |
| 197 | half scale_d = ptr_d[(i >> 8) * m]; // i/256 |
| 198 | |
| 199 | // j=0 |
| 200 | B.s0123 = read_imageh(src1, (i + 0)*n_4 + 0); |
| 201 | B.s4567 = read_imageh(src1, (i + 0)*n_4 + 1); |
| 202 | dq = (convert_half((bits4 & 0x000F) | ((bits2 & 0x03) << 4)) - 32.f) * scale_s * scale_d; |
| 203 | acc += B * dq; |
| 204 | |
| 205 | // j=1 |
| 206 | B.s0123 = read_imageh(src1, (i + 1)*n_4 + 0); |
| 207 | B.s4567 = read_imageh(src1, (i + 1)*n_4 + 1); |
| 208 | dq = (convert_half(((bits4 & 0x00F0) >> 4) | ((bits2 & 0x0C) << 2)) - 32.f) * scale_s * scale_d; |
| 209 | acc += B * dq; |
| 210 | |
| 211 | // j=2 |
| 212 | B.s0123 = read_imageh(src1, (i + 2)*n_4 + 0); |
| 213 | B.s4567 = read_imageh(src1, (i + 2)*n_4 + 1); |
| 214 | dq = (convert_half(((bits4 & 0x0F00) >> 8) | (bits2 & 0x30)) - 32.f) * scale_s * scale_d; |
| 215 | acc += B * dq; |
| 216 | |
| 217 | // j=3 |
| 218 | B.s0123 = read_imageh(src1, (i + 3)*n_4 + 0); |
| 219 | B.s4567 = read_imageh(src1, (i + 3)*n_4 + 1); |
| 220 | dq = (convert_half(((bits4 & mask_f000) >> 12) | ((bits2 & mask_c0) >> 2)) - 32.f) * scale_s * scale_d; |
| 221 | acc += B * dq; |
| 222 | } |
| 223 | |
| 224 | local float8 reduceLM[COK_SG * (COK_NSG - 1)]; |
| 225 | if (sg > 0) { |
| 226 | reduceLM[(sg - 1) * COK_SG + lane] = convert_float8(acc); |
| 227 | } |
| 228 | barrier(CLK_LOCAL_MEM_FENCE); |
| 229 | |
| 230 | if (sg == 0) { |
| 231 | float8 sum = convert_float8(acc); |
| 232 | for (int s = 0; s < COK_NSG - 1; s++) { |
| 233 | sum += reduceLM[s * COK_SG + lane]; |
| 234 | } |
| 235 | int idx = gx; |
| 236 | if (idx < m*n_no_padding) { dst[idx] = sum.s0; idx += m; } |
| 237 | if (idx < m*n_no_padding) { dst[idx] = sum.s1; idx += m; } |
| 238 | if (idx < m*n_no_padding) { dst[idx] = sum.s2; idx += m; } |
| 239 | if (idx < m*n_no_padding) { dst[idx] = sum.s3; idx += m; } |
| 240 | if (idx < m*n_no_padding) { dst[idx] = sum.s4; idx += m; } |
| 241 | if (idx < m*n_no_padding) { dst[idx] = sum.s5; idx += m; } |
| 242 | if (idx < m*n_no_padding) { dst[idx] = sum.s6; idx += m; } |
| 243 | if (idx < m*n_no_padding) { dst[idx] = sum.s7; } |
| 244 | } |
| 245 | } |