Skip to content

Commit 9ec19e1

Browse files
committed
vulkan: implement SSM conv operation
Add State Space Model conv operation to the Vulkan backend. Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 2a6f4cc commit 9ec19e1

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ struct vk_device_struct {
584584
vk_pipeline pipeline_rwkv_wkv7_f32;
585585
vk_pipeline pipeline_ssm_scan_f32_d128;
586586
vk_pipeline pipeline_ssm_scan_f32_d256;
587+
vk_pipeline pipeline_ssm_conv_f32;
587588
vk_pipeline pipeline_opt_step_adamw_f32;
588589
vk_pipeline pipeline_opt_step_sgd_f32;
589590
vk_pipeline pipeline_conv2d_f32[CONV_SHAPE_COUNT];
@@ -1096,6 +1097,12 @@ struct vk_op_ssm_scan_push_constants {
10961097
uint32_t s_off;
10971098
uint32_t n_head, d_head, n_group, n_tok;
10981099
};
1100+
struct vk_op_ssm_conv_push_constants {
1101+
uint32_t src0_nb1, src0_nb2;
1102+
uint32_t src1_nb1;
1103+
uint32_t dst_nb0, dst_nb1, dst_nb2;
1104+
uint32_t nc, ncs, nr, n_t, n_s;
1105+
};
10991106

11001107
struct vk_op_conv2d_push_constants {
11011108
uint32_t Cout;
@@ -3600,6 +3607,8 @@ static void ggml_vk_load_shaders(vk_device& device) {
36003607
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d128, "ssm_scan_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {128, device->subgroup_size, 16}, 1);
36013608
ggml_vk_create_pipeline(device, device->pipeline_ssm_scan_f32_d256, "ssm_scan_f32", ssm_scan_f32_len, ssm_scan_f32_data, "main", 8, sizeof(vk_op_ssm_scan_push_constants), {1, 1, 1}, {256, device->subgroup_size, 16}, 1);
36023609

3610+
ggml_vk_create_pipeline(device, device->pipeline_ssm_conv_f32, "ssm_conv_f32", ssm_conv_f32_len, ssm_conv_f32_data, "main", 3, sizeof(vk_op_ssm_conv_push_constants), {1, 1, 1}, {32}, 1);
3611+
36033612
ggml_vk_create_pipeline(device, device->pipeline_opt_step_adamw_f32, "opt_step_adamw_f32", opt_step_adamw_f32_len, opt_step_adamw_f32_data, "main", 5, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
36043613

36053614
ggml_vk_create_pipeline(device, device->pipeline_opt_step_sgd_f32, "opt_step_sgd_f32", opt_step_sgd_f32_len, opt_step_sgd_f32_data, "main", 3, sizeof(vk_op_push_constants), {512, 1, 1}, {}, 1);
@@ -8109,6 +8118,11 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
81098118
}
81108119
}
81118120
return nullptr;
8121+
case GGML_OP_SSM_CONV:
8122+
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
8123+
return ctx->device->pipeline_ssm_conv_f32;
8124+
}
8125+
return nullptr;
81128126
case GGML_OP_OPT_STEP_ADAMW:
81138127
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
81148128
return ctx->device->pipeline_opt_step_adamw_f32;
@@ -8603,6 +8617,14 @@ static void ggml_vk_op_f32(ggml_backend_vk_context * ctx, vk_context& subctx, co
86038617
}
86048618
}
86058619
break;
8620+
case GGML_OP_SSM_CONV:
8621+
{
8622+
const uint32_t nr = src0->ne[1];
8623+
const uint32_t n_t = dst->ne[1];
8624+
const uint32_t n_s = dst->ne[2];
8625+
elements = { CEIL_DIV(nr, 32), n_t, n_s };
8626+
}
8627+
break;
86068628
default:
86078629
elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
86088630
break;
@@ -9144,6 +9166,22 @@ static void ggml_vk_ssm_scan(ggml_backend_vk_context * ctx, vk_context& subctx,
91449166
}, pc, elements);
91459167
}
91469168

