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_mask_opt.comp

162 lines · 5540 bytes Raw Blame History
1#version 450
2
3#extension GL_EXT_control_flow_attributes : enable
4#extension GL_EXT_shader_16bit_storage : enable
5#extension GL_KHR_shader_subgroup_arithmetic : enable
6
7layout (constant_id = 0) const uint BLOCK_SIZE = 128;
8layout (constant_id = 1) const uint NUM_SUBGROUPS = 4;
9layout (constant_id = 2) const uint Br = 32;
10layout (constant_id = 3) const uint Bc = 32;
11
12layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
13
14layout (binding = 0) readonly buffer A {float16_t data_a[];};
15layout (binding = 0) readonly buffer Av4 {f16vec4 data_av4[];};
16layout (binding = 1) writeonly buffer D {uint data_d[];};
17
18layout (push_constant) uniform parameter {
19 uint nem0;
20 uint nem1;
21 uint nem2;
22 uint nbm1;
23 uint nbm2;
24 uint nbm3;
25 uint nbd1;
26 uint nbd2;
27 uint nbd3;
28};
29
30#define MASK_OPT_ALL_NEG_INF 1
31#define MASK_OPT_ALL_ZERO 2
32
33shared float minsh[NUM_SUBGROUPS];
34shared float maxsh[NUM_SUBGROUPS];
35
36float FLT_MAX_OVER_2 = uintBitsToFloat(0x7EFFFFFF);
37
38void loadvec4(inout uint result, const uint i0, const uint i1, const uint i2, const uint i3, const bool need_bounds_check) {
39 const uint tid = gl_LocalInvocationIndex;
40
41 [[unroll]] for (uint block_x = 0; block_x < 16; ++block_x) {
42 float min_v = FLT_MAX_OVER_2;
43 float max_v = -FLT_MAX_OVER_2;
44 [[unroll]] for (uint i = 0; i < Br * Bc / 4; i += BLOCK_SIZE) {
45 uint j0 = (i + tid) % (Bc / 4);
46 uint j1 = (i + tid) / (Bc / 4);
47
48 j0 *= 4;
49 j0 += (i0 * 16 + block_x) * Bc;
50 j1 += i1 * Br;
51
52 if (!need_bounds_check || j0 + 3 < nem0) {
53 vec4 f = vec4(data_av4[(j0 + j1 * nbm1 + i2 * nbm2 + i3 * nbm3) / 4]);
54 [[unroll]] for (int c = 0; c < 4; ++c) {
55 min_v = min(min_v, f[c]);
56 max_v = max(max_v, f[c]);
57 }
58 } else {
59 [[unroll]] for (int c = 0; c < 4; ++c) {
60 if (j0 + c < nem0) {
61 float f = float(data_a[j0 + j1 * nbm1 + i2 * nbm2 + i3 * nbm3]);
62 min_v = min(min_v, f);
63 max_v = max(max_v, f);
64 }
65 }
66 }
67 }
68 min_v = subgroupMin(min_v);
69 max_v = subgroupMax(max_v);
70 if (gl_SubgroupInvocationID == 0) {
71 minsh[gl_SubgroupID] = min_v;
72 maxsh[gl_SubgroupID] = max_v;
73 }
74 barrier();
75 if (tid == 0) {
76 [[unroll]] for (uint i = 0; i < NUM_SUBGROUPS; ++i) {
77 min_v = min(min_v, minsh[i]);
78 max_v = max(max_v, maxsh[i]);
79 }
80 if (max_v <= -FLT_MAX_OVER_2) {
81 result |= 1 << (2*block_x);
82 }
83 if (min_v == 0.0f && max_v == 0.0f) {
84 result |= 2 << (2*block_x);
85 }
86 }
87 barrier();
88 }
89}
90
91// For each Br x Bc block of the mask (input) buffer, read all values and check
92// if it's all -inf or all zero. Write out a two-bit code indicating which it is
93// (or zero for neither). Each workgroup processes 16 tiles and writes out a
94// 32-bit result mask.
95//
96// TODO: This is a lot of work per workgroup, might make sense to split this into
97// more workgroups in the future.
98void main() {
99 // Each workgroup handles a row
100 const uint tid = gl_LocalInvocationIndex;
101 const uint i0 = gl_WorkGroupID.x;
102 const uint i1 = gl_WorkGroupID.y;
103 const uint i2 = gl_WorkGroupID.z % nem2;
104 const uint i3 = gl_WorkGroupID.z / nem2;
105
106 uint result = 0;
107
108 // Fast path for fully in-bounds blocks where we can do f16vec4 loads
109 if ((nem0 % Bc) == 0 && (nem1 % Br) == 0 &&
110 ((Br * Bc) % (BLOCK_SIZE * 4)) == 0) {
111 if ((i0 + 1) * 16 * Bc <= nem0) {
112 loadvec4(result, i0, i1, i2, i3, false);
113 } else {
114 loadvec4(result, i0, i1, i2, i3, true);
115 }
116 } else {
117 [[unroll]] for (uint block_x = 0; block_x < 16; ++block_x) {
118 float min_v = FLT_MAX_OVER_2;
119 float max_v = -FLT_MAX_OVER_2;
120 [[unroll]] for (uint i = 0; i < Br * Bc; i += BLOCK_SIZE) {
121 if ((Br * Bc % BLOCK_SIZE) != 0 && i + tid >= Br * Bc) {
122 continue;
123 }
124 uint j0 = (i + tid) % Bc;
125 uint j1 = (i + tid) / Bc;
126
127 j0 += (i0 * 16 + block_x) * Bc;
128 j1 += i1 * Br;
129
130 if (j0 < nem0 && j1 < nem1) {
131 float f = float(data_a[j0 + j1 * nbm1 + i2 * nbm2 + i3 * nbm3]);
132 min_v = min(min_v, f);
133 max_v = max(max_v, f);
134 }
135 }
136 min_v = subgroupMin(min_v);
137 max_v = subgroupMax(max_v);
138 if (gl_SubgroupInvocationID == 0) {
139 minsh[gl_SubgroupID] = min_v;
140 maxsh[gl_SubgroupID] = max_v;
141 }
142 barrier();
143 if (tid == 0) {
144 [[unroll]] for (uint i = 0; i < NUM_SUBGROUPS; ++i) {
145 min_v = min(min_v, minsh[i]);
146 max_v = max(max_v, maxsh[i]);
147 }
148 if (max_v <= -FLT_MAX_OVER_2) {
149 result |= 1 << (2*block_x);
150 }
151 if (min_v == 0.0f && max_v == 0.0f) {
152 result |= 2 << (2*block_x);
153 }
154 }
155 barrier();
156 }
157 }
158
159 if (tid == 0) {
160 data_d[i0 + i1 * nbd1 + i2 * nbd2 + i3 * nbd3] = result;
161 }
162}