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 / argsort.comp

92 lines · 2745 bytes Raw Blame History
1#version 450
2#extension GL_EXT_control_flow_attributes : enable
3
4#include "types.glsl"
5
6layout(constant_id = 0) const int BLOCK_SIZE = 1024;
7layout(constant_id = 1) const int NCOLS_PADDED_LOG2 = 10;
8#define ASC 0
9
10layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
11
12layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
13layout (binding = 2) writeonly buffer D {int data_d[];};
14
15layout (push_constant) uniform parameter {
16 uint ncols;
17 uint ncols_padded;
18 uint ncols_padded_log2;
19 uint nrows;
20 uint order;
21 uint outer_start;
22 uint outer_end;
23 uint inner_start;
24 uint inner_end;
25} p;
26
27shared ivec2 dst_row[BLOCK_SIZE];
28
29void argsort(bool needs_bounds_check, const uint row) {
30 // bitonic sort
31 const int col = int(gl_LocalInvocationID.x);
32
33 const uint row_offset = row * p.ncols;
34
35 // initialize indices
36 ivec2 value = ivec2(col, 0);
37 if (!needs_bounds_check || col < p.ncols) {
38 value.y = floatBitsToInt(data_a[row_offset + col]);
39 }
40 dst_row[col] = value;
41 barrier();
42
43 uint num_outer_loop_iters = NCOLS_PADDED_LOG2;
44 [[unroll]] for (uint k = 2, outer_idx = 0; outer_idx < num_outer_loop_iters; k *= 2, outer_idx++) {
45 uint num_inner_loop_iters = outer_idx + 1;
46 [[unroll]] for (uint j = k / 2, inner_idx = 0; inner_idx < num_inner_loop_iters; j /= 2, inner_idx++) {
47 const int ixj = int(col ^ j);
48
49 if (ixj > col) {
50 int idx_0 = (col & k) == 0 ? col : ixj;
51 int idx_1 = (col & k) == 0 ? ixj : col;
52
53 ivec2 sh_idx_0 = dst_row[idx_0];
54 ivec2 sh_idx_1 = dst_row[idx_1];
55 bool idx_0_oob = needs_bounds_check ? sh_idx_0.x >= p.ncols : false;
56 bool idx_1_oob = needs_bounds_check ? sh_idx_1.x >= p.ncols : false;
57
58 if (idx_0_oob ||
59 (!idx_1_oob && intBitsToFloat(sh_idx_0.y) > intBitsToFloat(sh_idx_1.y))) {
60 dst_row[idx_0] = sh_idx_1;
61 dst_row[idx_1] = sh_idx_0;
62 }
63 }
64
65 barrier();
66 }
67 }
68
69 if (col < p.ncols) {
70 if (p.order == ASC) {
71 data_d[row_offset + col] = dst_row[col].x;
72 } else {
73 data_d[row_offset + p.ncols - col - 1] = dst_row[col].x;
74 }
75 }
76}
77
78void main() {
79 if (p.ncols == BLOCK_SIZE) {
80 uint row = gl_WorkGroupID.y;
81 while (row < p.nrows) {
82 argsort(false, row);
83 row += gl_WorkGroupSize.y * gl_NumWorkGroups.y;
84 }
85 } else {
86 uint row = gl_WorkGroupID.y;
87 while (row < p.nrows) {
88 argsort(true, row);
89 row += gl_WorkGroupSize.y * gl_NumWorkGroups.y;
90 }
91 }
92}