Skip to content

Commit f627f03

Browse files
jeffbolznvNeoZhangJianyu
authored andcommitted
vulkan: im2col and matmul optimizations for stable diffusion (ggml-org#10942)
* tests: Add im2col perf tests * vulkan: optimize im2col, more elements per thread * vulkan: increase small tile size for NV_coopmat2 * vulkan: change im2col to 512 elements per workgroup
1 parent 80bdfbb commit f627f03

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed

ggml/src/ggml-vulkan/ggml-vulkan.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,10 +1404,10 @@ static void ggml_vk_load_shaders(vk_device& device) {
14041404
// spec constants and tile sizes for non-quant matmul/matmul_id
14051405
l_warptile = { 256, 128, 256, 64 };
14061406
m_warptile = { 256, 128, 128, 64 };
1407-
s_warptile = { 128, 32, 16, 64 };
1407+
s_warptile = { 128, 64, 64, 64 };
14081408
l_wg_denoms = {128, 256, 1 };
14091409
m_wg_denoms = {128, 128, 1 };
1410-
s_wg_denoms = { 32, 16, 1 };
1410+
s_wg_denoms = { 64, 64, 1 };
14111411

14121412
// spec constants and tile sizes for quant matmul (non-Qi_K)
14131413
l_warptile_mmq = { 256, 128, 256, 64 };
@@ -2017,11 +2017,11 @@ static void ggml_vk_load_shaders(vk_device& device) {
20172017

20182018
ggml_vk_create_pipeline(device, device->pipeline_sum_rows_f32, "sum_rows_f32", sum_rows_f32_len, sum_rows_f32_data, "main", 2, sizeof(vk_op_push_constants), {1, 1, 1}, { device->subgroup_size }, 1);
20192019

2020-
ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32_len, im2col_f32_data, "main", 2, sizeof(vk_op_im2col_push_constants), {256, 1, 1}, {}, 1);
2020+
ggml_vk_create_pipeline(device, device->pipeline_im2col_f32, "im2col_f32", im2col_f32_len, im2col_f32_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
20212021
if (device->float_controls_rte_fp16) {
2022-
ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte_len, im2col_f32_f16_rte_data, "main", 2, sizeof(vk_op_im2col_push_constants), {256, 1, 1}, {}, 1);
2022+
ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_rte_len, im2col_f32_f16_rte_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
20232023
} else {
2024-
ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_len, im2col_f32_f16_data, "main", 2, sizeof(vk_op_im2col_push_constants), {256, 1, 1}, {}, 1);
2024+
ggml_vk_create_pipeline(device, device->pipeline_im2col_f32_f16, "im2col_f32_f16", im2col_f32_f16_len, im2col_f32_f16_data, "main", 2, sizeof(vk_op_im2col_push_constants), {512, 1, 1}, { device->subgroup_size }, 1, true);
20252025
}
20262026

20272027
ggml_vk_create_pipeline(device, device->pipeline_timestep_embedding_f32, "timestep_embedding_f32", timestep_embedding_f32_len, timestep_embedding_f32_data, "main", 2, sizeof(vk_op_timestep_embedding_push_constants), {256, 1, 1}, {}, 1);

ggml/src/ggml-vulkan/vulkan-shaders/im2col.comp

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#extension GL_EXT_shader_16bit_storage : require
44
#extension GL_EXT_spirv_intrinsics: enable
5+
#extension GL_EXT_control_flow_attributes : require
56

67
#if RTE16
78
spirv_execution_mode(capabilities = [4467], 4462, 16); // RoundingModeRTE, 16 bits
@@ -23,40 +24,64 @@ layout (push_constant) uniform parameter
2324

2425
#include "types.comp"
2526

26-
#define BLOCK_SIZE 256
27+
layout(constant_id = 0) const uint BLOCK_SIZE = 32;
2728

28-
layout(local_size_x = BLOCK_SIZE, local_size_y = 1, local_size_z = 1) in;
29+
const uint NUM_ITER = 512 / BLOCK_SIZE;
30+
31+
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
2932

3033
layout (binding = 0) readonly buffer X {A_TYPE data_a[];};
3134
layout (binding = 1) writeonly buffer D {D_TYPE data_d[];};
3235

3336
void main() {
34-
const uint i = gl_GlobalInvocationID.x;
35-
if (i >= p.pelements) {
36-
return;
37-
}
38-
39-
const uint ksize = p.OW * (p.KH > 1 ? p.KW : 1);
40-
const uint kx = i / ksize;
41-
const uint kd = kx * ksize;
42-
const uint ky = (i - kd) / p.OW;
43-
const uint ix = i % p.OW;
37+
const uint gidx = gl_GlobalInvocationID.x;
4438

4539
const uint oh = gl_GlobalInvocationID.y;
4640
const uint batch = gl_GlobalInvocationID.z / p.IC;
4741
const uint ic = gl_GlobalInvocationID.z % p.IC;
4842

49-
const uint iiw = ix * p.s0 + kx * p.d0 - p.p0;
50-
const uint iih = oh * p.s1 + ky * p.d1 - p.p1;
43+
A_TYPE values[NUM_ITER];
44+
uint offset_dst[NUM_ITER];
45+
[[unroll]] for (uint idx = 0; idx < NUM_ITER; ++idx) {
46+
values[idx] = A_TYPE(0);
47+
}
48+
49+
[[unroll]] for (uint idx = 0; idx < NUM_ITER; ++idx) {
50+
51+
const uint i = gidx * NUM_ITER + idx;
52+
53+
const uint ksize = p.OW * (p.KH > 1 ? p.KW : 1);
54+
const uint kx = i / ksize;
55+
const uint kd = kx * ksize;
56+
const uint ky = (i - kd) / p.OW;
57+
const uint ix = i % p.OW;
58+
59+
const uint iiw = ix * p.s0 + kx * p.d0 - p.p0;
60+
const uint iih = oh * p.s1 + ky * p.d1 - p.p1;
5161

52-
const uint offset_dst =
53-
((batch * p.OH + oh) * p.OW + ix) * p.CHW +
54-
(ic * (p.KW * p.KH) + ky * p.KW + kx);
62+
offset_dst[idx] =
63+
((batch * p.OH + oh) * p.OW + ix) * p.CHW +
64+
(ic * (p.KW * p.KH) + ky * p.KW + kx);
5565

56-
if (iih < 0 || iih >= p.IH || iiw < 0 || iiw >= p.IW) {
57-
data_d[offset_dst] = D_TYPE(0.0f);
58-
} else {
59-
const uint offset_src = ic * p.offset_delta + batch * p.batch_offset;
60-
data_d[offset_dst] = D_TYPE(data_a[offset_src + iih * p.IW + iiw]);
66+
if (i >= p.pelements) {
67+
continue;
68+
}
69+
70+
if (iih < p.IH && iiw < p.IW) {
71+
const uint offset_src = ic * p.offset_delta + batch * p.batch_offset;
72+
values[idx] = data_a[offset_src + iih * p.IW + iiw];
73+
}
6174
}
75+
76+
[[unroll]] for (uint idx = 0; idx < NUM_ITER; ++idx) {
77+
78+
const uint i = gidx * NUM_ITER + idx;
79+
80+
if (i >= p.pelements) {
81+
continue;
82+
}
83+
84+
data_d[offset_dst[idx]] = D_TYPE(values[idx]);
85+
}
86+
6287
}

tests/test-backend-ops.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3945,6 +3945,18 @@ static std::vector<std::unique_ptr<test_case>> make_test_cases_perf() {
39453945
}
39463946
}
39473947

3948+
for (int K : {3, 5}) {
3949+
for (int IC : {256, 2560}) {
3950+
for (int IW_IH : {32, 64, 256}) {
3951+
if (IC == 2560 && IW_IH == 256) {
3952+
// too big
3953+
continue;
3954+
}
3955+
test_cases.emplace_back(new test_im2col(GGML_TYPE_F32, GGML_TYPE_F16, GGML_TYPE_F32, {IW_IH, IW_IH, IC, 1}, {K, K, IC, 1}, 1, 1, 1, 1, 1, 1, true));
3956+
}
3957+
}
3958+
}
3959+
39483960
return test_cases;
39493961
}
39503962

0 commit comments

Comments
 (0)