From 9bc52ebae313c028c2293c260d12d0d0049c5ea1 Mon Sep 17 00:00:00 2001 From: Aaron Miller Date: Fri, 13 Oct 2023 11:10:02 -0700 Subject: [PATCH] attempted speedups --- ggml-vulkan.cpp | 9 ++++---- kompute/op_mul_mat_mat_q4_0.comp | 38 ++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/ggml-vulkan.cpp b/ggml-vulkan.cpp index 56f15310d..67270a3c7 100644 --- a/ggml-vulkan.cpp +++ b/ggml-vulkan.cpp @@ -1186,17 +1186,18 @@ void ggml_vk_mul_mat_mat_q4_x(const std::vector& spirv, std::shared_ptr s_algo = nullptr; if (!komputeManager()->hasAlgorithm(__func__)) { - s_algo = komputeManager()->algorithm(__func__, s_kompute_context->pool.get(), + const uint32_t local_x = ggml_vk_current_device().subgroupSize; + s_algo = komputeManager()->algorithm(__func__, s_kompute_context->pool.get(), {inA, inB, out}, spirv, - {unsigned(ne01)/32, + {unsigned(ne01), unsigned(ne11), unsigned(std::max(ne12, ne02))}, - {}, + {local_x, 4}, {pushConsts}); } else { s_algo = komputeManager()->getAlgorithm(__func__); s_algo->setTensors({inA, inB, out}); - s_algo->setWorkgroup({unsigned(ne01)/32, + s_algo->setWorkgroup({unsigned(ne01), unsigned(ne11), unsigned(std::max(ne12, ne02)), }); diff --git a/kompute/op_mul_mat_mat_q4_0.comp b/kompute/op_mul_mat_mat_q4_0.comp index aecd04cca..80a1ff627 100644 --- a/kompute/op_mul_mat_mat_q4_0.comp +++ b/kompute/op_mul_mat_mat_q4_0.comp @@ -14,7 +14,9 @@ #extension GL_KHR_shader_subgroup_arithmetic : require #extension GL_EXT_debug_printf : enable -layout(local_size_x = 32) in; +layout (local_size_x_id = 0) in; +layout (local_size_y_id = 1) in; +layout (constant_id = 1) const uint nsg = 2; layout(binding = 0) readonly buffer tensorInA { uint8_t inA[]; }; layout(binding = 1) readonly buffer tensorInB { float inB[]; }; @@ -38,34 +40,38 @@ layout(push_constant) uniform parameter { } pcs; -#define ELS_PER_BLOCK 32 -#define QS_OFFSET 2 -#define BLOCK_SIZE ((ELS_PER_BLOCK / 2) + QS_OFFSET) +const uint els_per_block = 32; +const uint qs_offset = 2; +const uint block_size = (els_per_block / 2) + qs_offset; + void main() { - uvec3 gid = gl_GlobalInvocationID; + uvec3 gid = gl_WorkGroupID; + uvec3 lid = gl_LocalInvocationID; + gid.y = gid.y * nsg + lid.y; uint bc_ab = pcs.ne12 > pcs.ne02 ? gid.z / (pcs.ne12 / pcs.ne02) : gid.z; uint bc_ba = pcs.ne02 > pcs.ne12 ? gid.z / (pcs.ne02 / pcs.ne12) : gid.z; - const uint x = (gid.x*pcs.nb01 + bc_ab*pcs.nb02) + pcs.inAOff; // Based from inA const uint y = (gid.y*pcs.nb11 + bc_ba*pcs.nb12) / 4 + pcs.inBOff; // based from inB float sum = 0.0f; - for (uint i = 0; i < pcs.ne00; i+=ELS_PER_BLOCK) { - for (uint j = 0; j < ELS_PER_BLOCK / 2; j++) { - const uint block_number = i / ELS_PER_BLOCK; - const uint block_offset = block_number * BLOCK_SIZE; + for (uint i = gl_SubgroupInvocationID * 2; i < pcs.ne00; i+=gl_SubgroupSize * 2) { + const uint block_number = i / els_per_block; + const uint block_offset = block_number * block_size; const float d = u8BufToFloat16(inA, x + block_offset); + const uint j = (i % els_per_block) / 2; const uint byte_position_in_block = j; - const int q0 = (inA[x+block_offset+QS_OFFSET+byte_position_in_block] & 0x0F) - 8; - const int q1 = (inA[x+block_offset+QS_OFFSET+byte_position_in_block] >> 4) - 8; + const int q0 = (inA[x+block_offset+qs_offset+byte_position_in_block] & 0x0F) - 8; + const int q1 = (inA[x+block_offset+qs_offset+byte_position_in_block] >> 4) - 8; const float dq0 = d * q0; const float dq1 = d * q1; - sum += (dq0 * float(inB[y+i+j])) + \ - (dq1 * float(inB[y+i+j+(ELS_PER_BLOCK/2)])); - } + const uint block_base = block_number * els_per_block; + sum += (dq0 * float(inB[y+block_base+j])) + \ + (dq1 * float(inB[y+block_base+j+(els_per_block/2)])); } - out_[gid.z*(pcs.nb2/4) + gid.y*(pcs.nb1/4) + gid.x + pcs.outOff] = sum; + const float all_sum = subgroupAdd(sum); + if (subgroupElect()) + out_[gid.z*(pcs.nb2/4) + gid.y*(pcs.nb1/4) + gid.x + pcs.outOff] = all_sum; } \ No newline at end of file