Skip to content

Commit 9928f4b

Browse files
committed
ggml : add ggml_ssm_conv metal impl
1 parent 7a3df79 commit 9928f4b

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

ggml/src/ggml-metal.m

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
GGML_METAL_KERNEL_TYPE_RMS_NORM,
8383
GGML_METAL_KERNEL_TYPE_GROUP_NORM,
8484
GGML_METAL_KERNEL_TYPE_NORM,
85+
GGML_METAL_KERNEL_TYPE_SSM_CONV_F32,
8586
GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32,
8687
GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16,
8788
GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32,
@@ -542,6 +543,7 @@ static void ggml_metal_log(enum ggml_log_level level, const char * format, ...){
542543
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_RMS_NORM, rms_norm, ctx->support_simdgroup_reduction);
543544
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_GROUP_NORM, group_norm, ctx->support_simdgroup_reduction);
544545
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_NORM, norm, true);
546+
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_SSM_CONV_F32, ssm_conv_f32, true);
545547
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F32_F32, mul_mv_f32_f32, ctx->support_simdgroup_reduction);
546548
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F16, mul_mv_f16_f16, ctx->support_simdgroup_reduction);
547549
GGML_METAL_ADD_KERNEL(GGML_METAL_KERNEL_TYPE_MUL_MV_F16_F32, mul_mv_f16_f32, ctx->support_simdgroup_reduction);
@@ -803,6 +805,8 @@ static bool ggml_metal_supports_op(const struct ggml_backend_metal_context * ctx
803805
return false;
804806
}
805807
return ctx->support_simdgroup_mm; // TODO: over-restricted for vec-kernels
808+
case GGML_OP_SSM_CONV:
809+
return true;
806810
case GGML_OP_MUL_MAT:
807811
case GGML_OP_MUL_MAT_ID:
808812
return ctx->support_simdgroup_reduction &&
@@ -1538,6 +1542,39 @@ static enum ggml_status ggml_metal_graph_compute(
15381542
[encoder dispatchThreadgroups:MTLSizeMake(ne00, ne01, ne02) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
15391543
}
15401544
} break;
1545+
case GGML_OP_SSM_CONV:
1546+
{
1547+
GGML_ASSERT(src0t == GGML_TYPE_F32);
1548+
GGML_ASSERT(src1t == GGML_TYPE_F32);
1549+
1550+
GGML_ASSERT(ggml_is_contiguous(src0));
1551+
GGML_ASSERT(ggml_is_contiguous(src1));
1552+
1553+
id<MTLComputePipelineState> pipeline = ctx->kernels[GGML_METAL_KERNEL_TYPE_SSM_CONV_F32].pipeline;
1554+
1555+
[encoder setComputePipelineState:pipeline];
1556+
[encoder setBuffer:id_src0 offset:offs_src0 atIndex:0];
1557+
[encoder setBuffer:id_src1 offset:offs_src1 atIndex:1];
1558+
[encoder setBuffer:id_dst offset:offs_dst atIndex:2];
1559+
[encoder setBytes:&ne00 length:sizeof(ne00) atIndex:3];
1560+
[encoder setBytes:&ne01 length:sizeof(ne01) atIndex:4];
1561+
[encoder setBytes:&ne02 length:sizeof(ne02) atIndex:5];
1562+
[encoder setBytes:&nb00 length:sizeof(nb00) atIndex:6];
1563+
[encoder setBytes:&nb01 length:sizeof(nb01) atIndex:7];
1564+
[encoder setBytes:&nb02 length:sizeof(nb02) atIndex:8];
1565+
[encoder setBytes:&ne10 length:sizeof(ne10) atIndex:9];
1566+
[encoder setBytes:&ne11 length:sizeof(ne11) atIndex:10];
1567+
[encoder setBytes:&nb10 length:sizeof(nb10) atIndex:11];
1568+
[encoder setBytes:&nb11 length:sizeof(nb11) atIndex:12];
1569+
[encoder setBytes:&ne0 length:sizeof(ne0) atIndex:13];
1570+
[encoder setBytes:&ne1 length:sizeof(ne1) atIndex:14];
1571+
[encoder setBytes:&ne2 length:sizeof(ne2) atIndex:15];
1572+
[encoder setBytes:&nb0 length:sizeof(nb0) atIndex:16];
1573+
[encoder setBytes:&nb1 length:sizeof(nb1) atIndex:17];
1574+
[encoder setBytes:&nb2 length:sizeof(nb2) atIndex:18];
1575+
1576+
[encoder dispatchThreadgroups:MTLSizeMake(ne01, ne1, ne02) threadsPerThreadgroup:MTLSizeMake(1, 1, 1)];
1577+
} break;
15411578
case GGML_OP_MUL_MAT:
15421579
{
15431580
GGML_ASSERT(ne00 == ne10);

ggml/src/ggml-metal.metal

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,54 @@ kernel void kernel_diag_mask_inf_8(
667667
}
668668
}
669669

