Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-vulkan/vulkan-shaders/wkv6.comp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-vulkan / vulkan-shaders / wkv6.comp` 87 lines ยท 2720 bytes Raw Blame History 1 #version 450 2 3 #extension GL_EXT_control_flow_attributes : require 4 5 #define BLOCK_SIZE 64 6 layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in; 7 8 layout(push_constant) uniform Parameters { 9 uint B; 10 uint T; 11 uint C; 12 uint H; 13 }; 14 15 layout(binding = 0) readonly buffer KBuf { A_TYPE k[]; }; 16 layout(binding = 1) readonly buffer VBuf { A_TYPE v[]; }; 17 layout(binding = 2) readonly buffer RBuf { A_TYPE r[]; }; 18 layout(binding = 3) readonly buffer TimeFBuf { A_TYPE tf[]; }; 19 layout(binding = 4) readonly buffer TimeDBuf { A_TYPE td[]; }; 20 layout(binding = 5) readonly buffer StateBuf { A_TYPE state_in[]; }; 21 layout(binding = 6) buffer DstBuf { A_TYPE dst[]; }; 22 23 shared A_TYPE _k[BLOCK_SIZE], _r[BLOCK_SIZE], _tf[BLOCK_SIZE], _td[BLOCK_SIZE]; 24 25 void 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 }