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_sort_by_expert.cl
| 1 | #pragma OPENCL EXTENSION cl_khr_fp16 : enable |
| 2 | |
| 3 | __kernel void kernel_moe_histogram( |
| 4 | __global const int * input, |
| 5 | __global int * hist, |
| 6 | uint N, |
| 7 | uint topK, |
| 8 | uint n_experts, |
| 9 | ulong offset |
| 10 | ) { |
| 11 | input = (__global const int *)((__global const char *)input + offset); |
| 12 | |
| 13 | uint n = get_global_id(0); |
| 14 | uint k = get_global_id(1); |
| 15 | |
| 16 | if (n >= N || k >= topK) { |
| 17 | return; |
| 18 | } |
| 19 | |
| 20 | int expert_id = input[n * n_experts + k]; |
| 21 | if (expert_id == -1) { // skipped slot, owned by no expert |
| 22 | return; |
| 23 | } |
| 24 | atomic_inc(&hist[expert_id]); |
| 25 | } |
| 26 | |
| 27 | __kernel void kernel_moe_scan( |
| 28 | __global int * hist, |
| 29 | __global int * tile_offset, |
| 30 | __global int * total_tiles, |
| 31 | __global int * slot_counter, |
| 32 | int tile_size, |
| 33 | uint n_experts |
| 34 | ) { |
| 35 | int offset = 0; |
| 36 | for (int v = 0; v < n_experts; v++) { |
| 37 | int count = hist[v]; |
| 38 | int tiles = (count + tile_size - 1) / tile_size; |
| 39 | tile_offset[v] = offset; |
| 40 | offset += tiles; |
| 41 | hist[v] = 0; |
| 42 | slot_counter[v] = 0; |
| 43 | } |
| 44 | |
| 45 | *total_tiles = offset; |
| 46 | } |
| 47 | |
| 48 | __kernel void kernel_moe_scatter( |
| 49 | __global const int * input, |
| 50 | __global int * post_router, |
| 51 | __global ushort * emap, |
| 52 | __global const int * tile_offset, |
| 53 | __global int * slot_counter, |
| 54 | int N, |
| 55 | int topK, |
| 56 | uint n_experts, |
| 57 | ulong offset |
| 58 | ) { |
| 59 | input = (__global const int *)((__global const char *)input + offset); |
| 60 | |
| 61 | uint n = get_global_id(0); |
| 62 | uint k = get_global_id(1); |
| 63 | |
| 64 | if (n >= N || k >= topK) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | int val = input[n * n_experts + k]; |
| 69 | if (val == -1) { // skipped slot, owned by no expert |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | int local_slot = atomic_inc(&slot_counter[val]); |
| 74 | |
| 75 | int tile_idx = tile_offset[val] + (local_slot / 32); |
| 76 | int lane = local_slot % 32; |
| 77 | int out_pos = tile_idx * 32 + lane; |
| 78 | |
| 79 | post_router[out_pos] = n * topK + k; |
| 80 | emap[tile_idx] = val; |
| 81 | } |
| 82 | |
| 83 | // Deterministic replacement for kernel_moe_scatter. |
| 84 | // |
| 85 | // kernel_moe_scatter takes each token's slot from atomic_inc(slot_counter[expert]), |
| 86 | // so the token -> slot packing inside an expert depends on which work-item wins the |
| 87 | // atomic and changes from run to run. The ragged prefill GEMM path is sensitive to |
| 88 | // that packing (the non-ragged path is not, since its padded slots alias slot 0 and |
| 89 | // are overwritten last), which makes MoE prompt processing non-reproducible: the same |
| 90 | // binary on the same prompt returns one of several outputs. |
| 91 | // |
| 92 | // Here the slot is the token's rank in flat (n, k) order among the tokens routed to |
| 93 | // the same expert - a fixed function of the routing input. One workgroup per expert |
| 94 | // walks the flat routing list in blocks of 64 and ranks its own tokens with a |
| 95 | // workgroup scan, carrying a running count between blocks. Cost is one pass over the |
| 96 | // routing list per expert; the list is a few KiB and stays in cache. |
| 97 | __kernel void kernel_moe_scatter_stable( |
| 98 | __global const int * input, |
| 99 | __global int * post_router, |
| 100 | __global ushort * emap, |
| 101 | __global const int * tile_offset, |
| 102 | int N, |
| 103 | int topK, |
| 104 | uint n_experts, |
| 105 | ulong offset |
| 106 | ) { |
| 107 | input = (__global const int *)((__global const char *)input + offset); |
| 108 | |
| 109 | const int e = get_group_id(1); |
| 110 | const int lid = get_local_id(0); |
| 111 | const int M = N * topK; |
| 112 | |
| 113 | __local int scan[64]; |
| 114 | __local int running; |
| 115 | |
| 116 | if (lid == 0) { |
| 117 | running = 0; |
| 118 | } |
| 119 | barrier(CLK_LOCAL_MEM_FENCE); |
| 120 | |
| 121 | for (int base = 0; base < M; base += 64) { |
| 122 | const int j = base + lid; |
| 123 | |
| 124 | int pred = 0; |
| 125 | if (j < M) { |
| 126 | const int n = j / topK; |
| 127 | const int k = j - n * topK; |
| 128 | pred = (input[n * (int)n_experts + k] == e) ? 1 : 0; |
| 129 | } |
| 130 | |
| 131 | scan[lid] = pred; |
| 132 | barrier(CLK_LOCAL_MEM_FENCE); |
| 133 | |
| 134 | // Hillis-Steele inclusive scan over the 64 lanes |
| 135 | for (int off = 1; off < 64; off <<= 1) { |
| 136 | int add = (lid >= off) ? scan[lid - off] : 0; |
| 137 | barrier(CLK_LOCAL_MEM_FENCE); |
| 138 | scan[lid] += add; |
| 139 | barrier(CLK_LOCAL_MEM_FENCE); |
| 140 | } |
| 141 | |
| 142 | if (pred) { |
| 143 | const int local_slot = running + (scan[lid] - 1); // exclusive rank |
| 144 | const int tile_idx = tile_offset[e] + (local_slot >> 5); |
| 145 | const int lane = local_slot & 31; |
| 146 | |
| 147 | post_router[tile_idx * 32 + lane] = j; |
| 148 | emap[tile_idx] = (ushort)e; |
| 149 | } |
| 150 | |
| 151 | barrier(CLK_LOCAL_MEM_FENCE); |
| 152 | if (lid == 63) { |
| 153 | running += scan[63]; |
| 154 | } |
| 155 | barrier(CLK_LOCAL_MEM_FENCE); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | __kernel void kernel_moe_fill( |
| 160 | __global int * post_router, |
| 161 | __global int * total_tiles, |
| 162 | int tile_size |
| 163 | ) { |
| 164 | int tile_id = get_global_id(0); |
| 165 | int vec_id_in_tile = get_global_id(1); |
| 166 | |
| 167 | if (tile_id < total_tiles[0]) { |
| 168 | post_router[tile_id * tile_size + vec_id_in_tile] = 0xFFFFFFFF; |
| 169 | } |
| 170 | } |