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_partials.comp
| 1 | #version 450 |
| 2 | |
| 3 | #include "generic_binary_head.glsl" |
| 4 | #include "types.glsl" |
| 5 | |
| 6 | #extension GL_EXT_control_flow_attributes : enable |
| 7 | #extension GL_KHR_shader_subgroup_arithmetic : enable |
| 8 | #extension GL_KHR_shader_subgroup_basic : enable |
| 9 | |
| 10 | #define BLOCK_SIZE 128 |
| 11 | |
| 12 | layout (constant_id = 1) const bool do_multiply = false; |
| 13 | #if RMS_NORM_ADD_FUSION |
| 14 | layout (constant_id = 2) const bool do_post_multiply = false; |
| 15 | #endif |
| 16 | |
| 17 | layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in; |
| 18 | |
| 19 | layout (binding = 3, std430) readonly buffer PartialsBuf {float partial_sums[];}; |
| 20 | |
| 21 | #if RMS_NORM_ADD_FUSION |
| 22 | layout (binding = 4) readonly buffer C {float data_c[];}; |
| 23 | layout (binding = 5) readonly buffer E {float data_e[];}; |
| 24 | #endif |
| 25 | |
| 26 | shared FLOAT_TYPE sumsh[BLOCK_SIZE]; |
| 27 | |
| 28 | void main() { |
| 29 | const uint ncols = p.ne00; |
| 30 | const uint nrows = gl_NumWorkGroups.x; |
| 31 | const uint nchannels = gl_NumWorkGroups.y; |
| 32 | |
| 33 | const uint row = 0; |
| 34 | const uint channel = gl_WorkGroupID.y; |
| 35 | const uint samp = gl_WorkGroupID.z; |
| 36 | // The work is split across multiple workgroups in the x dimension. Each invocation |
| 37 | // processes one element |
| 38 | const uint tid = gl_GlobalInvocationID.x; |
| 39 | |
| 40 | const uint stride_row = p.nb01; |
| 41 | const uint stride_channel = p.nb02; |
| 42 | const uint stride_sample = p.nb03; |
| 43 | |
| 44 | uint32_t a_offset = samp*stride_sample + channel*stride_channel + row*stride_row + get_aoffset(); |
| 45 | uint32_t b_offset = src1_idx(0, row, channel, samp) + get_boffset(); |
| 46 | uint32_t d_offset = ((samp*nchannels + channel)*nrows + row)*ncols + get_doffset(); |
| 47 | |
| 48 | FLOAT_TYPE sum = FLOAT_TYPE(0.0f); // partial sum for thread in warp |
| 49 | |
| 50 | uint32_t num_partials = p.param3; |
| 51 | for (uint32_t i = gl_SubgroupInvocationID; i < num_partials; i += gl_SubgroupSize) { |
| 52 | sum += partial_sums[i]; |
| 53 | } |
| 54 | sum = subgroupAdd(sum); |
| 55 | |
| 56 | uint col = tid; |
| 57 | if (col >= ncols) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | const FLOAT_TYPE mean = sum / FLOAT_TYPE(ncols); |
| 62 | const FLOAT_TYPE scale = inversesqrt(mean + FLOAT_TYPE(p.param1)); |
| 63 | |
| 64 | if (do_multiply) { |
| 65 | if (ncols > p.ne10) { |
| 66 | FLOAT_TYPE value = scale * FLOAT_TYPE(data_a[a_offset + col]) * FLOAT_TYPE(data_b[b_offset + fastmod(col, p.ne10)]); |
| 67 | #if RMS_NORM_ADD_FUSION |
| 68 | value += FLOAT_TYPE(data_c[d_offset + col]); |
| 69 | if (do_post_multiply) { |
| 70 | value *= FLOAT_TYPE(data_e[0]); |
| 71 | } |
| 72 | #endif |
| 73 | data_d[d_offset + col] = D_TYPE(value); |
| 74 | } else { |
| 75 | FLOAT_TYPE value = scale * FLOAT_TYPE(data_a[a_offset + col]) * FLOAT_TYPE(data_b[b_offset + col]); |
| 76 | #if RMS_NORM_ADD_FUSION |
| 77 | value += FLOAT_TYPE(data_c[d_offset + col]); |
| 78 | if (do_post_multiply) { |
| 79 | value *= FLOAT_TYPE(data_e[0]); |
| 80 | } |
| 81 | #endif |
| 82 | data_d[d_offset + col] = D_TYPE(value); |
| 83 | } |
| 84 | } else { |
| 85 | data_d[d_offset + col] = D_TYPE(scale * FLOAT_TYPE(data_a[a_offset + col])); |
| 86 | } |
| 87 | } |