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 / cumsum.wgsl
| 1 | @group(0) @binding(0) |
| 2 | var<storage, read_write> src: array<f32>; |
| 3 | |
| 4 | @group(0) @binding(1) |
| 5 | var<storage, read_write> dst: array<f32>; |
| 6 | |
| 7 | struct Params { |
| 8 | offset_src: u32, // in elements |
| 9 | offset_dst: u32, // in elements |
| 10 | ne0: u32, |
| 11 | }; |
| 12 | |
| 13 | @group(0) @binding(2) |
| 14 | var<uniform> params: Params; |
| 15 | |
| 16 | var<workgroup> shared_sum: array<f32, WG_SIZE>; |
| 17 | |
| 18 | @compute @workgroup_size(WG_SIZE) |
| 19 | fn main(@builtin(workgroup_id) wid: vec3<u32>, |
| 20 | @builtin(num_workgroups) num_wg: vec3<u32>, |
| 21 | @builtin(local_invocation_id) lid: vec3<u32>) { |
| 22 | |
| 23 | let wid_i = wid.x + wid.y * num_wg.x; |
| 24 | let row_idx = params.offset_src + wid_i * params.ne0; |
| 25 | let elems = (params.ne0 + WG_SIZE - 1) / WG_SIZE; |
| 26 | var local_sum: f32 = 0.0; |
| 27 | for (var col = lid.x * elems; col < (lid.x + 1) * elems && col < params.ne0; col ++) { |
| 28 | local_sum += src[row_idx + col]; |
| 29 | } |
| 30 | shared_sum[lid.x] = local_sum; |
| 31 | workgroupBarrier(); |
| 32 | |
| 33 | // upsweep |
| 34 | var offset = 1u; |
| 35 | while (offset < WG_SIZE) { |
| 36 | let idx = (lid.x + 1) * offset * 2 - 1; |
| 37 | if (idx < WG_SIZE) { |
| 38 | shared_sum[idx] = shared_sum[idx] + shared_sum[idx - offset]; |
| 39 | } |
| 40 | workgroupBarrier(); |
| 41 | offset <<= 1; |
| 42 | } |
| 43 | |
| 44 | // set last to 0 for exclusive sum |
| 45 | if (lid.x == 0) { |
| 46 | shared_sum[WG_SIZE - 1] = 0.0; |
| 47 | } |
| 48 | workgroupBarrier(); |
| 49 | |
| 50 | // downsweep |
| 51 | offset = WG_SIZE >> 1; |
| 52 | while (offset > 0) { |
| 53 | let idx = (lid.x + 1) * offset * 2 - 1; |
| 54 | if (idx < WG_SIZE) { |
| 55 | let t = shared_sum[idx - offset]; |
| 56 | shared_sum[idx - offset] = shared_sum[idx]; |
| 57 | shared_sum[idx] = shared_sum[idx] + t; |
| 58 | } |
| 59 | workgroupBarrier(); |
| 60 | offset = offset >> 1; |
| 61 | } |
| 62 | |
| 63 | // shared_sum[lid] is exclusive prefix sum up to this thread. |
| 64 | var running_sum = shared_sum[lid.x]; |
| 65 | for (var col = lid.x * elems; col < (lid.x + 1) * elems && col < params.ne0; col ++) { |
| 66 | running_sum += src[row_idx + col]; |
| 67 | dst[params.offset_dst + wid.x * params.ne0 + col] = running_sum; |
| 68 | } |
| 69 | } |