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_moe_mxfp4_f32_ns.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 3 | #pragma OPENCL EXTENSION cl_qcom_subgroup_uniform_load: enable |
| 4 | #pragma OPENCL EXTENSION cl_qcom_subgroup_constant_load: enable |
| 5 | #pragma OPENCL EXTENSION cl_qcom_extra_vector_types : enable |
| 6 | |
| 7 | #define TILESIZE_K 16 |
| 8 | #define TILESIZE_M 64 |
| 9 | #define TILESIZE_N 32 |
| 10 | |
| 11 | |
| 12 | static inline half8 mxfp4_to_fp16_packed8(ushort2 fp4x8) { |
| 13 | ushort2 fp16_packed_a_0, fp16_packed_b_0, bias_a, bias_b, sign_a, sign_b; |
| 14 | fp16_packed_a_0.lo = (fp4x8.s0 << 9) & 0x0E00; |
| 15 | fp16_packed_a_0.hi = (fp4x8.s0 << 5) & 0x0E00; |
| 16 | fp16_packed_b_0.lo = (fp4x8.s0 << 1) & 0x0E00; |
| 17 | fp16_packed_b_0.hi = (fp4x8.s0 >> 3) & 0x0E00; |
| 18 | |
| 19 | bias_a.lo = (fp16_packed_a_0.lo != 0) ? 0x3800 : 0x0; |
| 20 | bias_a.hi = (fp16_packed_a_0.hi != 0) ? 0x3800 : 0x0; |
| 21 | bias_b.lo = (fp16_packed_b_0.lo != 0) ? 0x3800 : 0x0; |
| 22 | bias_b.hi = (fp16_packed_b_0.hi != 0) ? 0x3800 : 0x0; |
| 23 | |
| 24 | fp16_packed_a_0.lo = (fp16_packed_a_0.lo != 0x0200) ? fp16_packed_a_0.lo : 0x0; |
| 25 | fp16_packed_a_0.hi = (fp16_packed_a_0.hi != 0x0200) ? fp16_packed_a_0.hi : 0x0; |
| 26 | fp16_packed_b_0.lo = (fp16_packed_b_0.lo != 0x0200) ? fp16_packed_b_0.lo : 0x0; |
| 27 | fp16_packed_b_0.hi = (fp16_packed_b_0.hi != 0x0200) ? fp16_packed_b_0.hi : 0x0; |
| 28 | |
| 29 | sign_a.lo = (fp4x8.s0 << 12) & 0x8000; |
| 30 | sign_a.hi = (fp4x8.s0 << 8) & 0x8000; |
| 31 | sign_b.lo = (fp4x8.s0 << 4) & 0x8000; |
| 32 | sign_b.hi = fp4x8.s0 & 0x8000; |
| 33 | |
| 34 | fp16_packed_a_0 = sign_a + bias_a + fp16_packed_a_0; |
| 35 | fp16_packed_b_0 = sign_b + bias_b + fp16_packed_b_0; |
| 36 | |
| 37 | ushort2 fp16_packed_a_1, fp16_packed_b_1; |
| 38 | fp16_packed_a_1.lo = (fp4x8.s1 << 9) & 0x0E00; |
| 39 | fp16_packed_a_1.hi = (fp4x8.s1 << 5) & 0x0E00; |
| 40 | fp16_packed_b_1.lo = (fp4x8.s1 << 1) & 0x0E00; |
| 41 | fp16_packed_b_1.hi = (fp4x8.s1 >> 3) & 0x0E00; |
| 42 | |
| 43 | bias_a.lo = (fp16_packed_a_1.lo != 0) ? 0x3800 : 0x0; |
| 44 | bias_a.hi = (fp16_packed_a_1.hi != 0) ? 0x3800 : 0x0; |
| 45 | bias_b.lo = (fp16_packed_b_1.lo != 0) ? 0x3800 : 0x0; |
| 46 | bias_b.hi = (fp16_packed_b_1.hi != 0) ? 0x3800 : 0x0; |
| 47 | |
| 48 | fp16_packed_a_1.lo = (fp16_packed_a_1.lo != 0x0200) ? fp16_packed_a_1.lo : 0x0; |
| 49 | fp16_packed_a_1.hi = (fp16_packed_a_1.hi != 0x0200) ? fp16_packed_a_1.hi : 0x0; |
| 50 | fp16_packed_b_1.lo = (fp16_packed_b_1.lo != 0x0200) ? fp16_packed_b_1.lo : 0x0; |
| 51 | fp16_packed_b_1.hi = (fp16_packed_b_1.hi != 0x0200) ? fp16_packed_b_1.hi : 0x0; |
| 52 | |
| 53 | sign_a.lo = (fp4x8.s1 << 12) & 0x8000; |
| 54 | sign_a.hi = (fp4x8.s1 << 8) & 0x8000; |
| 55 | sign_b.lo = (fp4x8.s1 << 4) & 0x8000; |
| 56 | sign_b.hi = fp4x8.s1 & 0x8000; |
| 57 | |
| 58 | fp16_packed_a_1 = sign_a + bias_a + fp16_packed_a_1; |
| 59 | fp16_packed_b_1 = sign_b + bias_b + fp16_packed_b_1; |
| 60 | |
| 61 | return as_half8((ushort8)(fp16_packed_a_0, fp16_packed_b_0, fp16_packed_a_1, fp16_packed_b_1)); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | #define dotx16_reduce8(a_reg, b_lm, c_reg, lm_offset) \ |
| 66 | acc.s0 = dot(a_reg.s0123, b_lm[lm_offset + 0]); \ |
| 67 | acc.s1 = dot(a_reg.s0123, b_lm[lm_offset + 1]); \ |
| 68 | acc.s2 = dot(a_reg.s0123, b_lm[lm_offset + 2]); \ |
| 69 | acc.s3 = dot(a_reg.s0123, b_lm[lm_offset + 3]); \ |
| 70 | acc.s4 = dot(a_reg.s0123, b_lm[lm_offset + 4]); \ |
| 71 | acc.s5 = dot(a_reg.s0123, b_lm[lm_offset + 5]); \ |
| 72 | acc.s6 = dot(a_reg.s0123, b_lm[lm_offset + 6]); \ |
| 73 | acc.s7 = dot(a_reg.s0123, b_lm[lm_offset + 7]); \ |
| 74 | acc.s8 = dot(a_reg.s0123, b_lm[lm_offset + 8]); \ |
| 75 | acc.s9 = dot(a_reg.s0123, b_lm[lm_offset + 9]); \ |
| 76 | acc.sa = dot(a_reg.s0123, b_lm[lm_offset + 10]); \ |
| 77 | acc.sb = dot(a_reg.s0123, b_lm[lm_offset + 11]); \ |
| 78 | acc.sc = dot(a_reg.s0123, b_lm[lm_offset + 12]); \ |
| 79 | acc.sd = dot(a_reg.s0123, b_lm[lm_offset + 13]); \ |
| 80 | acc.se = dot(a_reg.s0123, b_lm[lm_offset + 14]); \ |
| 81 | acc.sf = dot(a_reg.s0123, b_lm[lm_offset + 15]); \ |
| 82 | acc.s0 += dot(a_reg.s4567, b_lm[lm_offset + 32]); \ |
| 83 | acc.s1 += dot(a_reg.s4567, b_lm[lm_offset + 33]); \ |
| 84 | acc.s2 += dot(a_reg.s4567, b_lm[lm_offset + 34]); \ |
| 85 | acc.s3 += dot(a_reg.s4567, b_lm[lm_offset + 35]); \ |
| 86 | acc.s4 += dot(a_reg.s4567, b_lm[lm_offset + 36]); \ |
| 87 | acc.s5 += dot(a_reg.s4567, b_lm[lm_offset + 37]); \ |
| 88 | acc.s6 += dot(a_reg.s4567, b_lm[lm_offset + 38]); \ |
| 89 | acc.s7 += dot(a_reg.s4567, b_lm[lm_offset + 39]); \ |
| 90 | acc.s8 += dot(a_reg.s4567, b_lm[lm_offset + 40]); \ |
| 91 | acc.s9 += dot(a_reg.s4567, b_lm[lm_offset + 41]); \ |
| 92 | acc.sa += dot(a_reg.s4567, b_lm[lm_offset + 42]); \ |
| 93 | acc.sb += dot(a_reg.s4567, b_lm[lm_offset + 43]); \ |
| 94 | acc.sc += dot(a_reg.s4567, b_lm[lm_offset + 44]); \ |
| 95 | acc.sd += dot(a_reg.s4567, b_lm[lm_offset + 45]); \ |
| 96 | acc.se += dot(a_reg.s4567, b_lm[lm_offset + 46]); \ |
| 97 | acc.sf += dot(a_reg.s4567, b_lm[lm_offset + 47]); \ |
| 98 | c_reg.lo += convert_float8(acc.lo); \ |
| 99 | c_reg.hi += convert_float8(acc.hi); \ |
| 100 | acc.s0 = dot(a_reg.s89ab, b_lm[lm_offset + 64]); \ |
| 101 | acc.s1 = dot(a_reg.s89ab, b_lm[lm_offset + 65]); \ |
| 102 | acc.s2 = dot(a_reg.s89ab, b_lm[lm_offset + 66]); \ |
| 103 | acc.s3 = dot(a_reg.s89ab, b_lm[lm_offset + 67]); \ |
| 104 | acc.s4 = dot(a_reg.s89ab, b_lm[lm_offset + 68]); \ |
| 105 | acc.s5 = dot(a_reg.s89ab, b_lm[lm_offset + 69]); \ |
| 106 | acc.s6 = dot(a_reg.s89ab, b_lm[lm_offset + 70]); \ |
| 107 | acc.s7 = dot(a_reg.s89ab, b_lm[lm_offset + 71]); \ |
| 108 | acc.s8 = dot(a_reg.s89ab, b_lm[lm_offset + 72]); \ |
| 109 | acc.s9 = dot(a_reg.s89ab, b_lm[lm_offset + 73]); \ |
| 110 | acc.sa = dot(a_reg.s89ab, b_lm[lm_offset + 74]); \ |
| 111 | acc.sb = dot(a_reg.s89ab, b_lm[lm_offset + 75]); \ |
| 112 | acc.sc = dot(a_reg.s89ab, b_lm[lm_offset + 76]); \ |
| 113 | acc.sd = dot(a_reg.s89ab, b_lm[lm_offset + 77]); \ |
| 114 | acc.se = dot(a_reg.s89ab, b_lm[lm_offset + 78]); \ |
| 115 | acc.sf = dot(a_reg.s89ab, b_lm[lm_offset + 79]); \ |
| 116 | acc.s0 += dot(a_reg.scdef, b_lm[lm_offset + 96]); \ |
| 117 | acc.s1 += dot(a_reg.scdef, b_lm[lm_offset + 97]); \ |
| 118 | acc.s2 += dot(a_reg.scdef, b_lm[lm_offset + 98]); \ |
| 119 | acc.s3 += dot(a_reg.scdef, b_lm[lm_offset + 99]); \ |
| 120 | acc.s4 += dot(a_reg.scdef, b_lm[lm_offset + 100]); \ |
| 121 | acc.s5 += dot(a_reg.scdef, b_lm[lm_offset + 101]); \ |
| 122 | acc.s6 += dot(a_reg.scdef, b_lm[lm_offset + 102]); \ |
| 123 | acc.s7 += dot(a_reg.scdef, b_lm[lm_offset + 103]); \ |
| 124 | acc.s8 += dot(a_reg.scdef, b_lm[lm_offset + 104]); \ |
| 125 | acc.s9 += dot(a_reg.scdef, b_lm[lm_offset + 105]); \ |
| 126 | acc.sa += dot(a_reg.scdef, b_lm[lm_offset + 106]); \ |
| 127 | acc.sb += dot(a_reg.scdef, b_lm[lm_offset + 107]); \ |
| 128 | acc.sc += dot(a_reg.scdef, b_lm[lm_offset + 108]); \ |
| 129 | acc.sd += dot(a_reg.scdef, b_lm[lm_offset + 109]); \ |
| 130 | acc.se += dot(a_reg.scdef, b_lm[lm_offset + 110]); \ |
| 131 | acc.sf += dot(a_reg.scdef, b_lm[lm_offset + 111]); \ |
| 132 | c_reg.lo += convert_float8(acc.lo); \ |
| 133 | c_reg.hi += convert_float8(acc.hi); \ |
| 134 | |
| 135 | // Quarter-tile variant: computes 8 output columns (one skip-group) into a float8 |
| 136 | // accumulator. Same reduction order / flush cadence as dotx16_reduce8, so the |
| 137 | // non-skipped path is byte-identical; it just lets the caller skip empty |
| 138 | // 8-column groups at finer granularity. Uses a private half8 `acc8`. |
| 139 | #define dotx8_reduce4(a_reg, b_lm, c_reg, lm_offset) \ |
| 140 | acc8.s0 = dot(a_reg.s0123, b_lm[lm_offset + 0]); \ |
| 141 | acc8.s1 = dot(a_reg.s0123, b_lm[lm_offset + 1]); \ |
| 142 | acc8.s2 = dot(a_reg.s0123, b_lm[lm_offset + 2]); \ |
| 143 | acc8.s3 = dot(a_reg.s0123, b_lm[lm_offset + 3]); \ |
| 144 | acc8.s4 = dot(a_reg.s0123, b_lm[lm_offset + 4]); \ |
| 145 | acc8.s5 = dot(a_reg.s0123, b_lm[lm_offset + 5]); \ |
| 146 | acc8.s6 = dot(a_reg.s0123, b_lm[lm_offset + 6]); \ |
| 147 | acc8.s7 = dot(a_reg.s0123, b_lm[lm_offset + 7]); \ |
| 148 | acc8.s0 += dot(a_reg.s4567, b_lm[lm_offset + 32]); \ |
| 149 | acc8.s1 += dot(a_reg.s4567, b_lm[lm_offset + 33]); \ |
| 150 | acc8.s2 += dot(a_reg.s4567, b_lm[lm_offset + 34]); \ |
| 151 | acc8.s3 += dot(a_reg.s4567, b_lm[lm_offset + 35]); \ |
| 152 | acc8.s4 += dot(a_reg.s4567, b_lm[lm_offset + 36]); \ |
| 153 | acc8.s5 += dot(a_reg.s4567, b_lm[lm_offset + 37]); \ |
| 154 | acc8.s6 += dot(a_reg.s4567, b_lm[lm_offset + 38]); \ |
| 155 | acc8.s7 += dot(a_reg.s4567, b_lm[lm_offset + 39]); \ |
| 156 | c_reg += convert_float8(acc8); \ |
| 157 | acc8.s0 = dot(a_reg.s89ab, b_lm[lm_offset + 64]); \ |
| 158 | acc8.s1 = dot(a_reg.s89ab, b_lm[lm_offset + 65]); \ |
| 159 | acc8.s2 = dot(a_reg.s89ab, b_lm[lm_offset + 66]); \ |
| 160 | acc8.s3 = dot(a_reg.s89ab, b_lm[lm_offset + 67]); \ |
| 161 | acc8.s4 = dot(a_reg.s89ab, b_lm[lm_offset + 68]); \ |
| 162 | acc8.s5 = dot(a_reg.s89ab, b_lm[lm_offset + 69]); \ |
| 163 | acc8.s6 = dot(a_reg.s89ab, b_lm[lm_offset + 70]); \ |
| 164 | acc8.s7 = dot(a_reg.s89ab, b_lm[lm_offset + 71]); \ |
| 165 | acc8.s0 += dot(a_reg.scdef, b_lm[lm_offset + 96]); \ |
| 166 | acc8.s1 += dot(a_reg.scdef, b_lm[lm_offset + 97]); \ |
| 167 | acc8.s2 += dot(a_reg.scdef, b_lm[lm_offset + 98]); \ |
| 168 | acc8.s3 += dot(a_reg.scdef, b_lm[lm_offset + 99]); \ |
| 169 | acc8.s4 += dot(a_reg.scdef, b_lm[lm_offset + 100]); \ |
| 170 | acc8.s5 += dot(a_reg.scdef, b_lm[lm_offset + 101]); \ |
| 171 | acc8.s6 += dot(a_reg.scdef, b_lm[lm_offset + 102]); \ |
| 172 | acc8.s7 += dot(a_reg.scdef, b_lm[lm_offset + 103]); \ |
| 173 | c_reg += convert_float8(acc8); \ |
| 174 | |
| 175 | |
| 176 | static inline half e8m0_to_fp16(uchar x) { |
| 177 | ushort bits; |
| 178 | bits = (ushort)(x) - (ushort)(112); |
| 179 | bits = ((bits & 0x00E0) != 0) ? 0x7C00 : (bits << 10); |
| 180 | return as_half(bits); |
| 181 | } |
| 182 | |
| 183 | static inline float e8m0_to_fp32(uchar x) { |
| 184 | int bits; |
| 185 | bits = (x == 0) ? 0x00400000 : ((uint) x << 23); |
| 186 | return as_float(bits); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | __attribute__((qcom_wave_pair_mode(1))) // 1=force single 2=force pair |
| 191 | kernel void kernel_gemm_moe_mxfp4_f32_ns( |
| 192 | __read_only image1d_buffer_t src0_q, |
| 193 | __global uchar * src0_d, |
| 194 | __read_only image1d_buffer_t src1, |
| 195 | __global uint * src2, |
| 196 | __global ushort * src2_emap, |
| 197 | __write_only image1d_buffer_t dst, |
| 198 | __global int * total_tiles, |
| 199 | uint ne00, |
| 200 | uint ne01, |
| 201 | uint is_ragged, |
| 202 | uint skip_gran |
| 203 | ) { |
| 204 | uint block_id_m = get_global_id(1); // m_tile |
| 205 | uint block_id_n = get_global_id(2); // n_tile |
| 206 | |
| 207 | // Boundary check |
| 208 | if (block_id_n >= total_tiles[0]) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | // Ragged tile-skip: when is_ragged and the upper 16 token-slots of this tile are all |
| 213 | // padding (router 0xFFFFFFFF), skip the second (reg_c.hi) dotx16_reduce8 half -> ~half |
| 214 | // the GEMM dot for sparse tiles. Numerically identical (the skipped lanes are padding). |
| 215 | // Ragged tile-skip: tokens are packed contiguously per expert (moe_scatter fills |
| 216 | // lanes 0..V-1, moe_fill pre-pads the rest), so router padding (0xFFFFFFFF) is always |
| 217 | // trailing. Find the valid-token count V and round it UP to the skip granularity |
| 218 | // skip_gran (columns per skip-group: 8 = quarter, 16 = half/legacy, 32 = disabled). |
| 219 | // A 8-column group g is all-padding iff its first column (8*g) >= n_active, so its |
| 220 | // dotx8_reduce4 is skipped. Numerically identical (skipped lanes are padding). |
| 221 | uint n_active = TILESIZE_N; |
| 222 | if (is_ragged && skip_gran < TILESIZE_N) { |
| 223 | uint n_valid = TILESIZE_N; |
| 224 | for (uint _t = 0; _t < TILESIZE_N; ++_t) { |
| 225 | if (src2[block_id_n * TILESIZE_N + _t] == 0xFFFFFFFFu) { n_valid = _t; break; } |
| 226 | } |
| 227 | n_active = min((uint)TILESIZE_N, ((n_valid + skip_gran - 1) / skip_gran) * skip_gran); |
| 228 | } |
| 229 | // Group 0 (cols 0-7) always runs; groups 1-3 skip when fully padding. |
| 230 | bool skip_g1 = (8u >= n_active); |
| 231 | bool skip_g2 = (16u >= n_active); |
| 232 | bool skip_g3 = (24u >= n_active); |
| 233 | |
| 234 | __private half16 reg_a; |
| 235 | __private float32 reg_c = (float32)(0); |
| 236 | __local half4 shared_b[128]; |
| 237 | |
| 238 | const ushort expert_id = src2_emap[block_id_n]; |
| 239 | |
| 240 | const uint row = block_id_m * TILESIZE_M; |
| 241 | const uint col = block_id_n * TILESIZE_N; |
| 242 | |
| 243 | uint sub_block_id_m = get_local_id(0); |
| 244 | uint2 b_global_offset; |
| 245 | b_global_offset.x = ((sub_block_id_m & 3) << 2) + (sub_block_id_m >> 2) * ne00; |
| 246 | b_global_offset.y = b_global_offset.x + (16 * ne00); |
| 247 | uint2 b_local_offset; |
| 248 | b_local_offset.x = (sub_block_id_m & 3) * 32 + (sub_block_id_m >> 2); |
| 249 | b_local_offset.y = b_local_offset.x + 16; |
| 250 | |
| 251 | // Loop along K axis, 32 elements (one block) for each iteration, divided into 2 sub-blocks |
| 252 | for (uint step = 0; step < ne00; step += TILESIZE_K * 2) { |
| 253 | // First sub-block |
| 254 | uint q_sub_offset = row + ((ne01 * step) >> 3) + ((expert_id * ne00 * ne01) >> 3); |
| 255 | uint s_sub_offset = row + ((ne01 * step) >> 5) + ((expert_id * ne00 * ne01) >> 5); |
| 256 | uint b_sub_offset = col * ne00 + step; |
| 257 | |
| 258 | // Load scale for current mxfp4 block |
| 259 | uint s_offset = s_sub_offset + get_global_id(0); |
| 260 | float s = e8m0_to_fp32(src0_d[s_offset]); |
| 261 | |
| 262 | // Load 16 fp4 (64-bits) in transposed layout |
| 263 | uint2 mxfp4x16; |
| 264 | mxfp4x16.x = read_imageui(src0_q, q_sub_offset + sub_block_id_m).x; |
| 265 | mxfp4x16.y = read_imageui(src0_q, q_sub_offset + sub_block_id_m + ne01).x; |
| 266 | |
| 267 | // Load 16x32 floats from matrix B, each fiber out of 64 in a sub-group loads 8 elements |
| 268 | float8 bx8_f32; |
| 269 | bx8_f32.lo = read_imagef(src1, (b_sub_offset + b_global_offset.x) / 4); |
| 270 | bx8_f32.hi = read_imagef(src1, (b_sub_offset + b_global_offset.y) / 4); |
| 271 | // Convert to half and store to LM to share within the subgroup |
| 272 | half8 bx8_f16 = convert_half8(bx8_f32); |
| 273 | shared_b[b_local_offset.x] = bx8_f16.lo; |
| 274 | shared_b[b_local_offset.y] = bx8_f16.hi; |
| 275 | |
| 276 | // Dequantization |
| 277 | // Cast the e8m0 scale to half to satisfy E17 compilers |
| 278 | reg_a.lo = mxfp4_to_fp16_packed8(as_ushort2(mxfp4x16.lo)) * (half)s; |
| 279 | reg_a.hi = mxfp4_to_fp16_packed8(as_ushort2(mxfp4x16.hi)) * (half)s; |
| 280 | |
| 281 | sub_group_barrier(CLK_LOCAL_MEM_FENCE); |
| 282 | |
| 283 | // 32 16x16 fp16 dot product with 8 elements reduction for better precision |
| 284 | half8 acc8; |
| 285 | dotx8_reduce4(reg_a, shared_b, reg_c.lo.lo, 0); |
| 286 | if (!skip_g1) { dotx8_reduce4(reg_a, shared_b, reg_c.lo.hi, 8); } |
| 287 | if (!skip_g2) { dotx8_reduce4(reg_a, shared_b, reg_c.hi.lo, 16); } |
| 288 | if (!skip_g3) { dotx8_reduce4(reg_a, shared_b, reg_c.hi.hi, 24); } |
| 289 | |
| 290 | // Repeat for second sub-block |
| 291 | uint half_step = step + TILESIZE_K; |
| 292 | q_sub_offset = row + ((ne01 * half_step) >> 3) + ((expert_id * ne00 * ne01) >> 3); |
| 293 | b_sub_offset = col * ne00 + half_step; |
| 294 | |
| 295 | // Load next 16 fp4 (64-bits) in transposed layout |
| 296 | mxfp4x16.x = read_imageui(src0_q, q_sub_offset + sub_block_id_m).x; |
| 297 | mxfp4x16.y = read_imageui(src0_q, q_sub_offset + sub_block_id_m + ne01).x; |
| 298 | |
| 299 | // Load 16x32 floats from matrix B, each fiber out of 64 in a sub-group loads 8 elements |
| 300 | bx8_f32.lo = read_imagef(src1, (b_sub_offset + b_global_offset.x) / 4); |
| 301 | bx8_f32.hi = read_imagef(src1, (b_sub_offset + b_global_offset.y) / 4); |
| 302 | // Convert to half and store to LM to share within the subgroup |
| 303 | bx8_f16 = convert_half8(bx8_f32); |
| 304 | shared_b[b_local_offset.x] = bx8_f16.lo; |
| 305 | shared_b[b_local_offset.y] = bx8_f16.hi; |
| 306 | |
| 307 | // Dequantization |
| 308 | // Cast the e8m0 scale to half to satisfy E17 compilers |
| 309 | reg_a.lo = mxfp4_to_fp16_packed8(as_ushort2(mxfp4x16.lo)) * (half)s; |
| 310 | reg_a.hi = mxfp4_to_fp16_packed8(as_ushort2(mxfp4x16.hi)) * (half)s; |
| 311 | |
| 312 | sub_group_barrier(CLK_LOCAL_MEM_FENCE); |
| 313 | |
| 314 | // 32 16x16 fp16 dot product with 3-levels reduction for better precision |
| 315 | dotx8_reduce4(reg_a, shared_b, reg_c.lo.lo, 0); |
| 316 | if (!skip_g1) { dotx8_reduce4(reg_a, shared_b, reg_c.lo.hi, 8); } |
| 317 | if (!skip_g2) { dotx8_reduce4(reg_a, shared_b, reg_c.hi.lo, 16); } |
| 318 | if (!skip_g3) { dotx8_reduce4(reg_a, shared_b, reg_c.hi.hi, 24); } |
| 319 | } |
| 320 | |
| 321 | if ((get_global_id(0) + block_id_m * TILESIZE_M) >= ne01) { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | // Load poster router and share in LM |
| 326 | __local uint out_idx[TILESIZE_N]; |
| 327 | |
| 328 | if (get_local_id(0) < TILESIZE_N) { |
| 329 | uint idx = src2[block_id_n * TILESIZE_N + get_local_id(0)]; |
| 330 | if (idx == 0xFFFFFFFF) { |
| 331 | idx = src2[block_id_n * TILESIZE_N + 0]; |
| 332 | } |
| 333 | out_idx[get_local_id(0)] = idx * ne01; |
| 334 | } |
| 335 | |
| 336 | barrier(CLK_LOCAL_MEM_FENCE); |
| 337 | |
| 338 | // Scatter results back to original position in output grid |
| 339 | uint m_offset = row + get_local_id(0); |
| 340 | |
| 341 | write_imagef(dst, out_idx[1] + m_offset, (reg_c.s1)); |
| 342 | write_imagef(dst, out_idx[2] + m_offset, (reg_c.s2)); |
| 343 | write_imagef(dst, out_idx[3] + m_offset, (reg_c.s3)); |
| 344 | write_imagef(dst, out_idx[4] + m_offset, (reg_c.s4)); |
| 345 | write_imagef(dst, out_idx[5] + m_offset, (reg_c.s5)); |
| 346 | write_imagef(dst, out_idx[6] + m_offset, (reg_c.s6)); |
| 347 | write_imagef(dst, out_idx[7] + m_offset, (reg_c.s7)); |
| 348 | write_imagef(dst, out_idx[8] + m_offset, (reg_c.s8)); |
| 349 | write_imagef(dst, out_idx[9] + m_offset, (reg_c.s9)); |
| 350 | write_imagef(dst, out_idx[10] + m_offset, (reg_c.sa)); |
| 351 | write_imagef(dst, out_idx[11] + m_offset, (reg_c.sb)); |
| 352 | write_imagef(dst, out_idx[12] + m_offset, (reg_c.sc)); |
| 353 | write_imagef(dst, out_idx[13] + m_offset, (reg_c.sd)); |
| 354 | write_imagef(dst, out_idx[14] + m_offset, (reg_c.se)); |
| 355 | write_imagef(dst, out_idx[15] + m_offset, (reg_c.sf)); |
| 356 | write_imagef(dst, out_idx[16] + m_offset, (reg_c.sg)); |
| 357 | write_imagef(dst, out_idx[17] + m_offset, (reg_c.sh)); |
| 358 | write_imagef(dst, out_idx[18] + m_offset, (reg_c.si)); |
| 359 | write_imagef(dst, out_idx[19] + m_offset, (reg_c.sj)); |
| 360 | write_imagef(dst, out_idx[20] + m_offset, (reg_c.sk)); |
| 361 | write_imagef(dst, out_idx[21] + m_offset, (reg_c.sl)); |
| 362 | write_imagef(dst, out_idx[22] + m_offset, (reg_c.sm)); |
| 363 | write_imagef(dst, out_idx[23] + m_offset, (reg_c.sn)); |
| 364 | write_imagef(dst, out_idx[24] + m_offset, (reg_c.so)); |
| 365 | write_imagef(dst, out_idx[25] + m_offset, (reg_c.sp)); |
| 366 | write_imagef(dst, out_idx[26] + m_offset, (reg_c.sq)); |
| 367 | write_imagef(dst, out_idx[27] + m_offset, (reg_c.sr)); |
| 368 | write_imagef(dst, out_idx[28] + m_offset, (reg_c.ss)); |
| 369 | write_imagef(dst, out_idx[29] + m_offset, (reg_c.st)); |
| 370 | write_imagef(dst, out_idx[30] + m_offset, (reg_c.su)); |
| 371 | write_imagef(dst, out_idx[31] + m_offset, (reg_c.sv)); |
| 372 | |
| 373 | // Store zero padding parts to the index of first output in tile, override correct result in the end |
| 374 | barrier(CLK_GLOBAL_MEM_FENCE); |
| 375 | write_imagef(dst, out_idx[0] + m_offset, (reg_c.s0)); |
| 376 | } |