Skip to content

Commit c1fd645

Browse files
apage43cebtenzzre
authored andcommitted
attempted speedups 2
1 parent 9bc52eb commit c1fd645

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

ggml-vulkan.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -989,26 +989,27 @@ void ggml_vk_mul_mat_mat_f32(kp::Sequence& seq,
989989
nb1, nb2
990990
};
991991

992+
const uint32_t local_x = ggml_vk_current_device().subgroupSize;
992993
std::shared_ptr<kp::Algorithm> s_algo = nullptr;
993994
if (!komputeManager()->hasAlgorithm(__func__)) {
994-
//std::cerr << "init f32 matmat shader" << std::endl;
995-
s_algo = komputeManager()->algorithm<float, PushConstants>(__func__, s_kompute_context->pool.get(),
995+
s_algo = komputeManager()->algorithm<uint32_t, PushConstants>(__func__, s_kompute_context->pool.get(),
996996
{inA, inB, out}, spirv,
997997
{unsigned(ne01),
998998
unsigned(ne11),
999-
unsigned(ne12)},
1000-
{},
999+
unsigned(std::max(ne12, ne02))
1000+
},
1001+
{local_x},
10011002
{pushConsts});
10021003
} else {
10031004
s_algo = komputeManager()->getAlgorithm(__func__);
10041005
s_algo->setTensors({inA, inB, out});
10051006
s_algo->setWorkgroup({unsigned(ne01),
10061007
unsigned(ne11),
1007-
unsigned(std::max(ne12, ne02))});
1008+
unsigned(std::max(ne12, ne02)),
1009+
});
10081010
s_algo->setPushConstants<PushConstants>({pushConsts});
10091011
s_algo->updateDescriptors(s_kompute_context->pool.get());
10101012
}
1011-
//seq.record<kp::OpTensorFill>({out});
10121013
seq.record<kp::OpAlgoDispatch>(s_algo);
10131014
}
10141015

@@ -1038,15 +1039,16 @@ void ggml_vk_mul_mat_mat_f16(kp::Sequence& seq,
10381039
nb1, nb2
10391040
};
10401041

