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 / rms_norm_back.comp
| 1 | #version 450 |
| 2 | |
| 3 | #include "generic_head.glsl" |
| 4 | #include "types.glsl" |
| 5 | |
| 6 | #extension GL_EXT_control_flow_attributes : enable |
| 7 | #define BLOCK_SIZE 512 |
| 8 | |
| 9 | layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in; |
| 10 | |
| 11 | layout (binding = 0) readonly buffer G {A_TYPE data_a[];}; |
| 12 | layout (binding = 1) readonly buffer X {B_TYPE data_b[];}; |
| 13 | layout (binding = 2) writeonly buffer D {D_TYPE data_d[];}; |
| 14 | |
| 15 | shared FLOAT_TYPE sum_xx[BLOCK_SIZE]; |
| 16 | shared FLOAT_TYPE sum_xg[BLOCK_SIZE]; |
| 17 | |
| 18 | void main() { |
| 19 | const uint row = gl_WorkGroupID.z * 262144 + gl_WorkGroupID.y * 512 + gl_WorkGroupID.x; |
| 20 | const uint tid = gl_LocalInvocationID.x; |
| 21 | |
| 22 | // Compute derivative of x[i]/norm(x) = g[i]/norm(x) - x[i] dot(x,g)/KX / norm(x)^1.5 |
| 23 | |
| 24 | // partial sums for thread in warp |
| 25 | sum_xx[tid] = FLOAT_TYPE(0.0f); |
| 26 | sum_xg[tid] = FLOAT_TYPE(0.0f); |
| 27 | |
| 28 | [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) { |
| 29 | const FLOAT_TYPE gi = FLOAT_TYPE(data_a[row*p.KX + col]); |
| 30 | const FLOAT_TYPE xi = FLOAT_TYPE(data_b[row*p.KX + col]); |
| 31 | sum_xx[tid] += xi * xi; |
| 32 | sum_xg[tid] += xi * gi; |
| 33 | } |
| 34 | |
| 35 | // sum up partial sums and write back result |
| 36 | barrier(); |
| 37 | [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) { |
| 38 | if (tid < s) { |
| 39 | sum_xx[tid] += sum_xx[tid + s]; |
| 40 | sum_xg[tid] += sum_xg[tid + s]; |
| 41 | } |
| 42 | barrier(); |
| 43 | } |
| 44 | |
| 45 | const FLOAT_TYPE eps = FLOAT_TYPE(p.param1); |
| 46 | const FLOAT_TYPE mean = sum_xx[0] / FLOAT_TYPE(p.KX); |
| 47 | const FLOAT_TYPE scale_g = inversesqrt(mean + eps); |
| 48 | const FLOAT_TYPE scale_x = -scale_g * sum_xg[0] / (sum_xx[0] + FLOAT_TYPE(p.KX) * eps); |
| 49 | |
| 50 | [[unroll]] for (uint col = tid; col < p.KX; col += BLOCK_SIZE) { |
| 51 | data_d[row*p.KX + col] = D_TYPE( |
| 52 | scale_g * FLOAT_TYPE(data_a[row*p.KX + col]) + |
| 53 | scale_x * FLOAT_TYPE(data_b[row*p.KX + col])); |
| 54 | } |
| 55 | } |