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 / argsort_large.comp
| 1 | #version 450 |
| 2 | #extension GL_EXT_control_flow_attributes : enable |
| 3 | #extension GL_KHR_memory_scope_semantics : enable |
| 4 | #pragma use_vulkan_memory_model |
| 5 | |
| 6 | #include "types.glsl" |
| 7 | |
| 8 | layout(constant_id = 0) const int BLOCK_SIZE = 1024; |
| 9 | layout(constant_id = 1) const int WG_UNROLL_FACTOR = 2; |
| 10 | #define ASC 0 |
| 11 | |
| 12 | layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in; |
| 13 | |
| 14 | layout (binding = 0) readonly buffer A {A_TYPE data_a[];}; |
| 15 | layout (binding = 1) workgroupcoherent buffer B {ivec2 tmp_idx[];}; |
| 16 | layout (binding = 2) workgroupcoherent buffer D {int data_d[];}; |
| 17 | |
| 18 | layout (push_constant) uniform parameter { |
| 19 | uint ncols; |
| 20 | uint ncols_padded; |
| 21 | uint ncols_padded_log2; |
| 22 | uint nrows; |
| 23 | uint order; |
| 24 | uint outer_start; |
| 25 | uint outer_end; |
| 26 | uint inner_start; |
| 27 | uint inner_end; |
| 28 | } p; |
| 29 | |
| 30 | shared int s; |
| 31 | |
| 32 | void argsort(bool needs_bounds_check, const uint row) { |
| 33 | // bitonic sort |
| 34 | int col = int(gl_GlobalInvocationID.x); |
| 35 | col = (col % BLOCK_SIZE) + (col / BLOCK_SIZE) * BLOCK_SIZE * WG_UNROLL_FACTOR; |
| 36 | |
| 37 | const uint row_offset = row * p.ncols; |
| 38 | uint idx_offset = row * p.ncols_padded; |
| 39 | |
| 40 | // workaround for NV driver/compiler bug - dummy use of shared memory |
| 41 | if (gl_LocalInvocationIndex == 0) { |
| 42 | s = 0; |
| 43 | col += s; |
| 44 | } |
| 45 | |
| 46 | bool need_barrier = false; |
| 47 | |
| 48 | // initialize indices |
| 49 | if (p.outer_start == 0 && p.inner_start == 0) { |
| 50 | [[unroll]] for (int u = 0; u < WG_UNROLL_FACTOR; ++u) { |
| 51 | uint c = u*BLOCK_SIZE + col; |
| 52 | if (c < p.ncols_padded) { |
| 53 | ivec2 v = ivec2(c, 0); |
| 54 | if (!needs_bounds_check || c < p.ncols) { |
| 55 | v.y = floatBitsToInt(data_a[row_offset + c]); |
| 56 | } |
| 57 | tmp_idx[idx_offset + c] = v; |
| 58 | } |
| 59 | } |
| 60 | need_barrier = true; |
| 61 | } |
| 62 | |
| 63 | [[unroll]] for (uint outer_idx = p.outer_start, k = (2 << outer_idx); outer_idx < p.outer_end; k *= 2, outer_idx++) { |
| 64 | uint inner_end = min(p.inner_end, outer_idx + 1); |
| 65 | for (uint j = k >> (p.inner_start + 1), inner_idx = p.inner_start; inner_idx < inner_end; j /= 2, inner_idx++) { |
| 66 | if (need_barrier) { |
| 67 | controlBarrier(gl_ScopeWorkgroup, gl_ScopeWorkgroup, gl_StorageSemanticsBuffer, gl_SemanticsAcquireRelease); |
| 68 | } |
| 69 | need_barrier = true; |
| 70 | [[unroll]] for (int u = 0; u < WG_UNROLL_FACTOR; ++u) { |
| 71 | int c = u*BLOCK_SIZE + col; |
| 72 | const int ixj = int(c ^ j); |
| 73 | |
| 74 | if (ixj < c) { |
| 75 | continue; |
| 76 | } |
| 77 | |
| 78 | int idx_0 = (c & k) == 0 ? c : ixj; |
| 79 | int idx_1 = (c & k) == 0 ? ixj : c; |
| 80 | |
| 81 | ivec2 sh_idx_0 = tmp_idx[idx_offset + idx_0]; |
| 82 | ivec2 sh_idx_1 = tmp_idx[idx_offset + idx_1]; |
| 83 | bool idx_0_oob = needs_bounds_check ? sh_idx_0.x >= p.ncols : false; |
| 84 | bool idx_1_oob = needs_bounds_check ? sh_idx_1.x >= p.ncols : false; |
| 85 | |
| 86 | if ((idx_0_oob || |
| 87 | (!idx_1_oob && intBitsToFloat(sh_idx_0.y) > intBitsToFloat(sh_idx_1.y)))) { |
| 88 | tmp_idx[idx_offset + idx_0] = sh_idx_1; |
| 89 | tmp_idx[idx_offset + idx_1] = sh_idx_0; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (p.outer_end == p.ncols_padded_log2 && |
| 96 | p.inner_end >= p.ncols_padded_log2 + 1) { |
| 97 | controlBarrier(gl_ScopeWorkgroup, gl_ScopeWorkgroup, gl_StorageSemanticsBuffer, gl_SemanticsAcquireRelease); |
| 98 | [[unroll]] for (int u = 0; u < WG_UNROLL_FACTOR; ++u) { |
| 99 | uint c = u*BLOCK_SIZE + col; |
| 100 | if (c < p.ncols) { |
| 101 | if (p.order == ASC) { |
| 102 | data_d[row_offset + c] = tmp_idx[idx_offset + c].x; |
| 103 | } else { |
| 104 | data_d[row_offset + p.ncols - c - 1] = tmp_idx[idx_offset + c].x; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void main() { |
| 112 | if (p.ncols == p.ncols_padded) { |
| 113 | uint row = gl_WorkGroupID.y; |
| 114 | while (row < p.nrows) { |
| 115 | argsort(false, row); |
| 116 | row += gl_WorkGroupSize.y * gl_NumWorkGroups.y; |
| 117 | } |
| 118 | } else { |
| 119 | uint row = gl_WorkGroupID.y; |
| 120 | while (row < p.nrows) { |
| 121 | argsort(true, row); |
| 122 | row += gl_WorkGroupSize.y * gl_NumWorkGroups.y; |
| 123 | } |
| 124 | } |
| 125 | } |