Skip to content

Commit c331320

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 effbd4f commit c331320

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 nb01, nb02;
1102+
uint32_t nb11;
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;
@@ -3603,6 +3610,8 @@ static void ggml_vk_load_shaders(vk_device& device) {
36033610
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);
36043611
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);
36053612

3613+
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), {32, 1, 1}, {32}, 1);
3614+
36063615
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);
36073616

36083617
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);
@@ -8120,6 +8129,11 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
81208129
}
81218130
}
81228131
return nullptr;
8132+
case GGML_OP_SSM_CONV:
8133+
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
8134+
return ctx->device->pipeline_ssm_conv_f32;
8135+
}
8136+
return nullptr;
81238137
case GGML_OP_OPT_STEP_ADAMW:
81248138
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
81258139
return ctx->device->pipeline_opt_step_adamw_f32;
@@ -8614,6 +8628,14 @@ static void ggml_vk_op_f32(ggml_backend_vk_context * ctx, vk_context& subctx, co
86148628
}
86158629
}
86168630
break;
8631+
case GGML_OP_SSM_CONV:
8632+
{
8633+
const uint32_t nr = src0->ne[1];
8634+
const uint32_t n_t = dst->ne[1];
8635+
const uint32_t n_s = dst->ne[2];
8636+
elements = { nr, n_t, n_s };
8637+
}
8638+
break;
86178639
default:
86188640
elements = { (uint32_t)ggml_nelements(src0), 1, 1 };
86198641
break;
@@ -9155,6 +9177,22 @@ static void ggml_vk_ssm_scan(ggml_backend_vk_context * ctx, vk_context& subctx,
91559177
}, pc, elements);
91569178
}
91579179

9180+
static void ggml_vk_ssm_conv(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
9181+
const ggml_tensor * src0 = dst->src[0];
9182+
const ggml_tensor * src1 = dst->src[1];
9183+
9184+
ggml_vk_op_f32<vk_op_ssm_conv_push_constants>(ctx, subctx, src0, src1, nullptr, dst, GGML_OP_SSM_CONV, {
9185+
(uint32_t)src0->nb[1], (uint32_t)src0->nb[2],
9186+
(uint32_t)src1->nb[1],
9187+
(uint32_t)dst->nb[0], (uint32_t)dst->nb[1], (uint32_t)dst->nb[2],
9188+
(uint32_t)src1->ne[0],
9189+
(uint32_t)src0->ne[0],
9190+
(uint32_t)src0->ne[1],
9191+
(uint32_t)dst->ne[1],
9192+
(uint32_t)dst->ne[2],
9193+
}, dryrun);
9194+
}
9195+
91589196
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) {
91599197
const ggml_tensor * x = dst->src[0];
91609198
const ggml_tensor * g = dst->src[1];
@@ -10988,6 +11026,7 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgr
1098811026
case GGML_OP_RWKV_WKV6:
1098911027
case GGML_OP_RWKV_WKV7:
1099011028
case GGML_OP_SSM_SCAN:
11029+
case GGML_OP_SSM_CONV:
1099111030
case GGML_OP_LEAKY_RELU:
1099211031
case GGML_OP_FLASH_ATTN_EXT:
1099311032
case GGML_OP_OPT_STEP_ADAMW:
@@ -11410,6 +11449,11 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgr
1141011449

1141111450
break;
1141211451

11452+
case GGML_OP_SSM_CONV:
11453+
ggml_vk_ssm_conv(ctx, compute_ctx, node, dryrun);
11454+
11455+
break;
11456+
1141311457
case GGML_OP_OPT_STEP_ADAMW:
1141411458
ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
1141511459

@@ -11522,6 +11566,7 @@ static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_cgraph *
1152211566
case GGML_OP_RWKV_WKV6:
1152311567
case GGML_OP_RWKV_WKV7:
1152411568
case GGML_OP_SSM_SCAN:
11569+
case GGML_OP_SSM_CONV:
1152511570
case GGML_OP_LEAKY_RELU:
1152611571
case GGML_OP_REPEAT:
1152711572
case GGML_OP_REPEAT_BACK:
@@ -13045,6 +13090,8 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
1304513090

1304613091
return true;
1304713092
}
13093+
case GGML_OP_SSM_CONV:
13094+
return true;
1304813095
case GGML_OP_CONV_TRANSPOSE_1D:
1304913096
return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
1305013097
case GGML_OP_CONV_2D:
@@ -13706,6 +13753,8 @@ static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph *
1370613753
} else if (tensor->op == GGML_OP_SSM_SCAN) {
1370713754
tensor_clone = ggml_ssm_scan(ggml_ctx, src_clone[0], src_clone[1], src_clone[2],
1370813755
src_clone[3], src_clone[4], src_clone[5], src_clone[6]);
13756+
} else if (tensor->op == GGML_OP_SSM_CONV) {
13757+
tensor_clone = ggml_ssm_conv(ggml_ctx, src_clone[0], src_clone[1]);
1370913758
}
1371013759
else {
1371113760
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 uint 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 nb01; uint nb02;
17+
uint nb11;
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_GlobalInvocationID.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 * (nb02 / 4) + i2 + i1 * (nb01 / 4);
33+
const uint src1_base = i1 * (nb11 / 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
@@ -918,6 +918,8 @@ void process_shaders() {
918918

919919
string_to_spv("ssm_scan_f32", "ssm_scan.comp", {{"A_TYPE", "float"}});
920920

921+
string_to_spv("ssm_conv_f32", "ssm_conv.comp", {{"A_TYPE", "float"}});
922+
921923
for (auto &c : compiles) {
922924
c.wait();
923925
}

0 commit comments

Comments
 (0)