Skip to content

Commit effbd4f

Browse files
committed
vulkan: implement SSM scan operation
Add State Space Model scan operation to the Vulkan backend. Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 466c191 commit effbd4f

File tree

3 files changed

+302
-6
lines changed

3 files changed

+302
-6
lines changed

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

Lines changed: 175 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ struct vk_device_struct {
582582
vk_pipeline pipeline_pool2d_f32;
583583
vk_pipeline pipeline_rwkv_wkv6_f32;
584584
vk_pipeline pipeline_rwkv_wkv7_f32;
585+
vk_pipeline pipeline_ssm_scan_f32_d128;
586+
vk_pipeline pipeline_ssm_scan_f32_d256;
585587
vk_pipeline pipeline_opt_step_adamw_f32;
586588
vk_pipeline pipeline_opt_step_sgd_f32;
587589
vk_pipeline pipeline_conv2d_f32[CONV_SHAPE_COUNT];
@@ -1087,6 +1089,13 @@ struct vk_op_rwkv_wkv7_push_constants {
10871089
uint32_t C;
10881090
uint32_t H;
10891091
};
1092+
struct vk_op_ssm_scan_push_constants {
1093+
uint32_t nb02, nb03, nb12, nb13;
1094+
uint32_t nb21, nb22, nb31;
1095+
uint32_t nb42, nb43, nb52, nb53;
1096+
uint32_t s_off;
1097+
uint32_t n_head, d_head, n_group, n_tok;
1098+
};
10901099

10911100
struct vk_op_conv2d_push_constants {
10921101
uint32_t Cout;
@@ -3591,6 +3600,9 @@ static void ggml_vk_load_shaders(vk_device& device) {
35913600

35923601
ggml_vk_create_pipeline(device, device->pipeline_rwkv_wkv7_f32, "rwkv_wkv7_f32", rwkv_wkv7_f32_len, rwkv_wkv7_f32_data, "main", 8, sizeof(vk_op_rwkv_wkv7_push_constants), {1, 1, 1}, {device->subgroup_size}, 1);
35933602

3603+
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);
3604+
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);
3605+
35943606
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);
35953607

35963608
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);
@@ -8098,6 +8110,16 @@ static vk_pipeline ggml_vk_op_get_pipeline(ggml_backend_vk_context * ctx, const
80988110
return ctx->device->pipeline_rwkv_wkv7_f32;
80998111
}
81008112
return nullptr;
8113+
case GGML_OP_SSM_SCAN:
8114+
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
8115+
const uint32_t d_state = src0->ne[0];
8116+
if (d_state == 128) {
8117+
return ctx->device->pipeline_ssm_scan_f32_d128;
8118+
} else if (d_state == 256) {
8119+
return ctx->device->pipeline_ssm_scan_f32_d256;
8120+
}
8121+
}
8122+
return nullptr;
81018123
case GGML_OP_OPT_STEP_ADAMW:
81028124
if (src0->type == GGML_TYPE_F32 && dst->type == GGML_TYPE_F32) {
81038125
return ctx->device->pipeline_opt_step_adamw_f32;
@@ -9038,6 +9060,101 @@ static void ggml_vk_rwkv_wkv7(ggml_backend_vk_context * ctx, vk_context& subctx,
90389060
);
90399061
}
90409062