9169+
static void ggml_vk_ssm_conv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
9170+
const ggml_tensor * src0 = dst->src[0];
9171+
const ggml_tensor * src1 = dst->src[1];
9172+
9173+
ggml_vk_op_f32<vk_op_ssm_conv_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SSM_CONV, {
9174+
(uint32_t)src0->nb[1], (uint32_t)src0->nb[2],
9175+
(uint32_t)src1->nb[1],
9176+
(uint32_t)dst->nb[0], (uint32_t)dst->nb[1], (uint32_t)dst->nb[2],
9177+
(uint32_t)src1->ne[0],
9178+
(uint32_t)src0->ne[0],
9179+
(uint32_t)src0->ne[1],
9180+
(uint32_t)dst->ne[1],
9181+
(uint32_t)dst->ne[2],
9182+
}, dryrun);
9183+
}
9184+
91479185
static void ggml_vk_op_f32_opt_step_adamw(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, const vk_op_push_constants&& pc, bool dryrun = false) {
91489186
const ggml_tensor * x = dst->src[0];
91499187
const ggml_tensor * g = dst->src[1];
@@ -10977,6 +11015,7 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgr
1097711015
case GGML_OP_RWKV_WKV6:
1097811016
case GGML_OP_RWKV_WKV7:
1097911017
case GGML_OP_SSM_SCAN:
11018+
case GGML_OP_SSM_CONV:
1098011019
case GGML_OP_LEAKY_RELU:
1098111020
case GGML_OP_FLASH_ATTN_EXT:
1098211021
case GGML_OP_OPT_STEP_ADAMW:
@@ -11399,6 +11438,11 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgr
1139911438

1140011439
break;
1140111440

11441+
case GGML_OP_SSM_CONV:
11442+
ggml_vk_ssm_conv(ctx, compute_ctx, node, dryrun);
11443+
11444+
break;
11445+
1140211446
case GGML_OP_OPT_STEP_ADAMW:
1140311447
ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
1140411448

@@ -11511,6 +11555,7 @@ static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_cgraph *
1151111555
case GGML_OP_RWKV_WKV6:
1151211556
case GGML_OP_RWKV_WKV7:
1151311557
case GGML_OP_SSM_SCAN:
11558+
case GGML_OP_SSM_CONV:
1151411559
case GGML_OP_LEAKY_RELU:
1151511560
case GGML_OP_REPEAT:
1151611561
case GGML_OP_REPEAT_BACK:
@@ -13040,6 +13085,8 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
1304013085

1304113086
return true;
1304213087
}
13088+
case GGML_OP_SSM_CONV:
13089+
return true;
1304313090
case GGML_OP_CONV_TRANSPOSE_1D:
1304413091
return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
1304513092
case GGML_OP_CONV_2D:
@@ -13701,6 +13748,8 @@ static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph *
1370113748
} else if (tensor->op == GGML_OP_SSM_SCAN) {
1370213749
tensor_clone = ggml_ssm_scan(ggml_ctx, src_clone[0], src_clone[1], src_clone[2],
1370313750
src_clone[3], src_clone[4], src_clone[5], src_clone[6]);
13751+
} else if (tensor->op == GGML_OP_SSM_CONV) {
13752+
tensor_clone = ggml_ssm_conv(ggml_ctx, src_clone[0], src_clone[1]);
1370413753
}
1370513754
else {
1370613755
std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#version 450
2+
3+
#extension GL_EXT_control_flow_attributes : require
4+
5+
#include "types.glsl"
6+
7+
layout(constant_id = 0) const int BLOCK_SIZE = 32;
8+
9+
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
10+
11+
layout(binding = 0) readonly buffer Src0 { float src0[]; };
12+
layout(binding = 1) readonly buffer Src1 { float src1[]; };
13+
layout(binding = 2) buffer Dst { float dst[]; };
14+
15+
layout(push_constant) uniform PushConstants {
16+
uint src0_nb1; uint src0_nb2;
17+
uint src1_nb1;
18+
uint dst_nb0; uint dst_nb1; uint dst_nb2;
19+
uint nc; uint ncs; uint nr; uint n_t; uint n_s;
20+
};
21+
22+
void main() {
23+
const uint global_thread_id = gl_WorkGroupID.x * gl_WorkGroupSize.x + gl_LocalInvocationID.x;
24+
const uint i2 = gl_WorkGroupID.y;
25+
const uint i3 = gl_WorkGroupID.z;
26+
27+
if (global_thread_id >= nr || i2 >= n_t || i3 >= n_s) {
28+
return;
29+
}
30+
31+
const uint i1 = global_thread_id;
32+
const uint src0_base = i3 * (src0_nb2 / 4) + i2 + i1 * (src0_nb1 / 4);
33+
const uint src1_base = i1 * (src1_nb1 / 4);
34+
const uint dst_idx = i3 * (dst_nb2 / 4) + i2 * (dst_nb1 / 4) + i1;
35+
36+
float sum = 0.0;
37+
[[unroll]] for (uint i0 = 0; i0 < nc; i0++) {
38+
const uint src0_idx = src0_base + i0;
39+
const uint src1_idx = src1_base + i0;
40+
sum += src0[src0_idx] * src1[src1_idx];
41+
}
42+
43+
dst[dst_idx] = sum;
44+
}

ggml/src/ggml-vulkan/vulkan-shaders/vulkan-shaders-gen.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,8 @@ void process_shaders() {
921921

922922
string_to_spv("ssm_scan_f32", "ssm_scan.comp", {{"A_TYPE", "float"}});
923923

924+
string_to_spv("ssm_conv_f32", "ssm_conv.comp", {{"A_TYPE", "float"}});
925+
924926
for (auto &c : compiles) {
925927
c.wait();
926928
}

0 commit comments

Comments
 (0)