char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
160bc039c
char/aliceai-t5-moe
1#version 450 2 3#extension GL_EXT_control_flow_attributes : require 4 5#define BLOCK_SIZE 64 6layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in; 7 8layout(push_constant) uniform Parameters { 9 uint B; 10 uint T; 11 uint C; 12 uint H; 13}; 14 15layout(binding = 0) readonly buffer KBuf { A_TYPE k[]; }; 16layout(binding = 1) readonly buffer VBuf { A_TYPE v[]; }; 17layout(binding = 2) readonly buffer RBuf { A_TYPE r[]; }; 18layout(binding = 3) readonly buffer TimeFBuf { A_TYPE tf[]; }; 19layout(binding = 4) readonly buffer TimeDBuf { A_TYPE td[]; }; 20layout(binding = 5) readonly buffer StateBuf { A_TYPE state_in[]; }; 21layout(binding = 6) buffer DstBuf { A_TYPE dst[]; }; 22 23shared A_TYPE _k[BLOCK_SIZE], _r[BLOCK_SIZE], _tf[BLOCK_SIZE], _td[BLOCK_SIZE]; 24 25void main() { 26 const uint head_size = BLOCK_SIZE; 27 const uint batch_id = gl_WorkGroupID.x / H; 28 const uint head_id = gl_WorkGroupID.x % H; 29 const uint tid = gl_LocalInvocationID.x; 30 31 const uint state_size = C * head_size; 32 const uint n_seq_tokens = T / B; 33 34 if (batch_id >= B || head_id >= H) { 35 return; 36 } 37 38 A_TYPE state[BLOCK_SIZE]; 39 [[unroll]] for (uint i = 0; i < head_size; i++) { 40 state[i] = state_in[batch_id * state_size + head_id * head_size * head_size 41 + i * head_size + tid]; 42 } 43 44 barrier(); 45 _tf[tid] = tf[head_id * head_size + tid]; 46 barrier(); 47 48 const uint start_t = batch_id * n_seq_tokens * C + head_id * head_size + tid; 49 const uint end_t = (batch_id + 1) * n_seq_tokens * C + head_id * head_size + tid; 50 51 for (uint t = start_t; t < end_t; t += C) { 52 barrier(); 53 _k[tid] = k[t]; 54 _r[tid] = r[t]; 55 _td[tid] = td[t]; 56 barrier(); 57 58 const A_TYPE v_val = v[t]; 59 A_TYPE y = 0.0; 60 61 [[unroll]] for (uint j = 0; j < head_size; j += 4) { 62 vec4 k_vec = vec4(_k[j], _k[j+1], _k[j+2], _k[j+3]); 63 vec4 r_vec = vec4(_r[j], _r[j+1], _r[j+2], _r[j+3]); 64 vec4 tf_vec = vec4(_tf[j], _tf[j+1], _tf[j+2], _tf[j+3]); 65 vec4 td_vec = vec4(_td[j], _td[j+1], _td[j+2], _td[j+3]); 66 vec4 s_vec = vec4(state[j], state[j+1], state[j+2], state[j+3]); 67 68 vec4 kv = k_vec * v_val; 69 70 vec4 temp = tf_vec * kv + s_vec; 71 y += dot(r_vec, temp); 72 73 s_vec = s_vec * td_vec + kv; 74 state[j] = s_vec.x; 75 state[j+1] = s_vec.y; 76 state[j+2] = s_vec.z; 77 state[j+3] = s_vec.w; 78 } 79 80 dst[t] = y; 81 } 82 83 [[unroll]] for (uint i = 0; i < head_size; i++) { 84 dst[T * C + batch_id * state_size + head_id * head_size * head_size 85 + i * head_size + tid] = state[i]; 86 } 87}