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 / moe_reorder_quant_a_q8_1.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | // Fused MoE activation reorder + q8_1 quantization for the dp4a prefill GEMM. |
| 4 | // Combines kernel_moe_reorder_b (gather src1 rows per the post-router map) with |
| 5 | // the q8_1 quant pre-pass, so the f32 reordered-activation tile buffer is never |
| 6 | // materialised (saves a full write + read of [tok_slots * ne00] floats). |
| 7 | // |
| 8 | // One work-item per (token_slot, 32-block). Padding lanes (router 0xFFFFFFFF) |
| 9 | // emit d=0,s=0,qs=0 so they contribute nothing to the GEMM, exactly as the |
| 10 | // reorder zero-fill did. Output layout matches kernel_moe_quant_a_q8_1: |
| 11 | // qa[token_slot*K + blk*32 + i], da/sa[token_slot*(K/32) + blk]. |
| 12 | __kernel void kernel_moe_reorder_quant_a_q8_1( |
| 13 | __global const float * src, // original activations (offset applied) |
| 14 | __global const uint * router, // post-router indices [tok_slots] |
| 15 | __global char * qa, |
| 16 | __global half * da, |
| 17 | __global half * sa, |
| 18 | __global const int * total_tiles, |
| 19 | uint K, |
| 20 | ushort map_ratio, |
| 21 | uint tile_size, |
| 22 | uint n_kblocks // K / 32 |
| 23 | ) { |
| 24 | const uint blk = get_global_id(0); // 32-block along K |
| 25 | const uint tok = get_global_id(1); // token slot (post_router_idx) |
| 26 | |
| 27 | if (blk >= n_kblocks || tok >= (uint)total_tiles[0] * tile_size) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | const uint out_base = tok * K + blk * 32; |
| 32 | const uint bidx = tok * n_kblocks + blk; |
| 33 | |
| 34 | const uint router_idx = router[tok]; |
| 35 | |
| 36 | float v[32]; |
| 37 | float amax = 0.0f; |
| 38 | if (router_idx == 0xFFFFFFFF) { |
| 39 | #pragma unroll |
| 40 | for (int i = 0; i < 32; ++i) v[i] = 0.0f; |
| 41 | } else { |
| 42 | const uint act_idx = router_idx / map_ratio; |
| 43 | const uint in_base = act_idx * K + blk * 32; |
| 44 | #pragma unroll |
| 45 | for (int i = 0; i < 32; ++i) { |
| 46 | v[i] = src[in_base + i]; |
| 47 | amax = fmax(amax, fabs(v[i])); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | const float d = amax / 127.0f; |
| 52 | const float id = (amax > 0.0f) ? (127.0f / amax) : 0.0f; |
| 53 | |
| 54 | int sum = 0; |
| 55 | #pragma unroll |
| 56 | for (int i = 0; i < 32; ++i) { |
| 57 | const int q = (int)rint(v[i] * id); |
| 58 | qa[out_base + i] = (char)q; |
| 59 | sum += q; |
| 60 | } |
| 61 | |
| 62 | da[bidx] = (half)d; |
| 63 | sa[bidx] = (half)(d * (float)sum); |
| 64 | } |