Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-webgpu/wgsl-shaders/mul_mat_id_gather.wgsl # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-webgpu / wgsl-shaders / mul_mat_id_gather.wgsl` 100 lines ยท 3403 bytes Raw Blame History 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 ids: array; // [n_expert_used, n_tokens] 17 @group(0) @binding(1) var ids_slot_pos: array; // [n_expert_used * n_tokens] 18 @group(0) @binding(2) var ids_tiles: array; // [n_tiles, (expert, first slot, n slots) * n_tiles] 19 20 @group(0) @binding(3) var dst: array; // [m, n_expert_used, n_tokens] 21 22 @group(0) @binding(4) var 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 ids_count: array, 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) { 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 }