ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-webgpu / wgsl-shaders / mul_mat_id_gather.wgsl
| 1 | enable f16; |
| 2 | |
| 3 | struct MulMatIdGatherParams { |
| 4 | offset_ids: u32, |
| 5 | offset_dst: u32, |
| 6 | |
| 7 | n_expert: u32, |
| 8 | n_expert_used: u32, |
| 9 | n_tokens: u32, |
| 10 | |
| 11 | stride_ids_1: u32, |
| 12 | |
| 13 | m: u32, |
| 14 | }; |
| 15 | |
| 16 | @group(0) @binding(0) var<storage, read_write> ids: array<i32>; // [n_expert_used, n_tokens] |
| 17 | @group(0) @binding(1) var<storage, read_write> ids_slot_pos: array<u32>; // [n_expert_used * n_tokens] |
| 18 | @group(0) @binding(2) var<storage, read_write> ids_tiles: array<u32>; // [n_tiles, (expert, first slot, n slots) * n_tiles] |
| 19 | |
| 20 | @group(0) @binding(3) var<storage, read_write> dst: array<f32>; // [m, n_expert_used, n_tokens] |
| 21 | |
| 22 | @group(0) @binding(4) var<uniform> params: MulMatIdGatherParams; |
| 23 | |
| 24 | // per expert: the slot count, replaced by the first slot of the expert in ids_slot_pos by the prefix sum, then advanced as its write cursor |
| 25 | var<workgroup> ids_count: array<atomic<u32>, N_EXPERT>; |
| 26 | |
| 27 | // launched as a single workgroup, where the threads process the elements of ids in parallel with atomics |
| 28 | @compute @workgroup_size(WG_SIZE) |
| 29 | fn main(@builtin(local_invocation_id) local_id: vec3<u32>) { |
| 30 | |
| 31 | let thread_id = local_id.x; |
| 32 | let ids_slots_num = params.n_expert_used * params.n_tokens; |
| 33 | |
| 34 | for (var e = thread_id; e < N_EXPERT; e += WG_SIZE) { |
| 35 | atomicStore(&ids_count[e], 0u); |
| 36 | } |
| 37 | |
| 38 | workgroupBarrier(); |
| 39 | |
| 40 | // count the slots of each expert |
| 41 | for (var i = thread_id; i < ids_slots_num; i += WG_SIZE) { |
| 42 | let row = i / params.n_expert_used; |
| 43 | let col = i % params.n_expert_used; |
| 44 | let expert = ids[params.offset_ids + row * params.stride_ids_1 + col]; |
| 45 | if (expert != -1) { // skipped slot, owned by no expert |
| 46 | atomicAdd(&ids_count[u32(expert)], 1u); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | workgroupBarrier(); |
| 51 | |
| 52 | // exclusive prefix sum over the counts, and emit one tile descriptor per `TILE_N_SLOTS` slots of each expert |
| 53 | if (thread_id == 0u) { |
| 54 | var base: u32 = 0; |
| 55 | var tile: u32 = 0; |
| 56 | |
| 57 | for (var e: u32 = 0; e < N_EXPERT; e += 1) { |
| 58 | let n = atomicLoad(&ids_count[e]); |
| 59 | |
| 60 | atomicStore(&ids_count[e], base); |
| 61 | |
| 62 | for (var k: u32 = 0; k < n; k += TILE_N_SLOTS) { |
| 63 | ids_tiles[1 + 3 * tile] = e; |
| 64 | ids_tiles[1 + 3 * tile + 1] = base + k; |
| 65 | ids_tiles[1 + 3 * tile + 2] = min(n - k, TILE_N_SLOTS); |
| 66 | tile += 1; |
| 67 | } |
| 68 | |
| 69 | base += n; |
| 70 | } |
| 71 | |
| 72 | ids_tiles[0] = tile; |
| 73 | } |
| 74 | |
| 75 | workgroupBarrier(); |
| 76 | |
| 77 | // store the position in ids tensor to ids_slot_pos |
| 78 | for (var i = thread_id; i < ids_slots_num; i += WG_SIZE) { |
| 79 | let row = i / params.n_expert_used; |
| 80 | let col = i % params.n_expert_used; |
| 81 | let expert = ids[params.offset_ids + row * params.stride_ids_1 + col]; |
| 82 | if (expert != -1) { |
| 83 | let pos = atomicAdd(&ids_count[u32(expert)], 1u); |
| 84 | ids_slot_pos[pos] = i; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // the matmul writes only the rows that an expert owns, so zero the rows of the skipped slots |
| 89 | for (var i: u32 = 0; i < ids_slots_num; i += 1) { |
| 90 | let row = i / params.n_expert_used; |
| 91 | let col = i % params.n_expert_used; |
| 92 | if (ids[params.offset_ids + row * params.stride_ids_1 + col] != -1) { |
| 93 | continue; |
| 94 | } |
| 95 | let dst_row = params.offset_dst + i * params.m; |
| 96 | for (var r = thread_id; r < params.m; r += WG_SIZE) { |
| 97 | dst[dst_row + r] = 0.0f; |
| 98 | } |
| 99 | } |
| 100 | } |