interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / src / ggml-vulkan / vulkan-shaders / flash_attn_sparse_compact.comp

125 lines · 4020 bytes Raw Blame History
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_int32 : require
6#ifdef USE_SUBGROUPS
7#extension GL_KHR_shader_subgroup_basic : require
8#extension GL_KHR_shader_subgroup_ballot : require
9#endif
10
11layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
12layout(constant_id = 0) const uint BLOCK_SIZE = 128;
13layout(constant_id = 1) const uint NUM_SUBGROUPS = 1;
14
15layout (binding = 0) readonly buffer M {float16_t data_m[];};
16layout (binding = 1) writeonly buffer I {int32_t data_i[];};
17
18layout (push_constant) uniform parameter {
19 uint KV;
20 uint nem1;
21 uint nem2;
22 uint nbm1;
23 uint nbm2;
24 uint nbm3;
25 uint n_kv_max;
26} p;
27
28#ifdef USE_SUBGROUPS
29shared uint counts_sh[NUM_SUBGROUPS];
30#else
31shared uint scan[BLOCK_SIZE];
32#endif
33
34bool is_selected(const uint m_idx) {
35 const float v = float(data_m[m_idx]);
36 return !isinf(v) && !isnan(v);
37}
38
39// One workgroup per mask row: compact the finite-mask KV positions into a
40// per-row index list of length n_kv_max, -1 padded, in ascending KV order so
41// the downstream attention accumulation is deterministic.
42// The row is split into contiguous segments, one per subgroup (or per thread
43// without subgroups), so it needs a single workgroup scan instead of one per
44// BLOCK_SIZE chunk.
45void main() {
46 const uint i1 = gl_WorkGroupID.x;
47 const uint i2 = gl_WorkGroupID.y;
48 const uint i3 = gl_WorkGroupID.z;
49 const uint tid = gl_LocalInvocationIndex;
50
51 const uint m_base = i3 * p.nbm3 + i2 * p.nbm2 + i1 * p.nbm1;
52 const uint out_base = ((i3 * p.nem2 + i2) * p.nem1 + i1) * p.n_kv_max;
53
54#ifdef USE_SUBGROUPS
55 const uint sg = gl_SubgroupID;
56 const uint lane = gl_SubgroupInvocationID;
57 const uint seg = (p.KV + gl_NumSubgroups - 1) / gl_NumSubgroups;
58 const uint seg_begin = min(sg * seg, p.KV);
59 const uint seg_end = min(seg_begin + seg, p.KV);
60
61 // Lanes read consecutive positions, so each step is one coalesced load.
62 uint count = 0;
63 for (uint k0 = seg_begin; k0 < seg_end; k0 += gl_SubgroupSize) {
64 const uint k = k0 + lane;
65 count += subgroupBallotBitCount(subgroupBallot(k < seg_end && is_selected(m_base + k)));
66 }
67 if (subgroupElect()) {
68 counts_sh[sg] = count;
69 }
70 barrier();
71
72 uint slot = 0;
73 uint total = 0;
74 for (uint s = 0; s < gl_NumSubgroups; ++s) {
75 slot += s < sg ? counts_sh[s] : 0u;
76 total += counts_sh[s];
77 }
78
79 for (uint k0 = seg_begin; k0 < seg_end && slot < p.n_kv_max; k0 += gl_SubgroupSize) {
80 const uint k = k0 + lane;
81 const bool selected = k < seg_end && is_selected(m_base + k);
82 const uvec4 ballot = subgroupBallot(selected);
83 const uint pos = slot + subgroupBallotExclusiveBitCount(ballot);
84 if (selected && pos < p.n_kv_max) {
85 data_i[out_base + pos] = int32_t(k);
86 }
87 slot += subgroupBallotBitCount(ballot);
88 }
89#else
90 const uint run = (p.KV + BLOCK_SIZE - 1) / BLOCK_SIZE;
91 const uint begin = min(tid * run, p.KV);
92 const uint end = min(begin + run, p.KV);
93
94 uint count = 0;
95 for (uint k = begin; k < end; ++k) {
96 count += is_selected(m_base + k) ? 1u : 0u;
97 }
98
99 // Hillis-Steele inclusive prefix sum of the per-thread counts.
100 scan[tid] = count;
101 barrier();
102 for (uint off = 1; off < BLOCK_SIZE; off <<= 1) {
103 uint add = 0;
104 if (tid >= off) {
105 add = scan[tid - off];
106 }
107 barrier();
108 scan[tid] += add;
109 barrier();
110 }
111
112 const uint total = scan[BLOCK_SIZE - 1];
113 uint slot = scan[tid] - count;
114 for (uint k = begin; k < end && slot < p.n_kv_max; ++k) {
115 if (is_selected(m_base + k)) {
116 data_i[out_base + slot] = int32_t(k);
117 ++slot;
118 }
119 }
120#endif
121
122 for (uint s = min(total, p.n_kv_max) + tid; s < p.n_kv_max; s += BLOCK_SIZE) {
123 data_i[out_base + s] = int32_t(-1);
124 }
125}