interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-webgpu / wgsl-shaders / mul_mat_id_vec.wgsl

136 lines · 4432 bytes Raw Blame History
1#ifdef USE_SUBGROUP_REDUCTION
2enable subgroups;
3#endif
4enable f16;
5
6#define DECLARE_BYTE_LOADERS_SRC0
7#include "common_decls.tmpl"
8
9#include "mul_mat_vec_acc.tmpl"
10
11struct MulMatIdVecParams {
12 offset_src0: u32,
13 offset_src1: u32,
14 offset_ids: u32,
15 offset_dst: u32,
16
17 k: u32,
18 m: u32,
19 n_expert: u32,
20 n_expert_used: u32,
21 b_ne1: u32,
22
23 stride_01: u32,
24 stride_11: u32,
25 stride_02: u32,
26 stride_12: u32,
27};
28
29@group(0) @binding(0) var<storage, read_write> src0: array<SRC0_TYPE>; // [cols, rows, n_expert]
30@group(0) @binding(1) var<storage, read_write> src1: array<SRC1_TYPE>; // [cols, b_ne1, n_tokens(1)]
31@group(0) @binding(2) var<storage, read_write> ids: array<u32>; // [n_experd_used, n_tokens(1)]
32@group(0) @binding(3) var<storage, read_write> dst: array<f32>; // [rows, n_expert_used, n_tokens(1)]
33
34// "mul_mat_vec_acc.tmpl" requires params.k, params.m, params.stride_01
35@group(0) @binding(4) var<uniform> params: MulMatIdVecParams;
36
37// Flattened as [row][thread] to keep each row's reduction contiguous in memory.
38var<workgroup> partial_sums: array<f32, OUTPUTS_PER_WG * WG_SIZE>;
39
40fn partial_index(row: u32, thread: u32) -> u32 {
41 return row * WG_SIZE + thread;
42}
43
44var<workgroup> gathered_count_ids: array<u32, N_EXPERTS>;
45var<workgroup> gathered_expert_used: array<u32, N_EXPERTS>;
46
47@compute @workgroup_size(WG_SIZE)
48fn main(
49 @builtin(local_invocation_id) local_id: vec3<u32>,
50 @builtin(workgroup_id) wg_id: vec3<u32>,
51 @builtin(num_workgroups) num_wg: vec3<u32>
52#ifdef USE_SUBGROUP_REDUCTION
53 , @builtin(subgroup_id) subgroup_id: u32,
54 @builtin(subgroup_invocation_id) subgroup_invocation_id: u32,
55 @builtin(num_subgroups) num_subgroups: u32,
56 @builtin(subgroup_size) subgroup_size: u32
57#endif
58) {
59
60 let thread_id = local_id.x;
61
62 let output_groups: u32 = (params.m + OUTPUTS_PER_WG - 1u) / OUTPUTS_PER_WG;
63 let wg_linear = wg_id.y * num_wg.x + wg_id.x;
64
65 if (wg_linear >= output_groups * params.n_expert_used) {
66 return;
67 }
68
69 let slot = wg_linear / output_groups;
70 let own_expert = ids[params.offset_ids + slot];
71 let row_base = (wg_linear % output_groups) * OUTPUTS_PER_WG;
72
73 // a skipped slot (id -1) keeps the zero accumulator, so the reduction below writes zeros
74 // no early return: the barriers below must stay in uniform control flow
75 let skipped = i32(own_expert) == -1;
76
77 let src0_batch_offset = params.offset_src0 + select(own_expert, 0u, skipped) * params.stride_02;
78 let src1_idx_base = params.offset_src1 + (slot % params.b_ne1) * params.stride_11;
79 let dst_idx_base = params.offset_dst + slot * params.m + row_base;
80
81 var acc: array<array<f32, OUTPUTS_PER_WG>, NUM_COLS>;
82 if (!skipped) {
83 acc = accumulate_vec_dot(thread_id, row_base, src0_batch_offset, src1_idx_base);
84 }
85
86#ifdef USE_SUBGROUP_REDUCTION
87 for (var row = 0u; row < OUTPUTS_PER_WG; row++) {
88 let subgroup_total = subgroupAdd(acc[0][row]);
89 if (subgroup_invocation_id == 0u) {
90 partial_sums[partial_index(row, subgroup_id)] = subgroup_total;
91 }
92 }
93
94 workgroupBarrier();
95
96 for (var row = subgroup_id; (row < OUTPUTS_PER_WG) && (row_base + row < params.m); row += num_subgroups) {
97 let output_row = row_base + row;
98 var row_acc = 0.0f;
99 for (var k = subgroup_invocation_id; k < num_subgroups; k += subgroup_size) {
100 row_acc += partial_sums[partial_index(row, k)];
101 }
102 let row_total = subgroupAdd(row_acc);
103 if (subgroup_invocation_id == 0) {
104 dst[dst_idx_base + row] = row_total;
105 }
106 }
107#endif
108
109#ifdef USE_WORKGROUP_REDUCTION
110 for (var row = 0u; row < OUTPUTS_PER_WG; row++) {
111 partial_sums[partial_index(row, thread_id)] = acc[0][row];
112 }
113
114 workgroupBarrier();
115
116 var stride:u32 = WG_SIZE / 2u;
117
118 while (stride > 0) {
119 if (thread_id < stride) {
120 for (var row = 0u; row < OUTPUTS_PER_WG; row++) {
121 partial_sums[partial_index(row, thread_id)] += partial_sums[partial_index(row, thread_id + stride)];
122 }
123 }
124
125 workgroupBarrier();
126 stride = stride / 2;
127 }
128
129 if (thread_id < OUTPUTS_PER_WG) {
130 let output_row = row_base + thread_id;
131 if (output_row < params.m) {
132 dst[dst_idx_base + thread_id] = partial_sums[partial_index(thread_id, 0)];
133 }
134 }
135#endif
136}