char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Jeff Bolzvulkan: rms_norm fusion opportunities (#28024)9ac8c408a

char/aliceai-t5-moe
5.7 KiB178 linesraw
1#version 450
2
3#include "generic_binary_head.glsl"
4#include "types.glsl"
5
6#if RMS_NORM_ROPE_FUSION
7
8layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
9layout (binding = 1) readonly buffer B {B_TYPE data_b[];};
10
11// data is passed from rms_norm -> rope through shared memory.
12// rms_norm calls this data_d, rope calls this rope_data_a.
13// Binding 2 is not used
14shared FLOAT_TYPE rope_data_a[1024];
15#define data_d rope_data_a
16
17layout (binding = 3) readonly buffer R_Y {int rope_data_pos[];};
18layout (binding = 4) readonly buffer R_Z {float rope_data_ff[];};
19layout (binding = 5) writeonly buffer R_D {ROPE_D_TYPE rope_data_d[];};
20layout (binding = 6) readonly buffer R_I {uvec2 rope_data_i[];}; // indices for set_rows
21
22#include "rope_params.glsl"
23#include "rope_funcs.glsl"
24
25#define GGML_ROPE_TYPE_NORMAL 0
26#define GGML_ROPE_TYPE_NEOX   2
27#define GGML_ROPE_TYPE_MROPE  8
28#define GGML_ROPE_TYPE_VISION 24
29
30#elif RMS_NORM_ADD_FUSION
31
32layout (binding = 3) readonly buffer C {float data_c[];};
33layout (binding = 4) readonly buffer E {float data_e[];};
34
35#elif RMS_NORM_SET_ROWS_FUSION
36
37layout (binding = 3) readonly buffer I {uvec2 data_i[];};
38
39#endif
40
41#extension GL_EXT_control_flow_attributes : enable
42#define BLOCK_SIZE 512
43
44layout (constant_id = 1) const bool do_multiply = false;
45#if RMS_NORM_ADD_FUSION
46layout (constant_id = 2) const bool do_post_multiply = false;
47#endif
48
49layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
50
51shared FLOAT_TYPE sumsh[BLOCK_SIZE];
52
53void rms_norm(uint num_iters) {
54    const uint ncols     = p.ne00;
55    const uint nrows     = gl_NumWorkGroups.x;
56    const uint nchannels = gl_NumWorkGroups.y;
57
58    const uint row       = gl_WorkGroupID.x;
59    const uint channel   = gl_WorkGroupID.y;
60    const uint samp      = gl_WorkGroupID.z;
61    const uint tid       = gl_LocalInvocationID.x;
62
63    const uint stride_row       = p.nb01;
64    const uint stride_channel   = p.nb02;
65    const uint stride_sample    = p.nb03;
66
67    uint32_t a_offset = samp*stride_sample + channel*stride_channel + row*stride_row + get_aoffset();
68    uint32_t b_offset = src1_idx(0, row, channel, samp) + get_boffset();
69#if RMS_NORM_ROPE_FUSION
70    // Per-row offset in shared memory
71    uint32_t d_offset = 0;
72#elif RMS_NORM_SET_ROWS_FUSION
73    uint32_t d_offset = data_i[channel].x*p.nb21 + row*ncols + get_doffset();
74#else
75    uint32_t d_offset = ((samp*nchannels + channel)*nrows + row)*ncols + get_doffset();
76#endif
77    FLOAT_TYPE sum = FLOAT_TYPE(0.0f); // partial sum for thread in warp
78
79    [[unroll]] for (uint col = tid, idx = 0; idx < num_iters; col += BLOCK_SIZE, ++idx) {
80        FLOAT_TYPE xi = FLOAT_TYPE(0);
81        if (col < ncols) {
82            xi = FLOAT_TYPE(data_a[a_offset + col]);
83        }
84        sum += xi * xi;
85    }
86
87    sumsh[tid] = sum;
88    // sum up partial sums and write back result
89    barrier();
90    [[unroll]] for (int s = BLOCK_SIZE / 2; s > 0; s >>= 1) {
91        if (tid < s) {
92            sum += sumsh[tid + s];
93            sumsh[tid] = sum;
94        }
95        barrier();
96    }
97    sum = sumsh[0];
98
99    const FLOAT_TYPE mean = sum / FLOAT_TYPE(ncols);
100    const FLOAT_TYPE scale = inversesqrt(mean + FLOAT_TYPE(p.param1));
101
102    if (do_multiply) {
103        if (ncols > p.ne10) {
104            [[unroll]] for (uint col = tid, idx = 0; idx < num_iters; col += BLOCK_SIZE, ++idx) {
105                if (col >= ncols) {
106                    continue;
107                }
108                FLOAT_TYPE value = scale * FLOAT_TYPE(data_a[a_offset + col]) * FLOAT_TYPE(data_b[b_offset + fastmod(col, p.ne10)]);
109#if RMS_NORM_ADD_FUSION
110                value += FLOAT_TYPE(data_c[d_offset + col]);
111                if (do_post_multiply) {
112                    value *= FLOAT_TYPE(data_e[0]);
113                }
114#endif
115                data_d[d_offset + col] = D_TYPE(value);
116            }
117        } else {
118            [[unroll]] for (uint col = tid, idx = 0; idx < num_iters; col += BLOCK_SIZE, ++idx) {
119                if (col >= ncols) {
120                    continue;
121                }
122                FLOAT_TYPE value = scale * FLOAT_TYPE(data_a[a_offset + col]) * FLOAT_TYPE(data_b[b_offset + col]);
123#if RMS_NORM_ADD_FUSION
124                value += FLOAT_TYPE(data_c[d_offset + col]);
125                if (do_post_multiply) {
126                    value *= FLOAT_TYPE(data_e[0]);
127                }
128#endif
129                data_d[d_offset + col] = D_TYPE(value);
130            }
131        }
132    } else {
133        [[unroll]] for (uint col = tid, idx = 0; idx < num_iters; col += BLOCK_SIZE, ++idx) {
134            if (col >= ncols) {
135                continue;
136            }
137            data_d[d_offset + col] = D_TYPE(scale * FLOAT_TYPE(data_a[a_offset + col]));
138        }
139    }
140#if RMS_NORM_ROPE_FUSION
141    barrier();
142    rope_params rp = p.rope;
143    for (uint t = 2*tid; t < ncols; t += 2*BLOCK_SIZE) {
144        if (rp.rope_mode == GGML_ROPE_TYPE_NEOX) {
145            rope_neox(t, row, channel, samp, rp);
146        } else if (rp.rope_mode == GGML_ROPE_TYPE_NORMAL) {
147            rope_norm(t, row, channel, samp, rp);
148        }
149    }
150#endif
151}
152
153void main() {
154    // instantiate the rms_norm function for several different
155    // dimensions, to allow loop unrolling
156    uint num_blocks = (p.ne00 + BLOCK_SIZE - 1) / BLOCK_SIZE;
157    if (num_blocks > 32) {
158        rms_norm(num_blocks);
159    } else if (num_blocks > 16) {
160        rms_norm(32);
161    } else if (num_blocks > 12) {
162        rms_norm(16);
163    } else if (num_blocks > 10) {
164        rms_norm(12);
165    } else if (num_blocks > 8) {
166        rms_norm(10);
167    } else if (num_blocks > 4) {
168        rms_norm(8);
169    } else if (num_blocks == 4) {
170        rms_norm(4);
171    } else if (num_blocks == 3) {
172        rms_norm(3);
173    } else if (num_blocks == 2) {
174        rms_norm(2);
175    } else if (num_blocks == 1) {
176        rms_norm(1);
177    }
178}