1042+
const uint32_t local_x = ggml_vk_current_device().subgroupSize;
10411043
std::shared_ptr<kp::Algorithm> s_algo = nullptr;
10421044
if (!komputeManager()->hasAlgorithm(__func__)) {
1043-
s_algo = komputeManager()->algorithm<float, PushConstants>(__func__, s_kompute_context->pool.get(),
1045+
s_algo = komputeManager()->algorithm<uint32_t, PushConstants>(__func__, s_kompute_context->pool.get(),
10441046
{inA, inB, out}, spirv,
10451047
{unsigned(ne01),
10461048
unsigned(ne11),
10471049
unsigned(std::max(ne12, ne02))
10481050
},
1049-
{},
1051+
{local_x},
10501052
{pushConsts});
10511053
} else {
10521054
s_algo = komputeManager()->getAlgorithm(__func__);
@@ -1141,7 +1143,7 @@ void ggml_vk_mul_mat_mat_q6_k(
11411143
if (!komputeManager()->hasAlgorithm(__func__)) {
11421144
s_algo = komputeManager()->algorithm<float, PushConstants>(__func__, s_kompute_context->pool.get(),
11431145
{inA, inB, out}, spirv,
1144-
{unsigned(ne01)/32,
1146+
{unsigned(ne01)/256,
11451147
unsigned(ne11),
11461148
unsigned(std::max(ne12, ne02))
11471149
},
@@ -1150,7 +1152,7 @@ void ggml_vk_mul_mat_mat_q6_k(
11501152
} else {
11511153
s_algo = komputeManager()->getAlgorithm(__func__);
11521154
s_algo->setTensors({inA, inB, out});
1153-
s_algo->setWorkgroup({unsigned(ne01)/32,
1155+
s_algo->setWorkgroup({unsigned(ne01)/256,
11541156
unsigned(ne11),
11551157
unsigned(std::max(ne12, ne02)),
11561158
});
@@ -1192,7 +1194,7 @@ void ggml_vk_mul_mat_mat_q4_x(const std::vector<uint32_t>& spirv,
11921194
{unsigned(ne01),
11931195
unsigned(ne11),
11941196
unsigned(std::max(ne12, ne02))},
1195-
{local_x, 4},
1197+
{local_x, 1},
11961198
{pushConsts});
11971199
} else {
11981200
s_algo = komputeManager()->getAlgorithm(__func__);

kompute/op_mul_mat_mat_f16.comp

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
#extension GL_KHR_shader_subgroup_arithmetic : require
1515
#extension GL_EXT_debug_printf : enable
1616

17-
// layout(local_size_x = 8) in;
17+
// device subgroup size
18+
layout (local_size_x_id = 0) in;
1819

1920
layout(binding = 0) readonly buffer tensorInA { float16_t inA[]; };
2021
layout(binding = 1) readonly buffer tensorInB { float inB[]; };
@@ -40,17 +41,20 @@ pcs;
4041

4142

4243
void main() {
43-
uvec3 gid = gl_GlobalInvocationID;
44+
uvec3 gid = gl_WorkGroupID;
4445

4546
uint bc_ab = pcs.ne12 > pcs.ne02 ? gid.z / (pcs.ne12 / pcs.ne02) : gid.z;
4647
uint bc_ba = pcs.ne02 > pcs.ne12 ? gid.z / (pcs.ne02 / pcs.ne12) : gid.z;
4748

4849
const uint x = (gid.x*pcs.nb01 + bc_ab*pcs.nb02) / 2 + pcs.inAOff; // Based from inA
4950
const uint y = (gid.y*pcs.nb11 + bc_ba*pcs.nb12) / 4 + pcs.inBOff; // based from inB
5051
float sum = 0.0f;
51-
for (uint i = 0; i < pcs.ne00; i ++) {
52+
for (uint i = gl_SubgroupInvocationID.x; i < pcs.ne00; i += gl_SubgroupSize) {
5253
sum += float(inA[x+i]) * float(inB[y+i]);
5354
}
5455

55-
out_[gid.z*(pcs.nb2/4) + gid.y*(pcs.nb1/4) + gid.x + pcs.outOff] = sum;
56+
const float all_sum = subgroupAdd(sum);
57+
if (subgroupElect()) {
58+
out_[gid.z*(pcs.nb2/4) + gid.y*(pcs.nb1/4) + gid.x + pcs.outOff] = all_sum;
59+
}
5660
}

kompute/op_mul_mat_mat_f32.comp

+14-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
#extension GL_KHR_shader_subgroup_arithmetic : require
1515
#extension GL_EXT_debug_printf : enable
1616

17-
// layout(local_size_x = 8) in;
17+
// device subgroup size
18+
layout (local_size_x_id = 0) in;
1819

1920
layout(binding = 0) readonly buffer tensorInA { float inA[]; };
2021
layout(binding = 1) readonly buffer tensorInB { float inB[]; };
@@ -40,14 +41,20 @@ pcs;
4041

4142

4243
void main() {
43-
uvec3 gid = gl_GlobalInvocationID;
44+
uvec3 gid = gl_WorkGroupID;
4445

45-
const uint x = (gid.x*pcs.nb01 + gid.z/(pcs.ne12/pcs.ne02)*pcs.nb02) / 4 + pcs.inAOff; // Based from inA
46-
const uint y = (gid.y*pcs.nb11 + gid.z/(pcs.ne02/pcs.ne12)*pcs.nb12) / 4 + pcs.inBOff; // based from inB
46+
uint bc_ab = pcs.ne12 > pcs.ne02 ? gid.z / (pcs.ne12 / pcs.ne02) : gid.z;
47+
uint bc_ba = pcs.ne02 > pcs.ne12 ? gid.z / (pcs.ne02 / pcs.ne12) : gid.z;
48+
49+
const uint x = (gid.x*pcs.nb01 + bc_ab*pcs.nb02) / 4 + pcs.inAOff; // Based from inA
50+
const uint y = (gid.y*pcs.nb11 + bc_ba*pcs.nb12) / 4 + pcs.inBOff; // based from inB
4751
float sum = 0.0f;
48-
for (uint i = 0; i < pcs.ne00; i ++) {
52+
for (uint i = gl_SubgroupInvocationID.x; i < pcs.ne00; i += gl_SubgroupSize) {
4953
sum += float(inA[x+i]) * float(inB[y+i]);
5054
}
5155

52-
out_[gid.z*(pcs.nb2/4) + gid.y*(pcs.nb1/4) + gid.x + pcs.outOff] = sum;
53-
}
56+
const float all_sum = subgroupAdd(sum);
57+
if (subgroupElect()) {
58+
out_[gid.z*(pcs.nb2/4) + gid.y*(pcs.nb1/4) + gid.x + pcs.outOff] = all_sum;
59+
}
60+
}

kompute/op_mul_mat_mat_q6_k.comp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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 = 256) in;
1818

1919
layout(binding = 0) readonly buffer tensorInA { uint8_t inA[]; };
2020
layout(binding = 1) readonly buffer tensorInB { float inB[]; };

0 commit comments

Comments
 (0)