Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-vulkan/vulkan-shaders/mul_mat_vec_base.glsl # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-vulkan / vulkan-shaders / mul_mat_vec_base.glsl` 242 lines ยท 8725 bytes Raw Blame History 1 #extension GL_EXT_control_flow_attributes : enable 2 #extension GL_EXT_shader_16bit_storage : require 3 #extension GL_EXT_shader_8bit_storage : require 4 5 #if USE_SUBGROUP_ADD || USE_SUBGROUP_ADD_NO_SHMEM 6 #extension GL_KHR_shader_subgroup_basic : require 7 #extension GL_KHR_shader_subgroup_arithmetic : require 8 #endif 9 10 #ifdef MUL_MAT_ID 11 #define EXPERT_COUNT 8 12 #endif 13 14 #include "mul_mat_vec_iface.glsl" 15 16 layout (push_constant) uniform parameter 17 { 18 uint ncols; 19 uint stride_a; 20 uint stride_b; 21 uint stride_d; 22 23 uint batch_stride_a; 24 uint batch_stride_b; 25 uint batch_stride_d; 26 27 uint fusion_flags; 28 29 #ifdef MUL_MAT_ID 30 uint nei0; 31 uint ne11; 32 uint expert_i1; 33 uint nbi1; 34 #else 35 uint base_work_group_y; 36 uint ne02; 37 uint ne12; 38 uint broadcast2; 39 uint broadcast3; 40 #endif 41 } p; 42 43 #ifdef MUL_MAT_ID 44 uint expert_id; 45 bool expert_skip; // an id of -1 skips the slot, the result row is zeroed 46 #endif 47 48 void get_offsets(out uint a_offset, out uint b_offset, out uint d_offset) { 49 #ifdef MUL_MAT_ID 50 const uint expert_i0 = gl_WorkGroupID.y; 51 #else 52 const uint batch_idx = gl_WorkGroupID.y + p.base_work_group_y; 53 #endif 54 55 #ifndef MUL_MAT_ID 56 uint batch_idx_a = 0; 57 if (batch_idx != 0) { 58 const uint i13 = batch_idx / p.ne12; 59 const uint i12 = batch_idx % p.ne12; 60 61 const uint i03 = i13 / p.broadcast3; 62 const uint i02 = i12 / p.broadcast2; 63 64 batch_idx_a = i03 * p.ne02 + i02; 65 } 66 #else 67 const int expert = data_ids[expert_i0 + p.expert_i1 * p.nbi1]; 68 expert_skip = expert == -1; 69 expert_id = expert_skip ? 0 : uint(expert); // clamp, so that the offsets below stay in range 70 #endif 71 72 a_offset = 73 #ifdef MUL_MAT_ID 74 expert_id * (p.batch_stride_a / QUANT_K); 75 #else 76 batch_idx_a * (p.batch_stride_a / QUANT_K); 77 #endif 78 b_offset = 79 #ifdef MUL_MAT_ID 80 (expert_i0 % p.ne11) * p.stride_b + p.expert_i1 * p.batch_stride_b; 81 #else 82 batch_idx * p.batch_stride_b; 83 #endif 84 d_offset = 85 #ifdef MUL_MAT_ID 86 expert_i0 * p.stride_d + p.expert_i1 * p.batch_stride_d; 87 #else 88 batch_idx * p.batch_stride_d; 89 #endif 90 } 91 92 layout (constant_id = 0) const uint BLOCK_SIZE = 32; 93 layout (constant_id = 1) const uint NUM_ROWS = 1; 94 layout (constant_id = 2) const uint NUM_COLS = 1; 95 96 #ifdef USE_SUBGROUP_ADD_NO_SHMEM 97 void reduce_result(inout FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const in uint32_t d_offset, const in uint32_t first_row, const in uint32_t num_rows, const in uint32_t tid) { 98 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 99 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 100 temp[j][n] = subgroupAdd(temp[j][n]); 101 } 102 } 103 104 if (tid == 0) { 105 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 106 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 107 #ifdef MUL_MAT_ID 108 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) { 109 temp[j][n] += FLOAT_TYPE(data_fuse0[expert_id*p.stride_d + first_row + n]); 110 } 111 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_SCALE0) != 0) { 112 const uint expert_i0 = gl_GlobalInvocationID.y; 113 temp[j][n] *= FLOAT_TYPE(data_fuse0[expert_i0]); 114 } 115 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_SCALE1) != 0) { 116 const uint expert_i0 = gl_GlobalInvocationID.y; 117 temp[j][n] *= FLOAT_TYPE(data_fuse1[expert_i0]); 118 } 119 if (expert_skip) { 120 temp[j][n] = FLOAT_TYPE(0); 121 } 122 #else 123 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) { 124 temp[j][n] += FLOAT_TYPE(data_fuse0[j*p.batch_stride_d + d_offset + first_row + n]); 125 } 126 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS1) != 0) { 127 temp[j][n] += FLOAT_TYPE(data_fuse1[j*p.batch_stride_d + d_offset + first_row + n]); 128 } 129 #endif 130 data_d[j*p.batch_stride_d + d_offset + first_row + n] = D_TYPE(temp[j][n]); 131 } 132 } 133 } 134 } 135 #else 136 shared FLOAT_TYPE tmpsh[NUM_COLS][NUM_ROWS][BLOCK_SIZE]; 137 138 void reduce_result(FLOAT_TYPE temp[NUM_COLS][NUM_ROWS], const in uint32_t d_offset, const in uint32_t first_row, const in uint32_t num_rows, const in uint32_t tid) { 139 // subgroupAdd is probably faster on devices that support it, 140 // particularly when the workgroup has more than one subgroup 141 #if USE_SUBGROUP_ADD 142 // sum up partial sums within a subgroup 143 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 144 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 145 temp[j][n] = subgroupAdd(temp[j][n]); 146 } 147 } 148 149 // Go through shared memory to sum partials across subgroups 150 if (gl_SubgroupInvocationID == 0) { 151 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 152 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 153 tmpsh[j][n][gl_SubgroupID] = temp[j][n]; 154 } 155 } 156 } 157 barrier(); 158 if (tid == 0) { 159 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 160 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 161 temp[j][n] = FLOAT_TYPE(0); 162 [[unroll]] for (uint s = 0; s < gl_NumSubgroups; ++s) { 163 temp[j][n] += tmpsh[j][n][s]; 164 } 165 #ifdef MUL_MAT_ID 166 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) { 167 temp[j][n] += FLOAT_TYPE(data_fuse0[expert_id*p.stride_d + first_row + n]); 168 } 169 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_SCALE0) != 0) { 170 const uint expert_i0 = gl_GlobalInvocationID.y; 171 temp[j][n] *= FLOAT_TYPE(data_fuse0[expert_i0]); 172 } 173 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_SCALE1) != 0) { 174 const uint expert_i0 = gl_GlobalInvocationID.y; 175 temp[j][n] *= FLOAT_TYPE(data_fuse1[expert_i0]); 176 } 177 if (expert_skip) { 178 temp[j][n] = FLOAT_TYPE(0); 179 } 180 #else 181 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) { 182 temp[j][n] += FLOAT_TYPE(data_fuse0[j*p.batch_stride_d + d_offset + first_row + n]); 183 } 184 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS1) != 0) { 185 temp[j][n] += FLOAT_TYPE(data_fuse1[j*p.batch_stride_d + d_offset + first_row + n]); 186 } 187 #endif 188 data_d[j*p.batch_stride_d + d_offset + first_row + n] = D_TYPE(temp[j][n]); 189 } 190 } 191 } 192 #else 193 // sum up partial sums and write back result 194 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 195 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 196 tmpsh[j][n][tid] = temp[j][n]; 197 } 198 } 199 barrier(); 200 [[unroll]] for (uint s = BLOCK_SIZE/2; s > 0; s >>= 1) { 201 if (tid < s) { 202 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 203 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 204 tmpsh[j][n][tid] += tmpsh[j][n][tid + s]; 205 } 206 } 207 } 208 barrier(); 209 } 210 if (tid == 0) { 211 [[unroll]] for (uint j = 0; j < NUM_COLS; ++j) { 212 [[unroll]] for (uint n = 0; n < num_rows; ++n) { 213 #ifdef MUL_MAT_ID 214 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) { 215 tmpsh[j][n][0] += FLOAT_TYPE(data_fuse0[expert_id*p.stride_d + first_row + n]); 216 } 217 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_SCALE0) != 0) { 218 const uint expert_i0 = gl_GlobalInvocationID.y; 219 tmpsh[j][n][0] *= FLOAT_TYPE(data_fuse0[expert_i0]); 220 } 221 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_SCALE1) != 0) { 222 const uint expert_i0 = gl_GlobalInvocationID.y; 223 tmpsh[j][n][0] *= FLOAT_TYPE(data_fuse1[expert_i0]); 224 } 225 if (expert_skip) { 226 tmpsh[j][n][0] = FLOAT_TYPE(0); 227 } 228 #else 229 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS0) != 0) { 230 tmpsh[j][n][0] += FLOAT_TYPE(data_fuse0[j*p.batch_stride_d + d_offset + first_row + n]); 231 } 232 if ((p.fusion_flags & MAT_VEC_FUSION_FLAGS_BIAS1) != 0) { 233 tmpsh[j][n][0] += FLOAT_TYPE(data_fuse1[j*p.batch_stride_d + d_offset + first_row + n]); 234 } 235 #endif 236 data_d[j*p.batch_stride_d + d_offset + first_row + n] = D_TYPE(tmpsh[j][n][0]); 237 } 238 } 239 } 240 #endif 241 } 242 #endif