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 / lightning_indexer.comp
| 1 | #version 450 |
| 2 | |
| 3 | #extension GL_EXT_control_flow_attributes : require |
| 4 | #extension GL_EXT_shader_16bit_storage : require |
| 5 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 6 | |
| 7 | #define BINDING_IDX_K 0u |
| 8 | |
| 9 | #include "types.glsl" |
| 10 | #include "fa_types.glsl" |
| 11 | #define FaTypeV GGML_TYPE_F32 |
| 12 | |
| 13 | layout(constant_id = 0) const uint FaTypeK = GGML_TYPE_F32; |
| 14 | layout(constant_id = 1) const uint FaBlockBytesK = 4; |
| 15 | |
| 16 | #include "flash_attn_dequant.glsl" |
| 17 | |
| 18 | // one workgroup scores a tile of BK keys against BT tokens: the keys are staged once, |
| 19 | // the queries one head at a time, and each invocation owns one key for TPI tokens |
| 20 | #define HEAD_SIZE 128 |
| 21 | #define WG_SIZE 256 |
| 22 | #define BK 64 |
| 23 | #define BT 8 |
| 24 | #define TS (WG_SIZE / BK) |
| 25 | #define TPI (BT / TS) |
| 26 | |
| 27 | layout(local_size_x = WG_SIZE, local_size_y = 1, local_size_z = 1) in; |
| 28 | |
| 29 | layout(binding = 0) readonly buffer QBuf { float q[]; }; |
| 30 | layout(binding = 1) readonly buffer KBufF16 { float16_t k_f16[]; }; |
| 31 | layout(binding = 1) readonly buffer KBufF32 { float k_f32[]; }; |
| 32 | layout(binding = 1) readonly buffer KBufBF16 { uint16_t k_bf16[]; }; |
| 33 | layout(binding = 2) readonly buffer WBuf { float weights[]; }; |
| 34 | layout(binding = 3) readonly buffer MBuf { float16_t mask[]; }; |
| 35 | layout(binding = 4) writeonly buffer DstBuf { float dst[]; }; |
| 36 | |
| 37 | layout(push_constant) uniform PushConstants { |
| 38 | uint n_kv; |
| 39 | uint n_heads; |
| 40 | uint n_tokens; |
| 41 | uint n_masks; |
| 42 | uint q_nb1; |
| 43 | uint q_nb2; |
| 44 | uint q_nb3; |
| 45 | uint k_nb2; |
| 46 | uint k_nb3; |
| 47 | uint w_nb1; |
| 48 | uint w_nb3; |
| 49 | uint m_nb1; |
| 50 | uint m_nb3; |
| 51 | uint d_nb1; |
| 52 | uint d_nb3; |
| 53 | }; |
| 54 | |
| 55 | // the row padding keeps the keys of consecutive invocations in distinct banks |
| 56 | shared f16vec4 k_tile[BK][HEAD_SIZE / 4 + 1]; |
| 57 | shared f16vec4 q_tile[BT][HEAD_SIZE / 4]; |
| 58 | shared float w_tile[BT]; |
| 59 | |
| 60 | void main() { |
| 61 | const uint tid = gl_LocalInvocationID.x; |
| 62 | const uint ik0 = gl_WorkGroupID.x * BK; |
| 63 | const uint t0 = gl_WorkGroupID.y * BT; |
| 64 | const uint s = gl_WorkGroupID.z; |
| 65 | |
| 66 | if (fa_type_needs_shmem(FaTypeK)) { |
| 67 | init_iq_shmem(gl_WorkGroupSize); |
| 68 | } |
| 69 | |
| 70 | // k strides come in as bytes, so scale them down to the view being indexed |
| 71 | const uint k_block_elems = fa_block_elems(FaTypeK); |
| 72 | const uint k_elem_bytes = FaBlockBytesK / k_block_elems; |
| 73 | |
| 74 | // stage the key tile four elements at a time, rows past n_kv are zero |
| 75 | [[unroll]] for (uint i = tid; i < BK * HEAD_SIZE / 4; i += WG_SIZE) { |
| 76 | const uint r = i / (HEAD_SIZE / 4); |
| 77 | const uint c4 = i % (HEAD_SIZE / 4); |
| 78 | |
| 79 | vec4 v = vec4(0.0); |
| 80 | if (ik0 + r < n_kv) { |
| 81 | const uint k_offset = (ik0 + r) * k_nb2 + s * k_nb3; |
| 82 | const uint e = k_offset / k_elem_bytes + c4 * 4; |
| 83 | |
| 84 | if (FaTypeK == GGML_TYPE_F16) { |
| 85 | v = vec4(k_f16[e], k_f16[e + 1], k_f16[e + 2], k_f16[e + 3]); |
| 86 | } else if (FaTypeK == GGML_TYPE_F32) { |
| 87 | v = vec4(k_f32[e], k_f32[e + 1], k_f32[e + 2], k_f32[e + 3]); |
| 88 | } else if (FaTypeK == GGML_TYPE_BF16) { |
| 89 | v = bf16_to_fp32(uvec4(k_bf16[e], k_bf16[e + 1], k_bf16[e + 2], k_bf16[e + 3])); |
| 90 | } else { |
| 91 | v = dequantize4((c4 * 4) / k_block_elems, (c4 * 4) % k_block_elems, k_offset / FaBlockBytesK, BINDING_IDX_K); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | k_tile[r][c4] = f16vec4(v); |
| 96 | } |
| 97 | |
| 98 | const uint kl = tid % BK; |
| 99 | const uint tl = tid / BK; |
| 100 | |
| 101 | float score[TPI]; |
| 102 | [[unroll]] for (uint j = 0; j < TPI; ++j) { |
| 103 | score[j] = 0.0; |
| 104 | } |
| 105 | |
| 106 | for (uint h = 0; h < n_heads; ++h) { |
| 107 | // the previous head is fully consumed and, on the first pass, the key tile is complete |
| 108 | barrier(); |
| 109 | |
| 110 | [[unroll]] for (uint i = tid; i < BT * HEAD_SIZE / 4; i += WG_SIZE) { |
| 111 | const uint r = i / (HEAD_SIZE / 4); |
| 112 | const uint c4 = i % (HEAD_SIZE / 4); |
| 113 | const uint t = t0 + r; |
| 114 | |
| 115 | vec4 v = vec4(0.0); |
| 116 | if (t < n_tokens) { |
| 117 | const uint q_base = h * q_nb1 + t * q_nb2 + s * q_nb3 + c4 * 4; |
| 118 | v = vec4(q[q_base], q[q_base + 1], q[q_base + 2], q[q_base + 3]); |
| 119 | } |
| 120 | q_tile[r][c4] = f16vec4(v); |
| 121 | } |
| 122 | |
| 123 | if (tid < BT) { |
| 124 | const uint t = t0 + tid; |
| 125 | w_tile[tid] = t < n_tokens ? weights[h + t * w_nb1 + s * w_nb3] : 0.0; |
| 126 | } |
| 127 | |
| 128 | barrier(); |
| 129 | |
| 130 | float qk[TPI]; |
| 131 | [[unroll]] for (uint j = 0; j < TPI; ++j) { |
| 132 | qk[j] = 0.0; |
| 133 | } |
| 134 | |
| 135 | [[unroll]] for (uint c4 = 0; c4 < HEAD_SIZE / 4; ++c4) { |
| 136 | const f16vec4 kv = k_tile[kl][c4]; |
| 137 | [[unroll]] for (uint j = 0; j < TPI; ++j) { |
| 138 | const f16vec4 qv = q_tile[tl + j * TS][c4]; |
| 139 | qk[j] += float(dot(kv, qv)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | [[unroll]] for (uint j = 0; j < TPI; ++j) { |
| 144 | score[j] += max(qk[j], 0.0) * w_tile[tl + j * TS]; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | const uint ik = ik0 + kl; |
| 149 | if (ik >= n_kv) { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | [[unroll]] for (uint j = 0; j < TPI; ++j) { |
| 154 | const uint t = t0 + tl + j * TS; |
| 155 | if (t < n_tokens) { |
| 156 | const uint mask_offset = ik + t * m_nb1 + (s % n_masks) * m_nb3; |
| 157 | dst[ik + t * d_nb1 + s * d_nb3] = score[j] + float(mask[mask_offset]); |
| 158 | } |
| 159 | } |
| 160 | } |