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 / flash_attn_decode_phase_1.comp
| 1 | #version 450 |
| 2 | |
| 3 | #extension GL_EXT_control_flow_attributes : enable |
| 4 | #extension GL_EXT_shader_16bit_storage : require |
| 5 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 6 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : require |
| 7 | #extension GL_KHR_memory_scope_semantics : enable |
| 8 | #extension GL_KHR_shader_subgroup_basic : enable |
| 9 | #extension GL_KHR_shader_subgroup_ballot : enable |
| 10 | #extension GL_KHR_shader_subgroup_arithmetic : enable |
| 11 | #extension GL_KHR_cooperative_matrix : enable |
| 12 | #extension GL_EXT_shared_memory_block : enable |
| 13 | |
| 14 | layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 15 | |
| 16 | layout (binding = 0) readonly buffer Q {float16_t qState[];}; |
| 17 | layout (binding = 1) readonly buffer K_VEC4 {f16vec4 kStateVec4[];}; |
| 18 | layout (binding = 2) buffer MASK_F16 {float16_t mState_f16[];}; |
| 19 | layout (binding = 3) buffer P_FP16 {float16_t matP_f16[];}; |
| 20 | layout (binding = 4) buffer OUT_MAX {float out_max_f32[];}; |
| 21 | |
| 22 | layout (push_constant) uniform parameter |
| 23 | { |
| 24 | uint kvSeqLen; |
| 25 | uint activationLength; |
| 26 | uint qHead; |
| 27 | uint kvHead; |
| 28 | uint qkRatio; |
| 29 | uint qkSubGroups; |
| 30 | uint flag; |
| 31 | uint kvStride1; |
| 32 | uint kvStride2; |
| 33 | uint batchStrideQ; |
| 34 | uint batchStrideK; |
| 35 | uint batchStrideV; |
| 36 | uint batchStrideM; |
| 37 | uint batchStrideO; |
| 38 | float softMaxScale; |
| 39 | } p; |
| 40 | |
| 41 | layout (constant_id = 0) const uint GROUPSIZE = 128; |
| 42 | layout (constant_id = 1) const uint GQA_RATIO = 8; |
| 43 | layout (constant_id = 2) const uint HEAD_DIM = 128; |
| 44 | layout (constant_id = 3) const uint WARPSIZE = 16; |
| 45 | layout (constant_id = 4) const uint MATP_REDUCE = 32; |
| 46 | layout (constant_id = 5) const uint N_TOK = 1; |
| 47 | layout (constant_id = 6) const uint COOP_MAT_P_PER_LOOP = 4; |
| 48 | |
| 49 | #define MAX_HEADS 8 |
| 50 | |
| 51 | #define TN WARPSIZE |
| 52 | #define TM 8 |
| 53 | #define TK 16 |
| 54 | #define SUBGROUP_COUNT (GROUPSIZE / WARPSIZE) |
| 55 | #define MATP_PER_LOOP (COOP_MAT_P_PER_LOOP * TM) |
| 56 | #define P_LOOP_COUNT (MATP_REDUCE / MATP_PER_LOOP) |
| 57 | |
| 58 | #define COOP_MAT_Q_PER_TOKEN ((GQA_RATIO + TN - 1) / TN) |
| 59 | #define COOP_MAT_P_M COOP_MAT_Q_PER_TOKEN |
| 60 | #define COOP_MAT_P_N (MATP_REDUCE / TM) |
| 61 | #define SLM_PV_SIZE (MATP_REDUCE * COOP_MAT_P_M * TN) |
| 62 | #define SLM_MASK_SIZE (N_TOK * MATP_REDUCE) |
| 63 | #define SLM_POOL_SIZE_K (MATP_PER_LOOP * HEAD_DIM) |
| 64 | #define K_LOAD_PER_LOOP (GROUPSIZE * 4) |
| 65 | #define HEAD_DIM_VEC4 (HEAD_DIM / 4) |
| 66 | #define SLM_CHUNK_SIZE (TK / 4) |
| 67 | #define K_LOAD_LOOPS ((SLM_POOL_SIZE_K + K_LOAD_PER_LOOP - 1) / K_LOAD_PER_LOOP) |
| 68 | #define O_COUNT ((GQA_RATIO + SUBGROUP_COUNT - 1) / SUBGROUP_COUNT) |
| 69 | |
| 70 | shared slm_pool_block { |
| 71 | float slm_pool_pv[SLM_PV_SIZE + SLM_MASK_SIZE]; |
| 72 | } slm_pool_f32; |
| 73 | |
| 74 | shared slm_pool_alias_block { |
| 75 | float16_t slm_pool_k[SLM_POOL_SIZE_K]; |
| 76 | } slm_pool_f16; |
| 77 | |
| 78 | void main() { |
| 79 | const uint lane = gl_SubgroupInvocationID; |
| 80 | const uint kHeadIdx = gl_WorkGroupID.x % p.kvHead; |
| 81 | const uint outGroupIdx = gl_WorkGroupID.x / p.kvHead; |
| 82 | const uint v = gl_WorkGroupID.y; |
| 83 | const uint d = gl_WorkGroupID.z; |
| 84 | const uint localLinearId = gl_SubgroupID; |
| 85 | const uint wgLane = localLinearId * WARPSIZE + lane; |
| 86 | const uint qDim = p.qHead * HEAD_DIM; |
| 87 | const uint kvDim = p.kvStride1; |
| 88 | const uint maskDim = p.kvSeqLen; |
| 89 | const uint maxDim = (p.kvSeqLen + MATP_REDUCE - 1) / MATP_REDUCE; |
| 90 | const uint pDim = maxDim * MATP_REDUCE; |
| 91 | const uint tokFlatIdx = localLinearId + outGroupIdx * N_TOK; |
| 92 | uint offsetBaseQ = min(tokFlatIdx, p.activationLength - 1) * qDim; |
| 93 | offsetBaseQ = offsetBaseQ + d * p.batchStrideQ + kHeadIdx * HEAD_DIM * GQA_RATIO; |
| 94 | const uint offsetBaseK = (d * p.batchStrideK + (v * MATP_REDUCE) * kvDim + kHeadIdx * p.kvStride2) / 4; |
| 95 | uint offsetOut = d * p.qHead * p.activationLength * pDim + v * MATP_REDUCE + kHeadIdx * GQA_RATIO * pDim + (localLinearId * O_COUNT + outGroupIdx * N_TOK * p.qHead) * pDim + lane; |
| 96 | uint offsetMax = d * p.qHead * p.activationLength * maxDim + v + kHeadIdx * GQA_RATIO * maxDim + (localLinearId * O_COUNT + outGroupIdx * N_TOK * p.qHead) * maxDim; |
| 97 | const uint offsetSlmLoadPv = (localLinearId * O_COUNT * MATP_REDUCE + lane); |
| 98 | const uint offsetBaseM = v * MATP_REDUCE + lane; |
| 99 | const float fp32Min = uintBitsToFloat(0xFEFFFFFF); |
| 100 | |
| 101 | const uint loopCount = HEAD_DIM / TK; |
| 102 | float maskFp32[MATP_REDUCE / WARPSIZE]; |
| 103 | |
| 104 | if (tokFlatIdx < p.activationLength) { |
| 105 | [[unroll]] for (uint mk = 0; mk < MATP_REDUCE / WARPSIZE; mk++) { |
| 106 | const uint maskOffset = mk * WARPSIZE + offsetBaseM; |
| 107 | if (maskOffset < maskDim) { |
| 108 | maskFp32[mk] = float(mState_f16[d * p.batchStrideM + tokFlatIdx * maskDim + maskOffset]); |
| 109 | } else { |
| 110 | maskFp32[mk] = fp32Min; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | coopmat<float, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator> matP[COOP_MAT_P_M][COOP_MAT_P_N]; |
| 116 | |
| 117 | [[unroll]] for (uint mp = 0; mp < COOP_MAT_P_M; mp++) { |
| 118 | [[unroll]] for (uint np = 0; np < COOP_MAT_P_N; np++) { |
| 119 | matP[mp][np] = coopmat<float, gl_ScopeSubgroup, TM, TN, gl_MatrixUseAccumulator>(0.0f); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | [[unroll]] for (uint kLoad = 0; kLoad < K_LOAD_LOOPS; kLoad++) { |
| 124 | const uint flatOffset = kLoad * GROUPSIZE + wgLane; |
| 125 | const uint kRowIdx = flatOffset / HEAD_DIM_VEC4; |
| 126 | const uint kColIdx = flatOffset % HEAD_DIM_VEC4; |
| 127 | const uint slmChunkCol = kColIdx % SLM_CHUNK_SIZE; |
| 128 | const uint slmChunkRow = kColIdx / SLM_CHUNK_SIZE; |
| 129 | const uint offsetK = offsetBaseK + kRowIdx * kvDim / 4 + kColIdx; |
| 130 | const uint offsetSlmK = kRowIdx * TK + slmChunkRow * TK * MATP_PER_LOOP + slmChunkCol * 4; |
| 131 | slm_pool_f16.slm_pool_k[offsetSlmK + 0] = kStateVec4[offsetK].x; |
| 132 | slm_pool_f16.slm_pool_k[offsetSlmK + 1] = kStateVec4[offsetK].y; |
| 133 | slm_pool_f16.slm_pool_k[offsetSlmK + 2] = kStateVec4[offsetK].z; |
| 134 | slm_pool_f16.slm_pool_k[offsetSlmK + 3] = kStateVec4[offsetK].w; |
| 135 | } |
| 136 | |
| 137 | [[unroll]] for (uint pLoop = 0; pLoop < P_LOOP_COUNT; pLoop++) { |
| 138 | f16vec4 kTemp[K_LOAD_LOOPS]; |
| 139 | |
| 140 | if (pLoop + 1 < P_LOOP_COUNT) { |
| 141 | [[unroll]] for (uint kLoad = 0; kLoad < K_LOAD_LOOPS; kLoad++) { |
| 142 | const uint flatOffset = kLoad * GROUPSIZE + wgLane; |
| 143 | const uint kRowIdx = flatOffset / HEAD_DIM_VEC4 + (pLoop + 1) * MATP_PER_LOOP; |
| 144 | const uint kColIdx = flatOffset % HEAD_DIM_VEC4; |
| 145 | const uint offsetK = offsetBaseK + kRowIdx * kvDim / 4 + kColIdx; |
| 146 | kTemp[kLoad] = kStateVec4[offsetK]; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | barrier(); |
| 151 | if (localLinearId < N_TOK) { |
| 152 | [[unroll]] for (uint loop = 0; loop < loopCount; loop++) { |
| 153 | coopmat<float16_t, gl_ScopeSubgroup, TK, TN, gl_MatrixUseB> matQ[COOP_MAT_P_M]; |
| 154 | coopmat<float16_t, gl_ScopeSubgroup, TM, TK, gl_MatrixUseA> matK[COOP_MAT_P_PER_LOOP]; |
| 155 | |
| 156 | [[unroll]] for (uint mq = 0; mq < COOP_MAT_P_M; mq++) { |
| 157 | coopMatLoad( |
| 158 | matQ[mq], |
| 159 | qState, |
| 160 | offsetBaseQ + mq * TN * HEAD_DIM + loop * TK, |
| 161 | HEAD_DIM, |
| 162 | gl_CooperativeMatrixLayoutColumnMajor); |
| 163 | } |
| 164 | |
| 165 | [[unroll]] for (uint np = 0; np < COOP_MAT_P_PER_LOOP; np++) { |
| 166 | coopMatLoad( |
| 167 | matK[np], |
| 168 | slm_pool_f16.slm_pool_k, |
| 169 | loop * TK * MATP_PER_LOOP + np * TM * TK, |
| 170 | TK, |
| 171 | gl_CooperativeMatrixLayoutRowMajor); |
| 172 | } |
| 173 | |
| 174 | [[unroll]] for (uint mp = 0; mp < COOP_MAT_P_M; mp++) { |
| 175 | [[unroll]] for (uint np = 0; np < COOP_MAT_P_PER_LOOP; np++) { |
| 176 | matP[mp][pLoop * COOP_MAT_P_PER_LOOP + np] = coopMatMulAdd(matK[np], matQ[mp], matP[mp][pLoop * COOP_MAT_P_PER_LOOP + np]); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | barrier(); |
| 183 | |
| 184 | if (pLoop + 1 < P_LOOP_COUNT) { |
| 185 | [[unroll]] for (uint kLoad = 0; kLoad < K_LOAD_LOOPS; kLoad++) { |
| 186 | const uint flatOffset = kLoad * GROUPSIZE + wgLane; |
| 187 | const uint kRowIdx = flatOffset / HEAD_DIM_VEC4; |
| 188 | const uint kColIdx = flatOffset % HEAD_DIM_VEC4; |
| 189 | const uint slmChunkCol = kColIdx % SLM_CHUNK_SIZE; |
| 190 | const uint slmChunkRow = kColIdx / SLM_CHUNK_SIZE; |
| 191 | const uint offsetSlmK = kRowIdx * TK + slmChunkRow * TK * MATP_PER_LOOP + slmChunkCol * 4; |
| 192 | slm_pool_f16.slm_pool_k[offsetSlmK + 0] = kTemp[kLoad].x; |
| 193 | slm_pool_f16.slm_pool_k[offsetSlmK + 1] = kTemp[kLoad].y; |
| 194 | slm_pool_f16.slm_pool_k[offsetSlmK + 2] = kTemp[kLoad].z; |
| 195 | slm_pool_f16.slm_pool_k[offsetSlmK + 3] = kTemp[kLoad].w; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | barrier(); |
| 201 | |
| 202 | if (tokFlatIdx < p.activationLength) { |
| 203 | [[unroll]] for (uint mk = 0; mk < MATP_REDUCE / WARPSIZE; mk++) { |
| 204 | slm_pool_f32.slm_pool_pv[SLM_PV_SIZE + localLinearId * MATP_REDUCE + mk * WARPSIZE + lane] = maskFp32[mk]; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | [[unroll]] for (uint oLoop = 0; oLoop < N_TOK; oLoop++) { |
| 209 | if (oLoop + outGroupIdx * N_TOK < p.activationLength) { |
| 210 | if (localLinearId == oLoop) { |
| 211 | [[unroll]] for (uint mp = 0; mp < COOP_MAT_P_M; mp++) { |
| 212 | [[unroll]] for (uint np = 0; np < COOP_MAT_P_N; np++) { |
| 213 | coopMatStore(matP[mp][np], slm_pool_f32.slm_pool_pv, mp * MATP_REDUCE * TN + np * TM, MATP_REDUCE, gl_CooperativeMatrixLayoutColumnMajor); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | barrier(); |
| 219 | |
| 220 | [[unroll]] for (uint maskIdx = 0; maskIdx < MATP_REDUCE / WARPSIZE; maskIdx++) { |
| 221 | maskFp32[maskIdx] = slm_pool_f32.slm_pool_pv[SLM_PV_SIZE + oLoop * MATP_REDUCE + maskIdx * WARPSIZE + lane]; |
| 222 | } |
| 223 | |
| 224 | float fp32O[O_COUNT][MATP_REDUCE / WARPSIZE]; |
| 225 | float maxOut[O_COUNT]; |
| 226 | |
| 227 | [[unroll]] for (uint oc = 0; oc < O_COUNT; oc++) { |
| 228 | [[unroll]] for (uint os = 0; os < MATP_REDUCE / WARPSIZE; os++) { |
| 229 | fp32O[oc][os] = slm_pool_f32.slm_pool_pv[offsetSlmLoadPv + os * WARPSIZE + oc * MATP_REDUCE] * p.softMaxScale; |
| 230 | } |
| 231 | |
| 232 | [[unroll]] for (uint os = 0; os < MATP_REDUCE / WARPSIZE; os++) { |
| 233 | fp32O[oc][os] = fp32O[oc][os] + maskFp32[os]; |
| 234 | } |
| 235 | |
| 236 | float maxTemp = fp32Min; |
| 237 | [[unroll]] for (uint os = 0; os < MATP_REDUCE / WARPSIZE; os++) { |
| 238 | maxTemp = max(maxTemp, fp32O[oc][os]); |
| 239 | } |
| 240 | maxOut[oc] = subgroupMax(maxTemp); |
| 241 | [[unroll]] for (uint os = 0; os < MATP_REDUCE / WARPSIZE; os++) { |
| 242 | fp32O[oc][os] = exp(fp32O[oc][os] - maxOut[oc]); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | [[unroll]] for (uint oc = 0; oc < O_COUNT; oc++) { |
| 247 | if (localLinearId * O_COUNT + oc < GQA_RATIO) { |
| 248 | [[unroll]] for (uint os = 0; os < MATP_REDUCE / WARPSIZE; os++) { |
| 249 | matP_f16[offsetOut + oc * pDim + os * WARPSIZE] = float16_t(fp32O[oc][os]); |
| 250 | } |
| 251 | |
| 252 | if (lane == 0) { |
| 253 | out_max_f32[offsetMax + oc * maxDim] = maxOut[oc]; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | offsetOut = offsetOut + p.qHead * pDim; |
| 259 | offsetMax = offsetMax + p.qHead * maxDim; |
| 260 | barrier(); |
| 261 | } |
| 262 | } |
| 263 | } |