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-vulkan / vulkan-shaders / rms_norm.comp

210 lines · 7765 bytes Raw Blame History
1#version 450
2
3#include "generic_binary_head.glsl"
4#include "types.glsl"
5
6#extension GL_EXT_control_flow_attributes : enable
7#if USE_SUBGROUP_ADD
8#extension GL_KHR_shader_subgroup_basic : require
9#extension GL_KHR_shader_subgroup_arithmetic : require
10#endif
11
12layout (constant_id = 3) const int BLOCK_SIZE = 512;
13layout(local_size_x_id = 3, local_size_y = 1, local_size_z = 1) in;
14
15#if RMS_NORM_ROPE_FUSION
16
17layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
18layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
19
20// data is passed from rms_norm -> rope through shared memory.
21// rms_norm calls this data_d, rope calls this rope_data_a.
22// Binding 2 is not used
23// host picks a block size such that ncols <= 2*BLOCK_SIZE
24shared FLOAT_TYPE rope_data_a[2*BLOCK_SIZE];
25#define data_d rope_data_a
26
27layout (binding = 3) readonly buffer R_Y {int rope_data_pos[];};
28layout (binding = 4) readonly buffer R_Z {float rope_data_ff[];};
29layout (binding = 5) writeonly buffer R_D {ROPE_D_TYPE rope_data_d[];};
30layout (binding = 6) readonly buffer R_I {uvec2 rope_data_i[];}; // indices for set_rows
31
32#include "rope_params.glsl"
33#include "rope_funcs.glsl"
34
35#define GGML_ROPE_TYPE_NORMAL 0
36#define GGML_ROPE_TYPE_NEOX 2
37#define GGML_ROPE_TYPE_MROPE 8
38#define GGML_ROPE_TYPE_VISION 24
39
40#elif RMS_NORM_ADD_FUSION
41
42layout (binding = 3) readonly buffer C {float data_c[];};
43layout (binding = 4) readonly buffer E {float data_e[];};
44
45#elif RMS_NORM_SET_ROWS_FUSION
46
47layout (binding = 3) readonly buffer I {uvec2 data_i[];};
48
49#endif
50
51layout (constant_id = 1) const bool do_multiply = false;
52#if RMS_NORM_ADD_FUSION
53layout (constant_id = 2) const bool do_post_multiply = false;
54#endif
55
56shared FLOAT_TYPE sumsh[BLOCK_SIZE];
57
58void rms_norm(uint num_iters) {
59 const uint ncols = p.ne00;
60 const uint nrows = gl_NumWorkGroups.x;
61 const uint nchannels = p.ne02;
62 const uint nsamples = p.ne03;
63
64 const uint row = gl_WorkGroupID.x;
65 const uint tid = gl_LocalInvocationID.x;
66
67 const uint stride_row = p.nb01;
68 const uint stride_channel = p.nb02;
69 const uint stride_sample = p.nb03;
70
71 // grid.y/z are clamped to the device workgroup limit, iterate over the excess channels/samples
72 for (uint samp = gl_WorkGroupID.z; samp < nsamples; samp += gl_NumWorkGroups.z) {
73 for (uint channel = gl_WorkGroupID.y; channel < nchannels; channel += gl_NumWorkGroups.y) {
74 barrier();
75
76 uint32_t a_offset = samp*stride_sample + channel*stride_channel + row*stride_row + get_aoffset();
77 uint32_t b_offset = src1_idx(0, row, channel, samp) + get_boffset();
78#if RMS_NORM_ROPE_FUSION
79 // Per-row offset in shared memory
80 uint32_t d_offset = 0;
81#elif RMS_NORM_SET_ROWS_FUSION
82 uint32_t d_offset = data_i[channel].x*p.nb21 + row*ncols + get_doffset();
83#else
84 uint32_t d_offset = ((samp*nchannels + channel)*nrows + row)*ncols + get_doffset();
85#endif
86 FLOAT_TYPE sum = FLOAT_TYPE(0.0f); // partial sum for thread in warp
87
88 [[unroll]] for (uint col = tid, idx = 0; idx < num_iters; col += BLOCK_SIZE, ++idx) {
89 FLOAT_TYPE xi = FLOAT_TYPE(0);
90 if (col < ncols) {
91 xi = FLOAT_TYPE(data_a[a_offset + col]);
92 }
93 sum += xi * xi;
94 }
95
96 #if USE_SUBGROUP_ADD
97 sum = subgroupAdd(sum);
98 if (subgroupElect()) {
99 sumsh[gl_SubgroupID] = sum;
100 }
101 barrier();
102 sum = FLOAT_TYPE(0.0f);
103 if(gl_NumSubgroups <= 4) { // fast path to skip the 2nd subgroupAdd
104 [[unroll]] for(uint i=0; i < gl_NumSubgroups; ++i) {
105 sum += sumsh[i];
106 }
107 }
108 else {
109 // Each subgroup reads the partials, so one workgroup barrier is enough.
110 for (uint i = gl_SubgroupInvocationID; i < gl_NumSubgroups; i += gl_SubgroupSize) {
111 sum += sumsh[i];
112 }
113 sum = subgroupAdd(sum);
114 }
115 #else
116 sumsh[tid] = sum;
117 // sum up partial sums and write back result
118 barrier();
119 [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
120 if (tid < s) {
121 sum += sumsh[tid + s];
122 sumsh[tid] = sum;
123 }
124 barrier();
125 }
126 sum = sumsh[0];
127 #endif
128
129 const FLOAT_TYPE mean = sum / FLOAT_TYPE(ncols);
130 const FLOAT_TYPE scale = inversesqrt(mean + FLOAT_TYPE(p.param1));
131
132 if (do_multiply) {
133 if (ncols > p.ne10) {
134 [[unroll]] for (uint col = tid, idx = 0; idx < num_iters; col += BLOCK_SIZE, ++idx) {
135 if (col >= ncols) {
136 continue;
137 }
138 FLOAT_TYPE value = scale * FLOAT_TYPE(data_a[a_offset + col]) * FLOAT_TYPE(data_b[b_offset + fastmod(col, p.ne10)]);
139#if RMS_NORM_ADD_FUSION
140 value += FLOAT_TYPE(data_c[d_offset + col]);
141 if (do_post_multiply) {
142 value *= FLOAT_TYPE(data_e[0]);
143 }
144#endif
145 data_d[d_offset + col] = D_TYPE(value);
146 }
147 } else {
148 [[unroll]] for (uint col = tid, idx = 0; idx < num_iters; col += BLOCK_SIZE, ++idx) {
149 if (col >= ncols) {
150 continue;
151 }
152 FLOAT_TYPE value = scale * FLOAT_TYPE(data_a[a_offset + col]) * FLOAT_TYPE(data_b[b_offset + col]);
153#if RMS_NORM_ADD_FUSION
154 value += FLOAT_TYPE(data_c[d_offset + col]);
155 if (do_post_multiply) {
156 value *= FLOAT_TYPE(data_e[0]);
157 }
158#endif
159 data_d[d_offset + col] = D_TYPE(value);
160 }
161 }
162 } else {
163 [[unroll]] for (uint col = tid, idx = 0; idx < num_iters; col += BLOCK_SIZE, ++idx) {
164 if (col >= ncols) {
165 continue;
166 }
167 data_d[d_offset + col] = D_TYPE(scale * FLOAT_TYPE(data_a[a_offset + col]));
168 }
169 }
170#if RMS_NORM_ROPE_FUSION
171 barrier();
172 rope_params rp = p.rope;
173 for (uint t = 2*tid; t < ncols; t += 2*BLOCK_SIZE) {
174 if (rp.rope_mode == GGML_ROPE_TYPE_NEOX) {
175 rope_neox(t, row, channel, samp, rp);
176 } else if (rp.rope_mode == GGML_ROPE_TYPE_NORMAL) {
177 rope_norm(t, row, channel, samp, rp);
178 }
179 }
180#endif
181 }
182 }
183}
184
185void main() {
186 // instantiate the rms_norm function for several different
187 // dimensions, to allow loop unrolling
188 uint num_blocks = (p.ne00 + BLOCK_SIZE - 1) / BLOCK_SIZE;
189 if (num_blocks > 32) {
190 rms_norm(num_blocks);
191 } else if (num_blocks > 16) {
192 rms_norm(32);
193 } else if (num_blocks > 12) {
194 rms_norm(16);
195 } else if (num_blocks > 10) {
196 rms_norm(12);
197 } else if (num_blocks > 8) {
198 rms_norm(10);
199 } else if (num_blocks > 4) {
200 rms_norm(8);
201 } else if (num_blocks == 4) {
202 rms_norm(4);
203 } else if (num_blocks == 3) {
204 rms_norm(3);
205 } else if (num_blocks == 2) {
206 rms_norm(2);
207 } else if (num_blocks == 1) {
208 rms_norm(1);
209 }
210}