Skip to content

Commit 9bc52eb

Browse files
apage43cebtenzzre
authored andcommitted
attempted speedups
1 parent 8dc79ac commit 9bc52eb

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

ggml-vulkan.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,17 +1186,18 @@ void ggml_vk_mul_mat_mat_q4_x(const std::vector<uint32_t>& spirv,
11861186

11871187
std::shared_ptr<kp::Algorithm> s_algo = nullptr;
11881188
if (!komputeManager()->hasAlgorithm(__func__)) {
1189-
s_algo = komputeManager()->algorithm<float, PushConstants>(__func__, s_kompute_context->pool.get(),
1189+
const uint32_t local_x = ggml_vk_current_device().subgroupSize;
1190+
s_algo = komputeManager()->algorithm<uint32_t, PushConstants>(__func__, s_kompute_context->pool.get(),
11901191
{inA, inB, out}, spirv,
1191-
{unsigned(ne01)/32,
1192+
{unsigned(ne01),
11921193
unsigned(ne11),
11931194
unsigned(std::max(ne12, ne02))},
1194-
{},
1195+
{local_x, 4},
11951196
{pushConsts});
11961197
} else {
11971198
s_algo = komputeManager()->getAlgorithm(__func__);
11981199
s_algo->setTensors({inA, inB, out});
1199-
s_algo->setWorkgroup({unsigned(ne01)/32,
1200+
s_algo->setWorkgroup({unsigned(ne01),
12001201
unsigned(ne11),
12011202
unsigned(std::max(ne12, ne02)),
12021203
});

kompute/op_mul_mat_mat_q4_0.comp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
#extension GL_KHR_shader_subgroup_arithmetic : require
1515
#extension GL_EXT_debug_printf : enable
1616

17-
layout(local_size_x = 32) in;
17+
layout (local_size_x_id = 0) in;
18+
layout (local_size_y_id = 1) in;
19+
layout (constant_id = 1) const uint nsg = 2;
1820

1921
layout(binding = 0) readonly buffer tensorInA { uint8_t inA[]; };
2022
layout(binding = 1) readonly buffer tensorInB { float inB[]; };
@@ -38,34 +40,38 @@ layout(push_constant) uniform parameter {
3840
}
3941
pcs;
4042

41-
#define ELS_PER_BLOCK 32
42-
#define QS_OFFSET 2
43-
#define BLOCK_SIZE ((ELS_PER_BLOCK / 2) + QS_OFFSET)
43+
const uint els_per_block = 32;
44+
const uint qs_offset = 2;
45+
const uint block_size = (els_per_block / 2) + qs_offset;
46+
4447

4548
void main() {
46-
uvec3 gid = gl_GlobalInvocationID;
49+
uvec3 gid = gl_WorkGroupID;
50+
uvec3 lid = gl_LocalInvocationID;
51+
gid.y = gid.y * nsg + lid.y;
4752

4853
uint bc_ab = pcs.ne12 > pcs.ne02 ? gid.z / (pcs.ne12 / pcs.ne02) : gid.z;
4954
uint bc_ba = pcs.ne02 > pcs.ne12 ? gid.z / (pcs.ne02 / pcs.ne12) : gid.z;
5055

51-
5256
const uint x = (gid.x*pcs.nb01 + bc_ab*pcs.nb02) + pcs.inAOff; // Based from inA
5357
const uint y = (gid.y*pcs.nb11 + bc_ba*pcs.nb12) / 4 + pcs.inBOff; // based from inB
5458
float sum = 0.0f;
55-
for (uint i = 0; i < pcs.ne00; i+=ELS_PER_BLOCK) {
56-
for (uint j = 0; j < ELS_PER_BLOCK / 2; j++) {
57-
const uint block_number = i / ELS_PER_BLOCK;
58-
const uint block_offset = block_number * BLOCK_SIZE;
59+
for (uint i = gl_SubgroupInvocationID * 2; i < pcs.ne00; i+=gl_SubgroupSize * 2) {
60+
const uint block_number = i / els_per_block;
61+
const uint block_offset = block_number * block_size;
5962
const float d = u8BufToFloat16(inA, x + block_offset);
63+
const uint j = (i % els_per_block) / 2;
6064
const uint byte_position_in_block = j;
61-
const int q0 = (inA[x+block_offset+QS_OFFSET+byte_position_in_block] & 0x0F) - 8;
62-
const int q1 = (inA[x+block_offset+QS_OFFSET+byte_position_in_block] >> 4) - 8;
65+
const int q0 = (inA[x+block_offset+qs_offset+byte_position_in_block] & 0x0F) - 8;
66+
const int q1 = (inA[x+block_offset+qs_offset+byte_position_in_block] >> 4) - 8;
6367
const float dq0 = d * q0;
6468
const float dq1 = d * q1;
65-
sum += (dq0 * float(inB[y+i+j])) + \
66-
(dq1 * float(inB[y+i+j+(ELS_PER_BLOCK/2)]));
67-
}
69+
const uint block_base = block_number * els_per_block;
70+
sum += (dq0 * float(inB[y+block_base+j])) + \
71+
(dq1 * float(inB[y+block_base+j+(els_per_block/2)]));
6872
}
6973

70-
out_[gid.z*(pcs.nb2/4) + gid.y*(pcs.nb1/4) + gid.x + pcs.outOff] = sum;
74+
const float all_sum = subgroupAdd(sum);
75+
if (subgroupElect())
76+
out_[gid.z*(pcs.nb2/4) + gid.y*(pcs.nb1/4) + gid.x + pcs.outOff] = all_sum;
7177
}

0 commit comments

Comments
 (0)