ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-vulkan / vulkan-shaders / count_experts.comp
| 1 | #version 450 |
| 2 | |
| 3 | #extension GL_EXT_control_flow_attributes : enable |
| 4 | |
| 5 | #ifdef USE_SUBGROUPS |
| 6 | #extension GL_KHR_shader_subgroup_basic : enable |
| 7 | #extension GL_KHR_shader_subgroup_arithmetic : enable |
| 8 | #extension GL_KHR_shader_subgroup_vote : enable |
| 9 | #endif |
| 10 | |
| 11 | #include "types.glsl" |
| 12 | #include "utils.glsl" |
| 13 | |
| 14 | layout (push_constant) uniform parameter |
| 15 | { |
| 16 | uint32_t ne00; |
| 17 | uint32_t ne01; |
| 18 | uint32_t nb00; |
| 19 | uint32_t nb01; |
| 20 | uint32_t a_offset; |
| 21 | uint32_t n_experts; |
| 22 | uint32_t ne00mp; |
| 23 | uint32_t ne00L; |
| 24 | uint32_t row_tile_size; |
| 25 | uint32_t row_ids_offset; |
| 26 | uint32_t ne0_dst; |
| 27 | uint32_t stride_dst; |
| 28 | uint32_t batch_stride_dst; |
| 29 | } p; |
| 30 | |
| 31 | #define BLOCK_SIZE 256 |
| 32 | #define EXPERTS_PER_CHUNK 1024 |
| 33 | |
| 34 | layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in; |
| 35 | |
| 36 | layout (binding = 0) readonly buffer A {uint data_a[];}; |
| 37 | layout (binding = 1) writeonly buffer D {uint data_d[];}; |
| 38 | layout (binding = 2) writeonly buffer DST {float data_dst[];}; |
| 39 | |
| 40 | shared uint vals[EXPERTS_PER_CHUNK]; |
| 41 | shared uint offsets[EXPERTS_PER_CHUNK]; |
| 42 | shared uint cursors[EXPERTS_PER_CHUNK]; |
| 43 | shared uint skipped_rows; |
| 44 | shared uint skip_count; |
| 45 | |
| 46 | // data_d[0] is the tile count. Each tile has (expert, valid rows). |
| 47 | // Packed (token << 16) | slot row ids start at p.row_ids_offset, with each expert on a tile boundary. |
| 48 | void write_tiles(uint expert, uint count, uint tile_begin, uint n_tiles) { |
| 49 | for (uint t = 0; t < n_tiles; ++t) { |
| 50 | const uint descriptor = 1 + 2 * (tile_begin + t); |
| 51 | data_d[descriptor] = expert; |
| 52 | data_d[descriptor + 1] = min(p.row_tile_size, count - t * p.row_tile_size); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // The matmul does not write skipped rows, so collect their offsets and zero them cooperatively. |
| 57 | void zero_skipped_rows() { |
| 58 | const uint num_elements = p.ne00 * p.ne01; |
| 59 | const uint tid = gl_LocalInvocationID.x; |
| 60 | |
| 61 | // Reuse offsets[] as a queue, so each batch must fit in EXPERTS_PER_CHUNK. |
| 62 | for (uint base = 0; base < num_elements; base += EXPERTS_PER_CHUNK) { |
| 63 | const uint end = min(base + EXPERTS_PER_CHUNK, num_elements); |
| 64 | if (tid == 0) { |
| 65 | skip_count = 0; |
| 66 | } |
| 67 | barrier(); |
| 68 | |
| 69 | for (uint idx = base + tid; idx < end; idx += BLOCK_SIZE) { |
| 70 | const uint i01 = fastdiv(idx, p.ne00mp, p.ne00L); |
| 71 | const uint i00 = idx - i01 * p.ne00; |
| 72 | |
| 73 | if (int(data_a[p.a_offset + i01 * p.nb01 + i00 * p.nb00]) == -1) { |
| 74 | const uint slot = atomicAdd(skip_count, 1); |
| 75 | offsets[slot] = i00 * p.stride_dst + i01 * p.batch_stride_dst; |
| 76 | } |
| 77 | } |
| 78 | barrier(); |
| 79 | |
| 80 | const uint n_skipped = skip_count; |
| 81 | for (uint r = 0; r < n_skipped; ++r) { |
| 82 | const uint d_off = offsets[r]; |
| 83 | for (uint i = tid; i < p.ne0_dst; i += BLOCK_SIZE) { |
| 84 | data_dst[d_off + i] = 0.0f; |
| 85 | } |
| 86 | } |
| 87 | barrier(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void main() { |
| 92 | const uint num_elements = p.ne00 * p.ne01; |
| 93 | const uint tid = gl_LocalInvocationID.x; |
| 94 | const uint tile_shift = findLSB(p.row_tile_size); |
| 95 | uint total_tiles = 0; |
| 96 | uint local_skipped = 0; |
| 97 | if (tid == 0) { |
| 98 | skipped_rows = 0; |
| 99 | } |
| 100 | barrier(); |
| 101 | #ifdef USE_SUBGROUPS |
| 102 | // Use the subgroup that contains invocation 0. |
| 103 | const bool prefix_subgroup = subgroupAny(tid == 0); |
| 104 | #endif |
| 105 | |
| 106 | for (uint expert_base = 0; expert_base < p.n_experts;) { |
| 107 | const uint n_experts = min(EXPERTS_PER_CHUNK, p.n_experts - expert_base); |
| 108 | for (uint e = tid; e < n_experts; e += BLOCK_SIZE) { |
| 109 | vals[e] = 0; |
| 110 | } |
| 111 | barrier(); |
| 112 | |
| 113 | for (uint idx = tid; idx < num_elements; idx += BLOCK_SIZE) { |
| 114 | const uint i01 = fastdiv(idx, p.ne00mp, p.ne00L); |
| 115 | const uint i00 = idx - i01 * p.ne00; |
| 116 | const uint expert = data_a[p.a_offset + i01 * p.nb01 + i00 * p.nb00] - expert_base; |
| 117 | if (expert < n_experts) { |
| 118 | atomicAdd(vals[expert], 1); |
| 119 | } |
| 120 | } |
| 121 | barrier(); |
| 122 | |
| 123 | #ifdef USE_SUBGROUPS |
| 124 | if (prefix_subgroup) { |
| 125 | const uint n_experts_padded = (n_experts + gl_SubgroupSize - 1) & ~(gl_SubgroupSize - 1); |
| 126 | uint base = subgroupAdd(tid == 0 ? total_tiles : 0u); |
| 127 | for (uint e = gl_SubgroupInvocationID; e < n_experts_padded; e += gl_SubgroupSize) { |
| 128 | const bool in_range = e < n_experts; |
| 129 | const uint count = in_range ? vals[e] : 0; |
| 130 | const uint n_tiles = (count + p.row_tile_size - 1) >> tile_shift; |
| 131 | const uint tile_begin = base + subgroupExclusiveAdd(n_tiles); |
| 132 | if (in_range) { |
| 133 | offsets[e] = tile_begin * p.row_tile_size; |
| 134 | cursors[e] = 0; |
| 135 | write_tiles(expert_base + e, count, tile_begin, n_tiles); |
| 136 | } |
| 137 | base += subgroupAdd(n_tiles); |
| 138 | } |
| 139 | if (tid == 0) { |
| 140 | total_tiles = base; |
| 141 | } |
| 142 | } |
| 143 | #else |
| 144 | if (tid == 0) { |
| 145 | for (uint e = 0; e < n_experts; ++e) { |
| 146 | const uint count = vals[e]; |
| 147 | const uint n_tiles = (count + p.row_tile_size - 1) >> tile_shift; |
| 148 | offsets[e] = total_tiles * p.row_tile_size; |
| 149 | cursors[e] = 0; |
| 150 | write_tiles(expert_base + e, count, total_tiles, n_tiles); |
| 151 | total_tiles += n_tiles; |
| 152 | } |
| 153 | } |
| 154 | #endif |
| 155 | barrier(); |
| 156 | |
| 157 | for (uint idx = tid; idx < num_elements; idx += BLOCK_SIZE) { |
| 158 | const uint i01 = fastdiv(idx, p.ne00mp, p.ne00L); |
| 159 | const uint i00 = idx - i01 * p.ne00; |
| 160 | const uint id = data_a[p.a_offset + i01 * p.nb01 + i00 * p.nb00]; |
| 161 | // Count skipped slots only in the first expert chunk. |
| 162 | if (expert_base == 0 && int(id) == -1) { |
| 163 | local_skipped++; |
| 164 | } |
| 165 | const uint expert = id - expert_base; |
| 166 | if (expert < n_experts) { |
| 167 | const uint row = atomicAdd(cursors[expert], 1); |
| 168 | data_d[p.row_ids_offset + offsets[expert] + row] = (i01 << 16) | i00; |
| 169 | } |
| 170 | } |
| 171 | // The next chunk clears vals. Scatter uses only offsets and cursors. |
| 172 | expert_base += n_experts; |
| 173 | } |
| 174 | if (local_skipped != 0) { |
| 175 | atomicAdd(skipped_rows, local_skipped); |
| 176 | } |
| 177 | // All scatter reads must finish before reusing offsets[] as a queue. |
| 178 | barrier(); |
| 179 | |
| 180 | if (tid == 0) { |
| 181 | data_d[0] = total_tiles; |
| 182 | } |
| 183 | |
| 184 | if (skipped_rows != 0) { |
| 185 | zero_skipped_rows(); |
| 186 | } |
| 187 | } |