9063+
static void ggml_vk_ssm_scan(ggml_backend_vk_context * ctx, vk_context& subctx, ggml_tensor * dst, bool dryrun = false) {
9064+
const ggml_tensor * src0 = dst->src[0];
9065+
const ggml_tensor * src1 = dst->src[1];
9066+
const ggml_tensor * src2 = dst->src[2];
9067+
const ggml_tensor * src3 = dst->src[3];
9068+
const ggml_tensor * src4 = dst->src[4];
9069+
const ggml_tensor * src5 = dst->src[5];
9070+
9071+
GGML_ASSERT(dst->buffer != nullptr);
9072+
9073+
const uint32_t head_dim = src0->ne[1];
9074+
const uint32_t n_head = src1->ne[1];
9075+
const uint32_t n_group = src4->ne[1];
9076+
const uint32_t n_tok = src1->ne[2];
9077+
const uint32_t n_seq = src1->ne[3];
9078+
9079+
bool is_mamba2 = (src3->nb[1] == sizeof(float));
9080+
GGML_ASSERT(is_mamba2);
9081+
9082+
vk_pipeline pipeline = ggml_vk_op_get_pipeline(ctx, src0, src1, src2, dst, dst->op);
9083+
GGML_ASSERT(pipeline != nullptr);
9084+
9085+
if (dryrun) {
9086+
ggml_pipeline_request_descriptor_sets(ctx, pipeline, 1);
9087+
return;
9088+
}
9089+
9090+
const int64_t s_off = ggml_nelements(src1) * sizeof(float);
9091+
9092+
const vk_op_ssm_scan_push_constants pc = {
9093+
(uint32_t)src0->nb[2], (uint32_t)src0->nb[3],
9094+
(uint32_t)src1->nb[2], (uint32_t)src1->nb[3],
9095+
(uint32_t)src2->nb[1], (uint32_t)src2->nb[2],
9096+
(uint32_t)src3->nb[1],
9097+
(uint32_t)src4->nb[2], (uint32_t)src4->nb[3],
9098+
(uint32_t)src5->nb[2], (uint32_t)src5->nb[3],
9099+
(uint32_t)s_off,
9100+
n_head, head_dim, n_group, n_tok
9101+
};
9102+
9103+
ggml_backend_vk_buffer_context * dst_buf_ctx = (ggml_backend_vk_buffer_context *)dst->buffer->context;
9104+
ggml_backend_vk_buffer_context * src_buf_ctxs[GGML_MAX_SRC];
9105+
for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
9106+
src_buf_ctxs[i] = (ggml_backend_vk_buffer_context *)dst->src[i]->buffer->context;
9107+
}
9108+
9109+
vk_buffer d_D = nullptr, d_srcs[GGML_MAX_SRC] = { nullptr };
9110+
size_t dst_offset = 0, src_offsets[GGML_MAX_SRC] = { 0 };
9111+
bool dst_uma = false, srcs_uma[GGML_MAX_SRC] = { false };
9112+
9113+
if (ctx->device->uma) {
9114+
for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
9115+
ggml_vk_host_get(ctx->device, dst->src[i]->data, d_srcs[i], src_offsets[i]);
9116+
srcs_uma[i] = d_srcs[i] != nullptr;
9117+
}
9118+
ggml_vk_host_get(ctx->device, dst->data, d_D, dst_offset);
9119+
dst_uma = d_D != nullptr;
9120+
}
9121+
9122+
if (!dst_uma) {
9123+
d_D = dst_buf_ctx->dev_buffer;
9124+
dst_offset = vk_tensor_offset(dst) + dst->view_offs;
9125+
}
9126+
for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
9127+
if (!srcs_uma[i]) {
9128+
d_srcs[i] = src_buf_ctxs[i]->dev_buffer;
9129+
src_offsets[i] = vk_tensor_offset(dst->src[i]) + dst->src[i]->view_offs;
9130+
}
9131+
}
9132+
9133+
size_t dst_size = ggml_nbytes(dst);
9134+
size_t src_sizes[GGML_MAX_SRC];
9135+
for (int i = 0; i < GGML_MAX_SRC && dst->src[i] != nullptr; i++) {
9136+
src_sizes[i] = ggml_nbytes(dst->src[i]);
9137+
}
9138+
9139+
std::array<uint32_t, 3> elements;
9140+
9141+
const int splitH = 16;
9142+
const uint32_t num_workgroups_x = CEIL_DIV(n_head * head_dim, splitH);
9143+
const uint32_t num_workgroups_y = n_seq;
9144+
elements = { num_workgroups_x, num_workgroups_y, 1 };
9145+
9146+
ggml_vk_dispatch_pipeline(ctx, subctx, pipeline, {
9147+
vk_subbuffer{ d_srcs[0], src_offsets[0], src_sizes[0] },
9148+
vk_subbuffer{ d_srcs[1], src_offsets[1], src_sizes[1] },
9149+
vk_subbuffer{ d_srcs[2], src_offsets[2], src_sizes[2] },
9150+
vk_subbuffer{ d_srcs[3], src_offsets[3], src_sizes[3] },
9151+
vk_subbuffer{ d_srcs[4], src_offsets[4], src_sizes[4] },
9152+
vk_subbuffer{ d_srcs[5], src_offsets[5], src_sizes[5] },
9153+
vk_subbuffer{ d_srcs[6], src_offsets[6], src_sizes[6] },
9154+
vk_subbuffer{ d_D, dst_offset, dst_size }
9155+
}, pc, elements);
9156+
}
9157+
90419158
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) {
90429159
const ggml_tensor * x = dst->src[0];
90439160
const ggml_tensor * g = dst->src[1];
@@ -10870,6 +10987,7 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgr
1087010987
case GGML_OP_CONV_2D_DW:
1087110988
case GGML_OP_RWKV_WKV6:
1087210989
case GGML_OP_RWKV_WKV7:
10990+
case GGML_OP_SSM_SCAN:
1087310991
case GGML_OP_LEAKY_RELU:
1087410992
case GGML_OP_FLASH_ATTN_EXT:
1087510993
case GGML_OP_OPT_STEP_ADAMW:
@@ -11287,6 +11405,11 @@ static bool ggml_vk_build_graph(ggml_backend_vk_context * ctx, ggml_cgraph * cgr
1128711405

1128811406
break;
1128911407

11408+
case GGML_OP_SSM_SCAN:
11409+
ggml_vk_ssm_scan(ctx, compute_ctx, node, dryrun);
11410+
11411+
break;
11412+
1129011413
case GGML_OP_OPT_STEP_ADAMW:
1129111414
ggml_vk_opt_step_adamw(ctx, compute_ctx, node, dryrun);
1129211415

@@ -11398,6 +11521,7 @@ static bool ggml_vk_compute_forward(ggml_backend_vk_context * ctx, ggml_cgraph *
1139811521
case GGML_OP_CONV_2D_DW:
1139911522
case GGML_OP_RWKV_WKV6:
1140011523
case GGML_OP_RWKV_WKV7:
11524+
case GGML_OP_SSM_SCAN:
1140111525
case GGML_OP_LEAKY_RELU:
1140211526
case GGML_OP_REPEAT:
1140311527
case GGML_OP_REPEAT_BACK:
@@ -12879,6 +13003,48 @@ static bool ggml_backend_vk_device_supports_op(ggml_backend_dev_t dev, const ggm
1287913003
case GGML_OP_RWKV_WKV6:
1288013004
case GGML_OP_RWKV_WKV7:
1288113005
return true;
13006+
case GGML_OP_SSM_SCAN:
13007+
{
13008+
for (int i = 0; i < 6; i++) {
13009+
if (op->src[i] && ggml_is_quantized(op->src[i]->type)) {
13010+
return false;
13011+
}
13012+
}
13013+
if (op->src[6] && op->src[6]->type != GGML_TYPE_I32) {
13014+
return false;
13015+
}
13016+
if (op->src[0]->type != GGML_TYPE_F32 || op->type != GGML_TYPE_F32) {
13017+
return false;
13018+
}
13019+
13020+
const uint32_t d_state = op->src[0]->ne[0];
13021+
const uint32_t head_dim = op->src[0]->ne[1];
13022+
13023+
bool is_mamba2 = (op->src[3] && op->src[3]->nb[1] == sizeof(float));
13024+
if (!is_mamba2) {
13025+
return false;
13026+
}
13027+
13028+
if ((d_state != 128 && d_state != 256) || head_dim % 16 != 0) {
13029+
return false;
13030+
}
13031+
13032+
ggml_backend_vk_device_context * ctx = (ggml_backend_vk_device_context *)dev->context;
13033+
const vk_device& device = ggml_vk_get_device(ctx->device);
13034+
13035+
const uint32_t SPLIT_H = 16;
13036+
const uint32_t MAX_D_STATE = 256;
13037+
13038+
size_t stateC_size = SPLIT_H * MAX_D_STATE * sizeof(float);
13039+
size_t warp_sdata_size = MAX_D_STATE * sizeof(float);
13040+
size_t total_shared_memory = stateC_size + warp_sdata_size;
13041+
13042+
if (total_shared_memory > device->properties.limits.maxComputeSharedMemorySize) {
13043+
return false;
13044+
}
13045+
13046+
return true;
13047+
}
1288213048
case GGML_OP_CONV_TRANSPOSE_1D:
1288313049
return op->src[0]->type == GGML_TYPE_F32 && op->src[1]->type == GGML_TYPE_F32;
1288413050
case GGML_OP_CONV_2D:
@@ -13223,14 +13389,14 @@ static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph *
1322313389

1322413390
struct ggml_context * ggml_ctx = ggml_init(iparams);
1322513391

13226-
std::array<struct ggml_tensor *, 6> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
13227-
std::array<size_t, 6> src_size = {0, 0, 0, 0, 0, 0};
13228-
std::array<void *, 6> src_buffer = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
13229-
const char * srci_name[6] = {"src0", "src1", "src2", "src3", "src4", "src5"};
13392+
std::array<struct ggml_tensor *, GGML_MAX_SRC> src_clone = {nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
13393+
std::array<size_t, GGML_MAX_SRC> src_size = {};
13394+
std::array<void *, GGML_MAX_SRC> src_buffer = {};
13395+
const char * srci_name[GGML_MAX_SRC] = {"src0", "src1", "src2", "src3", "src4", "src5", "src6", "src7", "src8", "src9"};
1323013396

1323113397
struct ggml_tensor * tensor_clone = nullptr;
1323213398

13233-
for (int i = 0; i < 6; i++) {
13399+
for (int i = 0; i < GGML_MAX_SRC; i++) {
1323413400
ggml_tensor * srci = tensor->src[i];
1323513401
if (fused_rms_norm_mul) {
1323613402
rms_norm_idx = tensor->src[0]->op == GGML_OP_RMS_NORM ? 0 : 1;
@@ -13537,6 +13703,9 @@ static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph *
1353713703
src_clone[2]);
1353813704
} else if (tensor->op == GGML_OP_ADD_ID) {
1353913705
tensor_clone = ggml_add_id(ggml_ctx, src_clone[0], src_clone[1], src_clone[2]);
13706+
} else if (tensor->op == GGML_OP_SSM_SCAN) {
13707+
tensor_clone = ggml_ssm_scan(ggml_ctx, src_clone[0], src_clone[1], src_clone[2],
13708+
src_clone[3], src_clone[4], src_clone[5], src_clone[6]);
1354013709
}
1354113710
else {
1354213711
std::cerr << "Missing vk_check_results OP: " << ggml_op_name(tensor->op) << std::endl;
@@ -13558,7 +13727,7 @@ static void ggml_vk_check_results_0(ggml_backend_vk_context * ctx, ggml_cgraph *
1355813727
memcpy(comp_result, tensor_clone->data, comp_size);
1355913728
memcpy(comp_nb, tensor_clone->nb, sizeof(size_t) * GGML_MAX_DIMS);
1356013729

13561-
for (int i = 0; i < 6; i++) {
13730+
for (int i = 0; i < GGML_MAX_SRC; i++) {
1356213731
if (src_buffer[i] != nullptr) {
1356313732
free(src_buffer[i]);
1356413733
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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 D_STATE = 128;
8+
layout(constant_id = 1) const uint SUBGROUP_SIZE = 32;
9+
layout(constant_id = 2) const uint SPLIT_H = 16;
10+
11+
layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
12+
13+
layout(binding = 0) readonly buffer Src0 { float s0[]; };
14+
layout(binding = 1) readonly buffer Src1 { float x[]; };
15+
layout(binding = 2) readonly buffer Src2 { float dt[]; };
16+
layout(binding = 3) readonly buffer Src3 { float A[]; };
17+
layout(binding = 4) readonly buffer Src4 { float B[]; };
18+
layout(binding = 5) readonly buffer Src5 { float C[]; };
19+
layout(binding = 6) readonly buffer Src6 { int ids[]; };
20+
layout(binding = 7) buffer Dst { float d[]; };
21+
22+
layout(push_constant) uniform PushConstants {
23+
uint nb02; uint nb03; uint nb12; uint nb13;
24+
uint nb21; uint nb22; uint nb31;
25+
uint nb42; uint nb43; uint nb52; uint nb53;
26+
uint s_off;
27+
uint n_head;
28+
uint d_head;
29+
uint n_group;
30+
uint n_tok;
31+
};
32+
33+
float softplus(float x) {
34+
if (x <= 20.0) {
35+
return log(1.0 + exp(x));
36+
} else {
37+
return x;
38+
}
39+
}
40+
41+
shared float stateC[SPLIT_H * D_STATE];
42+
43+
void main() {
44+
const uint tid = gl_LocalInvocationID.x;
45+
const uint head_idx = (gl_WorkGroupID.x * SPLIT_H) / d_head;
46+
const uint head_off = ((gl_WorkGroupID.x * SPLIT_H) % d_head) * 4;
47+
const uint seq_idx = gl_WorkGroupID.y;
48+
49+
const uint group_off = (head_idx / (n_head / n_group)) * D_STATE * 4;
50+
const uint s0_base_idx = (uint(ids[seq_idx]) * nb03 + head_idx * nb02 + head_off * D_STATE) / 4;
51+
const uint x_base_idx = (seq_idx * nb13 + gl_WorkGroupID.x * SPLIT_H * 4) / 4;
52+
const uint dt_base_idx = (seq_idx * nb22 + head_idx * 4) / 4;
53+
const uint A_base_idx = (head_idx * nb31) / 4;
54+
const uint B_base_idx = (seq_idx * nb43 + group_off) / 4;
55+
const uint C_base_idx = (seq_idx * nb53 + group_off) / 4;
56+
const uint y_base_idx = seq_idx * n_tok * n_head * d_head + gl_WorkGroupID.x * SPLIT_H;
57+
const uint s_base_idx = (s_off + seq_idx * nb03 + head_idx * nb02 + head_off * D_STATE) / 4;
58+
59+
const uint stride_x = nb12 / 4;
60+
const uint stride_dt = nb21 / 4;
61+
const uint stride_B = nb42 / 4;
62+
const uint stride_C = nb52 / 4;
63+
const uint stride_y = n_head * d_head;
64+
65+
float state[SPLIT_H];
66+
[[unroll]] for (uint j = 0; j < SPLIT_H; j++) {
67+
state[j] = s0[s0_base_idx + j * D_STATE + tid];
68+
}
69+
70+
for (uint i = 0; i < n_tok; i++) {
71+
const float dt_soft_plus = softplus(dt[dt_base_idx + i * stride_dt]);
72+
73+
const float dA = exp(dt_soft_plus * A[A_base_idx]);
74+
75+
const float B_val = B[B_base_idx + i * stride_B + tid];
76+
const float C_val = C[C_base_idx + i * stride_C + tid];
77+
78+
[[unroll]] for (uint j = 0; j < SPLIT_H; j++) {
79+
const float x_dt = x[x_base_idx + i * stride_x + j] * dt_soft_plus;
80+
81+
state[j] = (state[j] * dA) + (B_val * x_dt);
82+
83+
stateC[j * D_STATE + tid] = state[j] * C_val;
84+
}
85+
86+
barrier();
87+
for (uint w = D_STATE; w > SUBGROUP_SIZE; w >>= 1) {
88+
[[unroll]] for (uint j = 0; j < ((w >> 1) * SPLIT_H + D_STATE - 1) / D_STATE; j++) {
89+
const uint k = (tid % (w >> 1)) +
90+
(D_STATE * (tid / (w >> 1))) +
91+
j * D_STATE * (D_STATE / (w >> 1));
92+
if (k < SPLIT_H * D_STATE && (k + (w >> 1)) < SPLIT_H * D_STATE) {
93+
stateC[k] += stateC[k + (w >> 1)];
94+
}
95+
}
96+
barrier();
97+
}
98+
99+
[[unroll]] for (uint j = 0; j < SPLIT_H / (D_STATE / SUBGROUP_SIZE); j++) {
100+
const uint idx = (tid % SUBGROUP_SIZE) +
101+
D_STATE * (tid / SUBGROUP_SIZE) +
102+
j * D_STATE * (D_STATE / SUBGROUP_SIZE);
103+
104+
uint lane = tid % SUBGROUP_SIZE;
105+
106+
[[unroll]] for (uint offset = SUBGROUP_SIZE / 2; offset > 0; offset >>= 1) {
107+
if (lane < offset) {
108+
stateC[idx] += stateC[idx + offset];
109+
}
110+
barrier();
111+
}
112+
113+
if (tid % SUBGROUP_SIZE == 0) {
114+
const uint k = tid / SUBGROUP_SIZE + j * (D_STATE / SUBGROUP_SIZE);
115+
d[y_base_idx + i * stride_y + k] = stateC[idx];
116+
}
117+
}
118+
119+
barrier();
120+
}
121+
122+
[[unroll]] for (uint j = 0; j < SPLIT_H; j++) {
123+
d[s_base_idx + j * D_STATE + tid] = state[j];
124+
}
125+
}

0 commit comments

Comments
 (0)