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 / dsv4_hc_post.comp
| 1 | #version 450 |
| 2 | |
| 3 | #extension GL_EXT_control_flow_attributes : require |
| 4 | |
| 5 | // Fan one stream back out to hc streams and add the combination-weighted |
| 6 | // residuals: |
| 7 | // |
| 8 | // dst[i0, idst, it] = x[i0, it]*post[idst, it] |
| 9 | // + sum_isrc residual[i0, isrc, it]*comb[idst, isrc, it] |
| 10 | // |
| 11 | // HAS_COMB == 0: identity mixing, each stream keeps its own residual: |
| 12 | // |
| 13 | // dst[i0, idst, it] = x[i0, it]*post[idst, it] + residual[i0, idst, it] |
| 14 | |
| 15 | layout(constant_id = 0) const uint BLOCK_SIZE = 256; |
| 16 | layout(constant_id = 1) const uint HAS_COMB = 1; |
| 17 | |
| 18 | layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 19 | |
| 20 | layout(push_constant) uniform parameter |
| 21 | { |
| 22 | uint n_embd; |
| 23 | uint n_tokens; |
| 24 | |
| 25 | uint nbx0; uint nbx1; // x |
| 26 | uint nbr0; uint nbr1; uint nbr2; // residual |
| 27 | uint nbp0; uint nbp1; // post |
| 28 | uint nbc0; uint nbc1; uint nbc2; // comb |
| 29 | uint nbd0; uint nbd1; uint nbd2; // dst |
| 30 | |
| 31 | uint x_offset; |
| 32 | uint r_offset; |
| 33 | uint p_offset; |
| 34 | uint c_offset; |
| 35 | uint d_offset; |
| 36 | |
| 37 | uint gate; // post = gate_scale_out*sigmoid(gate_scale_in*p) |
| 38 | float gate_scale_in; |
| 39 | float gate_scale_out; |
| 40 | }; |
| 41 | |
| 42 | layout(binding = 0, std430) readonly buffer X { float data_x[]; }; |
| 43 | layout(binding = 1, std430) readonly buffer R { float data_r[]; }; |
| 44 | layout(binding = 2, std430) readonly buffer P { float data_p[]; }; |
| 45 | layout(binding = 3, std430) readonly buffer C { float data_c[]; }; |
| 46 | layout(binding = 4, std430) writeonly buffer D { float data_d[]; }; |
| 47 | |
| 48 | const uint hc = 4; |
| 49 | |
| 50 | shared float post_s[hc]; |
| 51 | shared float comb_s[hc * hc]; |
| 52 | |
| 53 | void main() { |
| 54 | const uint tid = gl_LocalInvocationID.x; |
| 55 | const uint it = gl_WorkGroupID.y; |
| 56 | |
| 57 | if (tid < hc) { |
| 58 | const float p = data_p[p_offset + tid * nbp0 + it * nbp1]; |
| 59 | post_s[tid] = gate != 0 ? (1.0f / (1.0f + exp(-(p * gate_scale_in)))) * gate_scale_out : p; |
| 60 | } |
| 61 | if (HAS_COMB == 1 && tid < hc * hc) { |
| 62 | const uint idst = tid & 3; |
| 63 | const uint isrc = tid >> 2; |
| 64 | comb_s[tid] = data_c[c_offset + idst * nbc0 + isrc * nbc1 + it * nbc2]; |
| 65 | } |
| 66 | barrier(); |
| 67 | |
| 68 | // After the barrier, so every invocation reaches it. |
| 69 | const uint i0 = gl_WorkGroupID.x * BLOCK_SIZE + tid; |
| 70 | if (i0 >= n_embd) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | const float xv = data_x[x_offset + i0 * nbx0 + it * nbx1]; |
| 75 | |
| 76 | const uint rb = r_offset + i0 * nbr0 + it * nbr2; |
| 77 | |
| 78 | float r[hc]; |
| 79 | [[unroll]] |
| 80 | for (uint isrc = 0; isrc < hc; ++isrc) { |
| 81 | r[isrc] = data_r[rb + isrc * nbr1]; |
| 82 | } |
| 83 | |
| 84 | [[unroll]] |
| 85 | for (uint idst = 0; idst < hc; ++idst) { |
| 86 | float result = xv * post_s[idst]; |
| 87 | if (HAS_COMB == 1) { |
| 88 | [[unroll]] |
| 89 | for (uint isrc = 0; isrc < hc; ++isrc) { |
| 90 | result = fma(r[isrc], comb_s[idst + hc * isrc], result); |
| 91 | } |
| 92 | } else { |
| 93 | result += r[idst]; |
| 94 | } |
| 95 | data_d[d_offset + i0 * nbd0 + idst * nbd1 + it * nbd2] = result; |
| 96 | } |
| 97 | } |