ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-opencl / kernels / top_k.cl
| 1 | #ifdef cl_khr_subgroups |
| 2 | #pragma OPENCL EXTENSION cl_khr_subgroups : enable |
| 3 | #endif |
| 4 | |
| 5 | uint top_k_key(float value) { |
| 6 | uint bits = as_uint(value); |
| 7 | if ((bits & 0x7fffffffu) == 0) { |
| 8 | bits = 0; |
| 9 | } |
| 10 | return bits & 0x80000000u ? ~bits : bits ^ 0x80000000u; |
| 11 | } |
| 12 | |
| 13 | uint4 top_k_keys(global const char * src, int i, int n, ulong nb0) { |
| 14 | if (nb0 == sizeof(float) && i + 3 < n) { |
| 15 | const float4 v = vload4(0, (global const float *) src + i); |
| 16 | return (uint4) (top_k_key(v.s0), top_k_key(v.s1), top_k_key(v.s2), top_k_key(v.s3)); |
| 17 | } |
| 18 | return (uint4) ( |
| 19 | i < n ? top_k_key(*(global const float *) (src + (ulong) i *nb0)) : 0, |
| 20 | i + 1 < n ? top_k_key(*(global const float *) (src + (ulong) (i + 1)*nb0)) : 0, |
| 21 | i + 2 < n ? top_k_key(*(global const float *) (src + (ulong) (i + 2)*nb0)) : 0, |
| 22 | i + 3 < n ? top_k_key(*(global const float *) (src + (ulong) (i + 3)*nb0)) : 0); |
| 23 | } |
| 24 | |
| 25 | void top_k_store(global int * dst, global uint2 * candidates, global const char * src, |
| 26 | ulong nb0, int indexed, int base, int i, uint pos) { |
| 27 | const int index = indexed ? *(global const int *) (src + (ulong) i*nb0 + sizeof(float)) : base + i; |
| 28 | if (candidates) { |
| 29 | candidates[pos] = (uint2) (as_uint(*(global const float *) (src + (ulong) i*nb0)), index); |
| 30 | } else { |
| 31 | dst[pos] = index; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void top_k_select( |
| 36 | global const char * src, |
| 37 | global int * dst, |
| 38 | global uint2 * candidates, |
| 39 | ulong nb00, |
| 40 | int ne00, |
| 41 | int k, |
| 42 | int base, |
| 43 | int indexed, |
| 44 | local uint * hist |
| 45 | ) { |
| 46 | const int tid = get_local_id(0); |
| 47 | const int nth = get_local_size(0); |
| 48 | if (k == ne00) { |
| 49 | for (int i = tid; i < ne00; i += nth) { |
| 50 | top_k_store(dst, candidates, src, nb00, indexed, base, i, i); |
| 51 | } |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | uint prefix = 0; |
| 56 | uint mask = 0; |
| 57 | uint rank = k; |
| 58 | for (int shift = 24; shift >= 0; shift -= 8) { |
| 59 | for (int i = tid; i < 256; i += nth) { |
| 60 | hist[i] = 0; |
| 61 | } |
| 62 | barrier(CLK_LOCAL_MEM_FENCE); |
| 63 | for (int i = 4*tid; i < ne00; i += 4*nth) { |
| 64 | const uint4 v = top_k_keys(src, i, ne00, nb00); |
| 65 | const uint keys[4] = {v.s0, v.s1, v.s2, v.s3}; |
| 66 | #pragma unroll |
| 67 | for (int j = 0; j < 4; ++j) { |
| 68 | const uint key = keys[j]; |
| 69 | if (i + j < ne00 && (key & mask) == prefix) { |
| 70 | atomic_inc(hist + ((key >> shift) & 255)); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | barrier(CLK_LOCAL_MEM_FENCE); |
| 75 | if (tid == 0) { |
| 76 | uint above = 0; |
| 77 | for (int b = 255; b >= 0; --b) { |
| 78 | uint count = hist[b]; |
| 79 | if (above + count >= rank) { |
| 80 | hist[256] = b; |
| 81 | hist[257] = rank - above; |
| 82 | hist[258] = count; |
| 83 | break; |
| 84 | } |
| 85 | above += count; |
| 86 | } |
| 87 | } |
| 88 | barrier(CLK_LOCAL_MEM_FENCE); |
| 89 | prefix |= hist[256] << shift; |
| 90 | rank = hist[257]; |
| 91 | const bool done = hist[258] == rank; |
| 92 | barrier(CLK_LOCAL_MEM_FENCE); |
| 93 | if (done) { |
| 94 | // The whole bucket is selected, so its low bits do not matter. |
| 95 | break; |
| 96 | } |
| 97 | mask |= 255u << shift; |
| 98 | } |
| 99 | |
| 100 | uint greater = 0; |
| 101 | uint equal = 0; |
| 102 | for (int i = 4*tid; i < ne00; i += 4*nth) { |
| 103 | const uint4 v = top_k_keys(src, i, ne00, nb00); |
| 104 | const uint keys[4] = {v.s0, v.s1, v.s2, v.s3}; |
| 105 | #pragma unroll |
| 106 | for (int j = 0; j < 4; ++j) { |
| 107 | greater += i + j < ne00 && keys[j] > prefix; |
| 108 | equal += i + j < ne00 && keys[j] == prefix; |
| 109 | } |
| 110 | } |
| 111 | uint pos_greater; |
| 112 | uint pos_equal; |
| 113 | #ifdef cl_khr_subgroups |
| 114 | const uint sg = get_sub_group_id(); |
| 115 | const uint nsg = get_num_sub_groups(); |
| 116 | const uint total_greater = sub_group_reduce_add(greater); |
| 117 | const uint total_equal = sub_group_reduce_add(equal); |
| 118 | pos_greater = sub_group_scan_exclusive_add(greater); |
| 119 | pos_equal = sub_group_scan_exclusive_add(equal); |
| 120 | if (get_sub_group_local_id() == 0) { |
| 121 | hist[sg] = total_greater; |
| 122 | hist[nsg + sg] = total_equal; |
| 123 | } |
| 124 | barrier(CLK_LOCAL_MEM_FENCE); |
| 125 | for (uint i = 0; i < nsg; ++i) { |
| 126 | pos_equal += hist[i]; |
| 127 | if (i < sg) { |
| 128 | pos_greater += hist[i]; |
| 129 | pos_equal += hist[nsg + i]; |
| 130 | } |
| 131 | } |
| 132 | #else |
| 133 | hist[tid] = greater; |
| 134 | hist[nth + tid] = equal; |
| 135 | barrier(CLK_LOCAL_MEM_FENCE); |
| 136 | pos_greater = 0; |
| 137 | pos_equal = 0; |
| 138 | for (int i = 0; i < nth; ++i) { |
| 139 | pos_equal += hist[i]; |
| 140 | if (i < tid) { |
| 141 | pos_greater += hist[i]; |
| 142 | pos_equal += hist[nth + i]; |
| 143 | } |
| 144 | } |
| 145 | #endif |
| 146 | for (int i = 4*tid; i < ne00; i += 4*nth) { |
| 147 | const uint4 v = top_k_keys(src, i, ne00, nb00); |
| 148 | const uint keys[4] = {v.s0, v.s1, v.s2, v.s3}; |
| 149 | #pragma unroll |
| 150 | for (int j = 0; j < 4; ++j) { |
| 151 | if (i + j < ne00) { |
| 152 | if (keys[j] > prefix) { |
| 153 | top_k_store(dst, candidates, src, nb00, indexed, base, i + j, pos_greater++); |
| 154 | } else if (keys[j] == prefix && pos_equal < k) { |
| 155 | top_k_store(dst, candidates, src, nb00, indexed, base, i + j, pos_equal++); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | kernel void kernel_top_k_f32( |
| 163 | global const char * src, |
| 164 | ulong offset0, |
| 165 | global int * dst, |
| 166 | ulong offsetd, |
| 167 | int ne00, |
| 168 | int ne01, |
| 169 | int ne02, |
| 170 | ulong nb00, |
| 171 | ulong nb01, |
| 172 | ulong nb02, |
| 173 | ulong nb03, |
| 174 | int k_max, |
| 175 | local uint * hist, |
| 176 | global uint2 * candidates, |
| 177 | int tile_width |
| 178 | ) { |
| 179 | const ulong row = get_group_id(1); |
| 180 | const int part = get_group_id(0); |
| 181 | const int parts = get_num_groups(0); |
| 182 | const int base = part*tile_width; |
| 183 | const int n = min(ne00 - base, tile_width); |
| 184 | const int k = min(k_max, n); |
| 185 | const int out_width = (parts - 1)*k_max + min(k_max, ne00 - (parts - 1)*tile_width); |
| 186 | src += offset0 + (row % ne01)*nb01 + (row / ne01 % ne02)*nb02 + (row / ne01 / ne02)*nb03 + (ulong) base*nb00; |
| 187 | if (candidates) { |
| 188 | candidates += row*out_width + part*k_max; |
| 189 | } else { |
| 190 | dst = (global int *) ((global char *) dst + offsetd) + row*out_width + part*k_max; |
| 191 | } |
| 192 | top_k_select(src, dst, candidates, nb00, n, k, base, 0, hist); |
| 193 | } |
| 194 | |
| 195 | kernel void kernel_top_k_merge_f32( |
| 196 | global const uint2 * candidates, |
| 197 | global int * dst, |
| 198 | ulong offsetd, |
| 199 | int n_candidates, |
| 200 | int k, |
| 201 | local uint * hist |
| 202 | ) { |
| 203 | const ulong row = get_group_id(1); |
| 204 | candidates += row*n_candidates; |
| 205 | dst = (global int *) ((global char *) dst + offsetd) + row*k; |
| 206 | top_k_select((global const char *) candidates, dst, 0, sizeof(uint2), n_candidates, k, 0, 1, hist); |
| 207 | } |