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 / topk_nary_search.comp
| 1 | #version 450 |
| 2 | #extension GL_EXT_control_flow_attributes : enable |
| 3 | #extension GL_EXT_debug_printf : enable |
| 4 | #extension GL_KHR_shader_subgroup_basic : enable |
| 5 | #extension GL_KHR_shader_subgroup_ballot : enable |
| 6 | #extension GL_KHR_shader_subgroup_arithmetic : enable |
| 7 | #extension GL_KHR_shader_subgroup_shuffle : enable |
| 8 | |
| 9 | #include "types.glsl" |
| 10 | |
| 11 | layout(constant_id = 0) const int BLOCK_SIZE = 1024; |
| 12 | layout(constant_id = 1) const int SUBGROUP_SIZE = 32; |
| 13 | layout(constant_id = 2) const int SUBGROUP_SIZE_LOG2 = 5; |
| 14 | |
| 15 | layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 16 | |
| 17 | // Input can either be the source (A) or intermediate values (S). |
| 18 | // Similarly, output can be either destination (D) or intermediate values (S). |
| 19 | layout (binding = 0) readonly buffer A {A_TYPE data_a[];}; |
| 20 | layout (binding = 0) readonly buffer S {ivec2 data_s[];}; |
| 21 | layout (binding = 1) writeonly buffer D {int data_d[];}; |
| 22 | layout (binding = 1) writeonly buffer T {ivec2 data_t[];}; |
| 23 | |
| 24 | layout (push_constant) uniform parameter { |
| 25 | uint orig_ncols; |
| 26 | uint ncols_input; |
| 27 | uint ncols_output; |
| 28 | uint k; |
| 29 | uint nrows; |
| 30 | uint first_pass; |
| 31 | uint last_pass; |
| 32 | } p; |
| 33 | |
| 34 | // pairs of (gid, value) |
| 35 | shared ivec2 dst_row[BLOCK_SIZE]; |
| 36 | |
| 37 | shared int counts[SUBGROUP_SIZE]; |
| 38 | shared int sh_min_idx; |
| 39 | shared uint sh_total; |
| 40 | shared uint offset_partials[BLOCK_SIZE / SUBGROUP_SIZE]; |
| 41 | shared uint eq_min_partials[BLOCK_SIZE / SUBGROUP_SIZE]; |
| 42 | |
| 43 | // Map float values to uint such that comparisons still work. |
| 44 | // Positive values set the high bit, negative values are inverted. |
| 45 | // +0.0 -> 0x80000000, -0.0 -> 0x7FFFFFFF are in the correct places. |
| 46 | uint f2ui(float x) { |
| 47 | uint y = floatBitsToUint(x); |
| 48 | if ((y & 0x80000000) != 0) { |
| 49 | y ^= ~0; |
| 50 | } else { |
| 51 | y |= 0x80000000; |
| 52 | } |
| 53 | return y; |
| 54 | } |
| 55 | |
| 56 | void topk(const uint row) { |
| 57 | const int tid = int(gl_LocalInvocationID.x); |
| 58 | |
| 59 | // initialize indices |
| 60 | if (gl_GlobalInvocationID.x < p.ncols_input) { |
| 61 | if (p.first_pass != 0) { |
| 62 | const uint row_offset = row * p.ncols_input; |
| 63 | // NaN ranks lowest, like -inf, so that every value has a place in |
| 64 | // the ordering the search below counts |
| 65 | float a = float(data_a[row_offset + gl_GlobalInvocationID.x]); |
| 66 | if (isnan(a)) { |
| 67 | a = uintBitsToFloat(0xFF800000); |
| 68 | } |
| 69 | dst_row[tid] = ivec2(gl_GlobalInvocationID.x, floatBitsToInt(a)); |
| 70 | } else { |
| 71 | const uint row_offset = row * p.ncols_input; |
| 72 | dst_row[tid] = data_s[row_offset + gl_GlobalInvocationID.x]; |
| 73 | } |
| 74 | } else { |
| 75 | dst_row[tid] = ivec2(p.orig_ncols, 0xFF800000); // -inf |
| 76 | } |
| 77 | barrier(); |
| 78 | |
| 79 | if (p.k == 1) { |
| 80 | // Fast path for single output - just do a max reduction |
| 81 | [[unroll]] for (int s = BLOCK_SIZE / 2; s >= 1; s /= 2) { |
| 82 | if (tid < s) { |
| 83 | ivec2 a = dst_row[tid]; |
| 84 | ivec2 b = dst_row[tid + s]; |
| 85 | // compare as floats: the bit patterns of negative values |
| 86 | // order the other way as integers |
| 87 | if (a.x >= p.orig_ncols || |
| 88 | b.x < p.orig_ncols && intBitsToFloat(b.y) > intBitsToFloat(a.y)) { |
| 89 | dst_row[tid] = b; |
| 90 | } |
| 91 | } |
| 92 | barrier(); |
| 93 | } |
| 94 | } else { |
| 95 | // Do an N-ary search to find the K-th largest value. |
| 96 | // We remap the float values to be comparable as unsigned integers, |
| 97 | // and split the range into 2^N smaller ranges where N is the |
| 98 | // subgroup size. Count how many values are in each range, if the K-th |
| 99 | // largest value is in the middle of one of thee ranges then repeat |
| 100 | // and split again. |
| 101 | |
| 102 | // Mask is the current set of bits we're searching. Shift is the LSB index. |
| 103 | int shift = 32 - SUBGROUP_SIZE_LOG2; |
| 104 | uint mask = ((1 << SUBGROUP_SIZE_LOG2) - 1) << shift; |
| 105 | |
| 106 | // The current range, [range_min, range_max). It starts as every value |
| 107 | // (+inf maps to 0xFF800000 and NaN was replaced by -inf), so the |
| 108 | // buckets always hold at least limit values. |
| 109 | uint range_min = 0; |
| 110 | uint range_max = 0xFFFFFFFF; |
| 111 | // How many are above the current range, and how many we need to find. |
| 112 | uint total = 0; |
| 113 | uint limit = min(p.k, p.ncols_input - gl_WorkGroupID.x * BLOCK_SIZE); |
| 114 | |
| 115 | while (mask != 0) { |
| 116 | barrier(); |
| 117 | // Initialize bucket counts to zero. |
| 118 | if (tid < SUBGROUP_SIZE) { |
| 119 | counts[tid] = 0; |
| 120 | } |
| 121 | barrier(); |
| 122 | // Count how many values are in each bucket. |
| 123 | if (tid < p.ncols_input) { |
| 124 | float y = intBitsToFloat(dst_row[tid].y); |
| 125 | uint fy = f2ui(y); |
| 126 | if (fy >= range_min && fy < range_max) { |
| 127 | uint bucket = (fy & mask) >> shift; |
| 128 | atomicAdd(counts[bucket], 1); |
| 129 | } |
| 130 | } |
| 131 | barrier(); |
| 132 | |
| 133 | // On the first subgroup, do a scan to count (from the top down) how |
| 134 | // many elements are in the top N buckets. Find the index of the first |
| 135 | // that is over the limit. Copy it to the other invocations through |
| 136 | // shared memory. |
| 137 | if (tid < SUBGROUP_SIZE) { |
| 138 | uint partial_sum = counts[SUBGROUP_SIZE - 1 - tid]; |
| 139 | partial_sum = subgroupInclusiveAdd(partial_sum) + total; |
| 140 | uint t = subgroupBallotFindLSB(subgroupBallot(partial_sum >= limit)); |
| 141 | if (tid == t) { |
| 142 | sh_min_idx = int(SUBGROUP_SIZE - 1 - t); |
| 143 | sh_total = partial_sum; |
| 144 | } |
| 145 | } |
| 146 | barrier(); |
| 147 | int min_idx = sh_min_idx; |
| 148 | total = sh_total; |
| 149 | |
| 150 | // Update the range, and break if we've found the K-th largest. |
| 151 | // The end of the top bucket wraps past 2^32, clamp it instead. |
| 152 | range_min = range_min + (uint(min_idx) << shift); |
| 153 | range_max = range_min + (1u << shift); |
| 154 | if (range_max < range_min) { |
| 155 | range_max = 0xFFFFFFFF; |
| 156 | } |
| 157 | |
| 158 | if (total == p.k) { |
| 159 | break; |
| 160 | } |
| 161 | total -= counts[min_idx]; |
| 162 | mask >>= SUBGROUP_SIZE_LOG2; |
| 163 | shift -= SUBGROUP_SIZE_LOG2; |
| 164 | if (shift < 0) { |
| 165 | shift = 0; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | ivec2 v = dst_row[tid]; |
| 170 | |
| 171 | // We need to compact these values to the start of the dst_row array. |
| 172 | // Have each subgroup count how many items it'll store, so other |
| 173 | // subgroups can compute their base offset. |
| 174 | // Values strictly greater than range_min must be stored. For values equal |
| 175 | // to range_min, there can be ties and it's possible we'll need to store |
| 176 | // an arbitrary subset of them. |
| 177 | // If total == p.k, have a fast path where we don't need to handle ties. |
| 178 | if (total == p.k) { |
| 179 | bool top = f2ui(intBitsToFloat(v.y)) >= range_min; |
| 180 | uvec4 b = subgroupBallot(top); |
| 181 | uint bit_count = subgroupBallotBitCount(b); |
| 182 | if ((tid % SUBGROUP_SIZE) == 0) { |
| 183 | offset_partials[tid / SUBGROUP_SIZE] = bit_count; |
| 184 | } |
| 185 | barrier(); |
| 186 | |
| 187 | uint out_idx = 0; |
| 188 | [[unroll]] for (int i = 0; i < BLOCK_SIZE / SUBGROUP_SIZE; ++i) { |
| 189 | if (i < tid / SUBGROUP_SIZE) { |
| 190 | out_idx += offset_partials[i]; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | uint bit_count_ex = subgroupBallotExclusiveBitCount(b); |
| 195 | if (top) { |
| 196 | // TODO: Copy directly to the output? |
| 197 | dst_row[out_idx + bit_count_ex] = v; |
| 198 | } |
| 199 | } else { |
| 200 | bool top = f2ui(intBitsToFloat(v.y)) > range_min; |
| 201 | bool eq_min = f2ui(intBitsToFloat(v.y)) == range_min; |
| 202 | uvec4 b_top = subgroupBallot(top); |
| 203 | uvec4 b_eq_min = subgroupBallot(eq_min); |
| 204 | uint bit_count_top = subgroupBallotBitCount(b_top); |
| 205 | uint bit_count_eq_min = subgroupBallotBitCount(b_eq_min); |
| 206 | if ((tid % SUBGROUP_SIZE) == 0) { |
| 207 | offset_partials[tid / SUBGROUP_SIZE] = bit_count_top; |
| 208 | eq_min_partials[tid / SUBGROUP_SIZE] = bit_count_eq_min; |
| 209 | } |
| 210 | barrier(); |
| 211 | |
| 212 | uint out_idx = 0; |
| 213 | uint eq_min_base = 0; |
| 214 | uint eq_min_idx = 0; |
| 215 | [[unroll]] for (int i = 0; i < BLOCK_SIZE / SUBGROUP_SIZE; ++i) { |
| 216 | if (i < tid / SUBGROUP_SIZE) { |
| 217 | out_idx += offset_partials[i]; |
| 218 | eq_min_idx += eq_min_partials[i]; |
| 219 | } |
| 220 | eq_min_base += offset_partials[i]; |
| 221 | } |
| 222 | // range_min values are stored at the end |
| 223 | eq_min_idx += eq_min_base; |
| 224 | |
| 225 | uint bit_count_ex_top = subgroupBallotExclusiveBitCount(b_top); |
| 226 | uint bit_count_ex_eq_min = subgroupBallotExclusiveBitCount(b_eq_min); |
| 227 | if (top) { |
| 228 | // TODO: Copy directly to the output? |
| 229 | dst_row[out_idx + bit_count_ex_top] = v; |
| 230 | } |
| 231 | if (eq_min && eq_min_idx + bit_count_ex_eq_min < p.k) { |
| 232 | dst_row[eq_min_idx + bit_count_ex_eq_min] = v; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | barrier(); |
| 237 | } |
| 238 | |
| 239 | if (tid < p.k) { |
| 240 | if (p.last_pass != 0) { |
| 241 | if (gl_GlobalInvocationID.x < p.ncols_input) { |
| 242 | const uint row_offset = row * p.k; |
| 243 | data_d[row_offset + tid] = dst_row[tid].x; |
| 244 | } |
| 245 | } else { |
| 246 | if (gl_WorkGroupID.x * p.k + tid < p.ncols_output) { |
| 247 | const uint row_offset = row * p.ncols_output + gl_WorkGroupID.x * p.k; |
| 248 | data_t[row_offset + tid] = dst_row[tid]; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void main() { |
| 255 | uint row = gl_WorkGroupID.y; |
| 256 | while (row < p.nrows) { |
| 257 | topk(row); |
| 258 | row += gl_WorkGroupSize.y * gl_NumWorkGroups.y; |
| 259 | } |
| 260 | } |