670+
// ref: ggml.c:ggml_compute_forward_ssm_conv_f32
671+
// TODO: optimize
672+
kernel void kernel_ssm_conv_f32(
673+
device const void * src0,
674+
device const void * src1,
675+
device float * dst,
676+
constant int64_t & ne00,
677+
constant int64_t & ne01,
678+
constant int64_t & ne02,
679+
constant uint64_t & nb00,
680+
constant uint64_t & nb01,
681+
constant uint64_t & nb02,
682+
constant int64_t & ne10,
683+
constant int64_t & ne11,
684+
constant uint64_t & nb10,
685+
constant uint64_t & nb11,
686+
constant int64_t & ne0,
687+
constant int64_t & ne1,
688+
constant int64_t & ne2,
689+
constant uint64_t & nb0,
690+
constant uint64_t & nb1,
691+
constant uint64_t & nb2,
692+
uint3 tgpig[[threadgroup_position_in_grid]],
693+
uint3 tpitg[[thread_position_in_threadgroup]],
694+
uint3 ntg[[threads_per_threadgroup]]) {
695+
const int64_t ir = tgpig.x;
696+
const int64_t i2 = tgpig.y;
697+
const int64_t i3 = tgpig.z;
698+
699+
const int64_t nc = ne10;
700+
const int64_t ncs = ne00;
701+
const int64_t nr = ne01;
702+
const int64_t n_t = ne1;
703+
const int64_t n_s = ne2;
704+
705+
device const float * s = (device const float *) ((device const char *) src0 + ir*nb01 + i2*nb00 + i3*nb02);
706+
device const float * c = (device const float *) ((device const char *) src1 + ir*nb11);
707+
device float * x = (device float *) ((device char *) dst + ir*nb0 + i2*nb1 + i3*nb2);
708+
709+
float sumf = 0.0f;
710+
711+
for (int64_t i0 = 0; i0 < nc; ++i0) {
712+
sumf += s[i0] * c[i0];
713+
}
714+
715+
x[0] = sumf;
716+
}
717+
670718
kernel void kernel_norm(
671719
device const void * src0,
672720
device float * dst,

tests/test-backend-ops.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,29 @@ struct test_rms_norm : public test_case {
949949
}
950950
};
951951

952+
// GGML_OP_SSM_CONV
953+
struct test_ssm_conv : public test_case {
954+
const ggml_type type;
955+
const std::array<int64_t, 4> ne_a;
956+
const std::array<int64_t, 4> ne_b;
957+
958+
std::string vars() override {
959+
return VARS_TO_STR3(type, ne_a, ne_b);
960+
}
961+
962+
test_ssm_conv(ggml_type type = GGML_TYPE_F32,
963+
std::array<int64_t, 4> ne_a = {10, 10, 10, 1},
964+
std::array<int64_t, 4> ne_b = {3, 3, 1, 1})
965+
: type(type), ne_a(ne_a), ne_b(ne_b) {}
966+
967+
ggml_tensor * build_graph(ggml_context * ctx) override {
968+
ggml_tensor * a = ggml_new_tensor(ctx, type, 4, ne_a.data());
969+
ggml_tensor * b = ggml_new_tensor(ctx, type, 4, ne_b.data());
970+
ggml_tensor * out = ggml_ssm_conv(ctx, a, b);
971+
return out;
972+
}
973+
};
974+
952975
// GGML_OP_MUL_MAT
953976
struct test_mul_mat : public test_case {
954977
const ggml_type type_a;
@@ -2240,6 +2263,10 @@ static bool test_backend(ggml_backend_t backend, test_mode mode, const char * op
22402263
test_cases.emplace_back(new test_rms_norm(GGML_TYPE_F32, {64, 10, 10, 10}, eps));
22412264
}
22422265

2266+
test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 1536, 1, 1}, {4, 1536, 1, 1}));
2267+
test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {8, 1536, 1, 1}, {4, 1536, 1, 1}));
2268+
test_cases.emplace_back(new test_ssm_conv(GGML_TYPE_F32, {4, 1536, 4, 1}, {4, 1536, 1, 1}));
2269+
22432270
#if 1
22442271
for (ggml_type type_a : base_types) {
22452272
for (ggml_type type_b : {GGML_TYPE_F32, GGML_TYPE_F16}) {

0 commit comments

Comments
 (0)