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-opencl/kernels/gemv_noshuffle_q4_0_f32.cl # 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-opencl / kernels / gemv_noshuffle_q4_0_f32.cl` 398 lines · 22301 bytes Raw Blame History 1 #pragma OPENCL EXTENSION cl_khr_fp16 : enable 2 #pragma OPENCL EXTENSION cl_khr_subgroups : 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_64 __attribute__((qcom_reqd_sub_group_size("half"))) 8 #endif 9 10 // A6X compiler incorrectly constant-folds get_local_size() results; 11 // force runtime materialization via a no-op ALU round-trip. 12 #ifdef GGML_CL_A6X_CONSTFOLD_FIX 13 #define MATERIALIZE_WG(x) do { (x) *= 2u; if ((x) > 1u) (x) /= 2u; } while(0) 14 #else 15 #define MATERIALIZE_WG(x) 16 #endif 17 18 // assume 19 #define QK4_0 32 20 #define N_SIMDGROUP 4 21 22 #define dequantizeBlockAccum_ns_sgbroadcast_1_hi(total_sums, bits4, scale, y) \ 23 float shared_y; \ 24 shared_y = sub_group_broadcast(y.s0, 0); \ 25 total_sums.s0 += ((bits4.s0 & 0x000F) - 8) * scale.s0 * shared_y; \ 26 total_sums.s1 += ((bits4.s1 & 0x000F) - 8) * scale.s1 * shared_y; \ 27 shared_y = sub_group_broadcast(y.s1, 0); \ 28 total_sums.s0 += (((bits4.s0 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y; \ 29 total_sums.s1 += (((bits4.s1 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y; \ 30 shared_y = sub_group_broadcast(y.s2, 0); \ 31 total_sums.s0 += (((bits4.s0 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y; \ 32 total_sums.s1 += (((bits4.s1 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y; \ 33 shared_y = sub_group_broadcast(y.s3, 0); \ 34 total_sums.s0 += (((bits4.s0 & 0xF000) >> 12) - 8) * scale.s0 * shared_y; \ 35 total_sums.s1 += (((bits4.s1 & 0xF000) >> 12) - 8) * scale.s1 * shared_y; \ 36 shared_y = sub_group_broadcast(y.s4, 0); \ 37 total_sums.s0 += ((bits4.s2 & 0x000F) - 8) * scale.s0 * shared_y; \ 38 total_sums.s1 += ((bits4.s3 & 0x000F) - 8) * scale.s1 * shared_y; \ 39 shared_y = sub_group_broadcast(y.s5, 0); \ 40 total_sums.s0 += (((bits4.s2 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y; \ 41 total_sums.s1 += (((bits4.s3 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y; \ 42 shared_y = sub_group_broadcast(y.s6, 0); \ 43 total_sums.s0 += (((bits4.s2 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y; \ 44 total_sums.s1 += (((bits4.s3 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y; \ 45 shared_y = sub_group_broadcast(y.s7, 0); \ 46 total_sums.s0 += (((bits4.s2 & 0xF000) >> 12) - 8) * scale.s0 * shared_y; \ 47 total_sums.s1 += (((bits4.s3 & 0xF000) >> 12) - 8) * scale.s1 * shared_y; \ 48 shared_y = sub_group_broadcast(y.s0, 1); \ 49 total_sums.s0 += ((bits4.s4 & 0x000F) - 8) * scale.s0 * shared_y; \ 50 total_sums.s1 += ((bits4.s5 & 0x000F) - 8) * scale.s1 * shared_y; \ 51 shared_y = sub_group_broadcast(y.s1, 1); \ 52 total_sums.s0 += (((bits4.s4 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y; \ 53 total_sums.s1 += (((bits4.s5 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y; \ 54 shared_y = sub_group_broadcast(y.s2, 1); \ 55 total_sums.s0 += (((bits4.s4 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y; \ 56 total_sums.s1 += (((bits4.s5 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y; \ 57 shared_y = sub_group_broadcast(y.s3, 1); \ 58 total_sums.s0 += (((bits4.s4 & 0xF000) >> 12) - 8) * scale.s0 * shared_y; \ 59 total_sums.s1 += (((bits4.s5 & 0xF000) >> 12) - 8) * scale.s1 * shared_y; \ 60 shared_y = sub_group_broadcast(y.s4, 1); \ 61 total_sums.s0 += ((bits4.s6 & 0x000F) - 8) * scale.s0 * shared_y; \ 62 total_sums.s1 += ((bits4.s7 & 0x000F) - 8) * scale.s1 * shared_y; \ 63 shared_y = sub_group_broadcast(y.s5, 1); \ 64 total_sums.s0 += (((bits4.s6 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y; \ 65 total_sums.s1 += (((bits4.s7 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y; \ 66 shared_y = sub_group_broadcast(y.s6, 1); \ 67 total_sums.s0 += (((bits4.s6 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y; \ 68 total_sums.s1 += (((bits4.s7 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y; \ 69 shared_y = sub_group_broadcast(y.s7, 1); \ 70 total_sums.s0 += (((bits4.s6 & 0xF000) >> 12) - 8) * scale.s0 * shared_y; \ 71 total_sums.s1 += (((bits4.s7 & 0xF000) >> 12) - 8) * scale.s1 * shared_y; \ 72 73 74 #define dequantizeBlockAccum_ns_sgbroadcast_1_lo(total_sums, bits4, scale, y) \ 75 shared_y = sub_group_broadcast(y.s0, 2); \ 76 total_sums.s0 += ((bits4.s0 & 0x000F) - 8) * scale.s0 * shared_y; \ 77 total_sums.s1 += ((bits4.s1 & 0x000F) - 8) * scale.s1 * shared_y; \ 78 shared_y = sub_group_broadcast(y.s1, 2); \ 79 total_sums.s0 += (((bits4.s0 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y; \ 80 total_sums.s1 += (((bits4.s1 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y; \ 81 shared_y = sub_group_broadcast(y.s2, 2); \ 82 total_sums.s0 += (((bits4.s0 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y; \ 83 total_sums.s1 += (((bits4.s1 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y; \ 84 shared_y = sub_group_broadcast(y.s3, 2); \ 85 total_sums.s0 += (((bits4.s0 & 0xF000) >> 12) - 8) * scale.s0 * shared_y; \ 86 total_sums.s1 += (((bits4.s1 & 0xF000) >> 12) - 8) * scale.s1 * shared_y; \ 87 shared_y = sub_group_broadcast(y.s4, 2); \ 88 total_sums.s0 += ((bits4.s2 & 0x000F) - 8) * scale.s0 * shared_y; \ 89 total_sums.s1 += ((bits4.s3 & 0x000F) - 8) * scale.s1 * shared_y; \ 90 shared_y = sub_group_broadcast(y.s5, 2); \ 91 total_sums.s0 += (((bits4.s2 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y; \ 92 total_sums.s1 += (((bits4.s3 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y; \ 93 shared_y = sub_group_broadcast(y.s6, 2); \ 94 total_sums.s0 += (((bits4.s2 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y; \ 95 total_sums.s1 += (((bits4.s3 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y; \ 96 shared_y = sub_group_broadcast(y.s7, 2); \ 97 total_sums.s0 += (((bits4.s2 & 0xF000) >> 12) - 8) * scale.s0 * shared_y; \ 98 total_sums.s1 += (((bits4.s3 & 0xF000) >> 12) - 8) * scale.s1 * shared_y; \ 99 shared_y = sub_group_broadcast(y.s0, 3); \ 100 total_sums.s0 += ((bits4.s4 & 0x000F) - 8) * scale.s0 * shared_y; \ 101 total_sums.s1 += ((bits4.s5 & 0x000F) - 8) * scale.s1 * shared_y; \ 102 shared_y = sub_group_broadcast(y.s1, 3); \ 103 total_sums.s0 += (((bits4.s4 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y; \ 104 total_sums.s1 += (((bits4.s5 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y; \ 105 shared_y = sub_group_broadcast(y.s2, 3); \ 106 total_sums.s0 += (((bits4.s4 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y; \ 107 total_sums.s1 += (((bits4.s5 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y; \ 108 shared_y = sub_group_broadcast(y.s3, 3); \ 109 total_sums.s0 += (((bits4.s4 & 0xF000) >> 12) - 8) * scale.s0 * shared_y; \ 110 total_sums.s1 += (((bits4.s5 & 0xF000) >> 12) - 8) * scale.s1 * shared_y; \ 111 shared_y = sub_group_broadcast(y.s4, 3); \ 112 total_sums.s0 += ((bits4.s6 & 0x000F) - 8) * scale.s0 * shared_y; \ 113 total_sums.s1 += ((bits4.s7 & 0x000F) - 8) * scale.s1 * shared_y; \ 114 shared_y = sub_group_broadcast(y.s5, 3); \ 115 total_sums.s0 += (((bits4.s6 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y; \ 116 total_sums.s1 += (((bits4.s7 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y; \ 117 shared_y = sub_group_broadcast(y.s6, 3); \ 118 total_sums.s0 += (((bits4.s6 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y; \ 119 total_sums.s1 += (((bits4.s7 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y; \ 120 shared_y = sub_group_broadcast(y.s7, 3); \ 121 total_sums.s0 += (((bits4.s6 & 0xF000) >> 12) - 8) * scale.s0 * shared_y; \ 122 total_sums.s1 += (((bits4.s7 & 0xF000) >> 12) - 8) * scale.s1 * shared_y; \ 123 124 125 #define dequantizeBlockAccum_ns_sgbroadcast_8_hi(total_sums, bits4, scale, y) \ 126 float8 shared_y; \ 127 shared_y = sub_group_broadcast(y, 0); \ 128 total_sums.s0 += ((bits4.s0 & 0x000F) - 8) * scale.s0 * shared_y.s0; \ 129 total_sums.s0 += (((bits4.s0 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y.s1; \ 130 total_sums.s0 += (((bits4.s0 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y.s2; \ 131 total_sums.s0 += (((bits4.s0 & 0xF000) >> 12) - 8) * scale.s0 * shared_y.s3; \ 132 total_sums.s0 += ((bits4.s2 & 0x000F) - 8) * scale.s0 * shared_y.s4; \ 133 total_sums.s0 += (((bits4.s2 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y.s5; \ 134 total_sums.s0 += (((bits4.s2 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y.s6; \ 135 total_sums.s0 += (((bits4.s2 & 0xF000) >> 12) - 8) * scale.s0 * shared_y.s7; \ 136 total_sums.s1 += ((bits4.s1 & 0x000F) - 8) * scale.s1 * shared_y.s0; \ 137 total_sums.s1 += (((bits4.s1 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y.s1; \ 138 total_sums.s1 += (((bits4.s1 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y.s2; \ 139 total_sums.s1 += (((bits4.s1 & 0xF000) >> 12) - 8) * scale.s1 * shared_y.s3; \ 140 total_sums.s1 += ((bits4.s3 & 0x000F) - 8) * scale.s1 * shared_y.s4; \ 141 total_sums.s1 += (((bits4.s3 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y.s5; \ 142 total_sums.s1 += (((bits4.s3 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y.s6; \ 143 total_sums.s1 += (((bits4.s3 & 0xF000) >> 12) - 8) * scale.s1 * shared_y.s7; \ 144 shared_y = sub_group_broadcast(y, 1); \ 145 total_sums.s0 += ((bits4.s4 & 0x000F) - 8) * scale.s0 * shared_y.s0; \ 146 total_sums.s0 += (((bits4.s4 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y.s1; \ 147 total_sums.s0 += (((bits4.s4 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y.s2; \ 148 total_sums.s0 += (((bits4.s4 & 0xF000) >> 12) - 8) * scale.s0 * shared_y.s3; \ 149 total_sums.s0 += ((bits4.s6 & 0x000F) - 8) * scale.s0 * shared_y.s4; \ 150 total_sums.s0 += (((bits4.s6 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y.s5; \ 151 total_sums.s0 += (((bits4.s6 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y.s6; \ 152 total_sums.s0 += (((bits4.s6 & 0xF000) >> 12) - 8) * scale.s0 * shared_y.s7; \ 153 total_sums.s1 += ((bits4.s5 & 0x000F) - 8) * scale.s1 * shared_y.s0; \ 154 total_sums.s1 += (((bits4.s5 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y.s1; \ 155 total_sums.s1 += (((bits4.s5 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y.s2; \ 156 total_sums.s1 += (((bits4.s5 & 0xF000) >> 12) - 8) * scale.s1 * shared_y.s3; \ 157 total_sums.s1 += ((bits4.s7 & 0x000F) - 8) * scale.s1 * shared_y.s4; \ 158 total_sums.s1 += (((bits4.s7 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y.s5; \ 159 total_sums.s1 += (((bits4.s7 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y.s6; \ 160 total_sums.s1 += (((bits4.s7 & 0xF000) >> 12) - 8) * scale.s1 * shared_y.s7; \ 161 162 163 #define dequantizeBlockAccum_ns_sgbroadcast_8_lo(total_sums, bits4, scale, y) \ 164 shared_y = sub_group_broadcast(y, 2); \ 165 total_sums.s0 += ((bits4.s0 & 0x000F) - 8) * scale.s0 * shared_y.s0; \ 166 total_sums.s0 += (((bits4.s0 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y.s1; \ 167 total_sums.s0 += (((bits4.s0 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y.s2; \ 168 total_sums.s0 += (((bits4.s0 & 0xF000) >> 12) - 8) * scale.s0 * shared_y.s3; \ 169 total_sums.s0 += ((bits4.s2 & 0x000F) - 8) * scale.s0 * shared_y.s4; \ 170 total_sums.s0 += (((bits4.s2 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y.s5; \ 171 total_sums.s0 += (((bits4.s2 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y.s6; \ 172 total_sums.s0 += (((bits4.s2 & 0xF000) >> 12) - 8) * scale.s0 * shared_y.s7; \ 173 total_sums.s1 += ((bits4.s1 & 0x000F) - 8) * scale.s1 * shared_y.s0; \ 174 total_sums.s1 += (((bits4.s1 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y.s1; \ 175 total_sums.s1 += (((bits4.s1 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y.s2; \ 176 total_sums.s1 += (((bits4.s1 & 0xF000) >> 12) - 8) * scale.s1 * shared_y.s3; \ 177 total_sums.s1 += ((bits4.s3 & 0x000F) - 8) * scale.s1 * shared_y.s4; \ 178 total_sums.s1 += (((bits4.s3 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y.s5; \ 179 total_sums.s1 += (((bits4.s3 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y.s6; \ 180 total_sums.s1 += (((bits4.s3 & 0xF000) >> 12) - 8) * scale.s1 * shared_y.s7; \ 181 shared_y = sub_group_broadcast(y, 3); \ 182 total_sums.s0 += ((bits4.s4 & 0x000F) - 8) * scale.s0 * shared_y.s0; \ 183 total_sums.s0 += (((bits4.s4 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y.s1; \ 184 total_sums.s0 += (((bits4.s4 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y.s2; \ 185 total_sums.s0 += (((bits4.s4 & 0xF000) >> 12) - 8) * scale.s0 * shared_y.s3; \ 186 total_sums.s0 += ((bits4.s6 & 0x000F) - 8) * scale.s0 * shared_y.s4; \ 187 total_sums.s0 += (((bits4.s6 & 0x00F0) >> 4) - 8) * scale.s0 * shared_y.s5; \ 188 total_sums.s0 += (((bits4.s6 & 0x0F00) >> 8) - 8) * scale.s0 * shared_y.s6; \ 189 total_sums.s0 += (((bits4.s6 & 0xF000) >> 12) - 8) * scale.s0 * shared_y.s7; \ 190 total_sums.s1 += ((bits4.s5 & 0x000F) - 8) * scale.s1 * shared_y.s0; \ 191 total_sums.s1 += (((bits4.s5 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y.s1; \ 192 total_sums.s1 += (((bits4.s5 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y.s2; \ 193 total_sums.s1 += (((bits4.s5 & 0xF000) >> 12) - 8) * scale.s1 * shared_y.s3; \ 194 total_sums.s1 += ((bits4.s7 & 0x000F) - 8) * scale.s1 * shared_y.s4; \ 195 total_sums.s1 += (((bits4.s7 & 0x00F0) >> 4) - 8) * scale.s1 * shared_y.s5; \ 196 total_sums.s1 += (((bits4.s7 & 0x0F00) >> 8) - 8) * scale.s1 * shared_y.s6; \ 197 total_sums.s1 += (((bits4.s7 & 0xF000) >> 12) - 8) * scale.s1 * shared_y.s7; \ 198 199 #ifdef ADRENO_GPU 200 REQD_SUBGROUP_SIZE_64 201 #endif 202 __kernel void kernel_gemv_noshuffle_q4_0_f32( 203 __read_only image1d_buffer_t src0_q, // quantized A 204 global half2 * src0_d, // A scales 205 __read_only image1d_buffer_t src1, // B 206 ulong offset1, // offset to B (0) 207 global float * dst, // C 208 ulong offsetd, // offset to C (0) 209 int ne00, // K 210 int ne01, // M 211 int ne02, // 1 212 int ne10, // K 213 int ne12, // 1 214 int ne0, // M 215 int ne1, // N 216 int r2, // 1 217 int r3) 218 { 219 uint groupId = get_local_id(1); 220 uint gid = get_global_id(0); 221 ushort slid = get_sub_group_local_id(); 222 223 uint K = ne00; 224 uint M = ne01; 225 226 uint LINE_STRIDE_A = M / 2; 227 // Keep tail lanes active for subgroup ops, but read a valid weight row. 228 uint gid_s = min(gid, LINE_STRIDE_A - 1); 229 uint BLOCK_STRIDE_A = N_SIMDGROUP * M; 230 231 __private uint4 regA; 232 __private half2 regS; 233 __private float8 regB; 234 235 __private float2 totalSum = (float2)(0.0f); 236 237 // loop along K in block granularity, skip 4 blocks every iter 238 for (uint k = groupId; k < (K / QK4_0); k += N_SIMDGROUP) { 239 regS = src0_d[gid_s + k * LINE_STRIDE_A]; // each fiber loads scale of two rows 240 // first 4 fibers in each wave load 8 B values to its private scope 241 if (slid < 4) { 242 regB.s0123 = read_imagef(src1, (slid * 2 + k * 8)); 243 regB.s4567 = read_imagef(src1, (1 + slid * 2 + k * 8)); 244 } 245 246 // load half weights for two blocks in consecutive rows 247 regA.s0 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 0)).x; 248 regA.s1 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 1)).x; 249 regA.s2 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 2)).x; 250 regA.s3 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 3)).x; 251 #ifdef VECTOR_SUB_GROUP_BROADCAST 252 dequantizeBlockAccum_ns_sgbroadcast_8_hi(totalSum, as_ushort8(regA), regS, regB); 253 #else 254 dequantizeBlockAccum_ns_sgbroadcast_1_hi(totalSum, as_ushort8(regA), regS, regB); 255 #endif // VECTOR_SUB_GROUP_BROADCAST 256 257 regA.s0 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 4)).x; 258 regA.s1 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 5)).x; 259 regA.s2 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 6)).x; 260 regA.s3 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 7)).x; 261 #ifdef VECTOR_SUB_GROUP_BROADCAST 262 dequantizeBlockAccum_ns_sgbroadcast_8_lo(totalSum, as_ushort8(regA), regS, regB); 263 #else 264 dequantizeBlockAccum_ns_sgbroadcast_1_lo(totalSum, as_ushort8(regA), regS, regB); 265 #endif // VECTOR_SUB_GROUP_BROADCAST 266 } 267 268 // reduction in local memory, assumes #wave=4 269 __local float2 reduceLM[SIMDGROUP_WIDTH * 3]; 270 if (groupId == 1) reduceLM[SIMDGROUP_WIDTH * 0 + slid] = totalSum; 271 if (groupId == 2) reduceLM[SIMDGROUP_WIDTH * 1 + slid] = totalSum; 272 if (groupId == 3) reduceLM[SIMDGROUP_WIDTH * 2 + slid] = totalSum; 273 barrier(CLK_LOCAL_MEM_FENCE); 274 if (groupId == 0) totalSum += reduceLM[SIMDGROUP_WIDTH * 0 + slid]; 275 if (groupId == 0) totalSum += reduceLM[SIMDGROUP_WIDTH * 1 + slid]; 276 if (groupId == 0) totalSum += reduceLM[SIMDGROUP_WIDTH * 2 + slid]; 277 278 // 2 outputs per fiber in wave 0 279 if (groupId == 0) { 280 dst = (global float*)((global char*)dst + offsetd); 281 // Guard the two output rows. The x-grid is padded to CEIL_DIV(ne01/2,64)*64, 282 // so when ne01 is not a multiple of 128 the tail row-pairs run past row ne01 283 // and would overrun dst into the adjacent tensor. No-op / byte-identical when 284 // ne01 % 128 == 0 (M/2 already a multiple of 64 -> no padding). 285 if (gid * 2 + 0 < M) dst[gid * 2 + 0] = totalSum.s0; 286 if (gid * 2 + 1 < M) dst[gid * 2 + 1] = totalSum.s1; 287 } 288 289 } 290 291 // Multi-column (N in [2..4]) variant of the q4_0 decode GEMV, for the speculative 292 // / MTP verify batch (n_cols = 2..4 = drafted + bonus positions). Routes the small- 293 // batch verify OFF the transposed-GEMM dead-zone (gemm_noshuffle_q4_0) onto the 294 // efficient GEMV path. Each K-block's weights (regA hi+lo) are loaded ONCE and 295 // reused across the n_cols activation columns. Per-column accumulation is 296 // independent and identical to n_cols standalone GEMVs. n_cols==3 is byte-identical 297 // to the original mc3 (col3 disabled, slots 6/7 stay zero). Kept the _mc3 name. 298 #ifdef VECTOR_SUB_GROUP_BROADCAST 299 #define MC_DQ_HI dequantizeBlockAccum_ns_sgbroadcast_8_hi 300 #define MC_DQ_LO dequantizeBlockAccum_ns_sgbroadcast_8_lo 301 #else 302 #define MC_DQ_HI dequantizeBlockAccum_ns_sgbroadcast_1_hi 303 #define MC_DQ_LO dequantizeBlockAccum_ns_sgbroadcast_1_lo 304 #endif 305 // One column c: load this column's activation (own brace scope so the macros' 306 // `shared_y` decl is re-scoped), then dequant (hi+lo) against the shared weights. 307 #define MC_COL_Q40(ts, c) \ 308 { if (slid < 4) { regB.s0123 = read_imagef(src1, (c)*COL_STRIDE + slid*2 + k*8); \ 309 regB.s4567 = read_imagef(src1, (c)*COL_STRIDE + 1 + slid*2 + k*8); } \ 310 MC_DQ_HI(ts, as_ushort8(regA_hi), regS, regB); \ 311 MC_DQ_LO(ts, as_ushort8(regA_lo), regS, regB); } 312 313 #ifdef ADRENO_GPU 314 REQD_SUBGROUP_SIZE_64 315 #endif 316 __kernel void kernel_gemv_noshuffle_q4_0_f32_mc3( 317 __read_only image1d_buffer_t src0_q, // quantized A 318 global half2 * src0_d, // A scales 319 __read_only image1d_buffer_t src1, // B (n_cols columns, col-major image) 320 global float * dst, // C (column-major [M x n_cols]) 321 ulong offsetd, 322 int ne00, // K 323 int ne01, // M 324 int n_cols) // N (2..4) 325 { 326 uint groupId = get_local_id(1); 327 uint gid = get_global_id(0); 328 ushort slid = get_sub_group_local_id(); 329 330 uint K = ne00; 331 uint M = ne01; 332 333 uint LINE_STRIDE_A = M / 2; 334 // Keep tail lanes active for subgroup ops, but read a valid weight row. 335 uint gid_s = min(gid, LINE_STRIDE_A - 1); 336 // BLOCK_STRIDE_A is the LAYOUT stride between consecutive K-blocks = 4 uints 337 // per q4_0 block * M (set by the trans4_ns convert). The "4" is uints/block, NOT 338 // the subgroup count — keep it fixed so the K-split count (nsg) can vary. 339 uint BLOCK_STRIDE_A = N_SIMDGROUP * M; // = 4 * M (N_SIMDGROUP is the #define 4) 340 uint COL_STRIDE = K / 4; // float4 pixels per activation column 341 uint nsg = get_local_size(1); // runtime K-split (4 default, 8 small-M) 342 MATERIALIZE_WG(nsg); 343 344 __private uint4 regA_hi, regA_lo; 345 __private half2 regS; 346 __private float8 regB; 347 348 __private float2 ts0 = (float2)(0.0f); 349 __private float2 ts1 = (float2)(0.0f); 350 __private float2 ts2 = (float2)(0.0f); 351 __private float2 ts3 = (float2)(0.0f); 352 353 for (uint k = groupId; k < (K / QK4_0); k += nsg) { 354 regS = src0_d[gid_s + k * LINE_STRIDE_A]; 355 356 // weights loaded ONCE, reused across the columns 357 regA_hi.s0 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 0)).x; 358 regA_hi.s1 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 1)).x; 359 regA_hi.s2 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 2)).x; 360 regA_hi.s3 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 3)).x; 361 regA_lo.s0 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 4)).x; 362 regA_lo.s1 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 5)).x; 363 regA_lo.s2 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 6)).x; 364 regA_lo.s3 = read_imageui(src0_q, (gid_s + k * BLOCK_STRIDE_A + LINE_STRIDE_A * 7)).x; 365 366 MC_COL_Q40(ts0, 0); 367 MC_COL_Q40(ts1, 1); 368 if (n_cols > 2) MC_COL_Q40(ts2, 2); 369 if (n_cols > 3) MC_COL_Q40(ts3, 3); 370 } 371 372 // cross-subgroup reduce over nsg subgroups: pack the (up to 4) columns' float2 373 // into a float8. Generalized to runtime nsg (4 default, 8 for small-M). Each 374 // subgroup writes its partial; subgroup 0 sums the rest into its own acc. At 375 // nsg==4 this is byte-identical to the original (sums subgroups 1,2,3 in order). 376 __local float8 reduceLM[SIMDGROUP_WIDTH * 8]; 377 float8 acc = (float8)(ts0.s0, ts0.s1, ts1.s0, ts1.s1, ts2.s0, ts2.s1, ts3.s0, ts3.s1); 378 reduceLM[groupId * SIMDGROUP_WIDTH + slid] = acc; 379 380 barrier(CLK_LOCAL_MEM_FENCE); 381 382 if (groupId == 0) { 383 for (uint g = 1; g < nsg; g++) { 384 acc += reduceLM[g * SIMDGROUP_WIDTH + slid]; 385 } 386 dst = (global float*)((global char*)dst + offsetd); 387 // dst is column-major [M rows x n_cols cols]: (row, col) at col*M + row 388 if (gid < LINE_STRIDE_A) { 389 vstore2((float2)(acc.s0, acc.s1), 0, &(dst[0 * M + gid * 2])); 390 vstore2((float2)(acc.s2, acc.s3), 0, &(dst[1 * M + gid * 2])); 391 if (n_cols > 2) vstore2((float2)(acc.s4, acc.s5), 0, &(dst[2 * M + gid * 2])); 392 if (n_cols > 3) vstore2((float2)(acc.s6, acc.s7), 0, &(dst[3 * M + gid * 2])); 393 } 394 } 395 } 396 #undef MC_COL_Q40 397 #undef MC_DQ_HI 398 #undef MC_DQ_LO