Skip to content

Commit 27b1879

Browse files
am17anqnixsynapse
authored andcommitted
Add Conv2d for CPU (ggml-org#14388)
* Conv2D: Add CPU version * Half decent * Tiled approach for F32 * remove file * Fix tests * Support F16 operations * add assert about size * Review: further formatting fixes, add assert and use CPU version of fp32->fp16
1 parent 82f6275 commit 27b1879

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

ggml/src/ggml.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,7 @@ static const char * GGML_OP_NAME[GGML_OP_COUNT] = {
945945
"CONV_TRANSPOSE_1D",
946946
"IM2COL",
947947
"IM2COL_BACK",
948+
"CONV_2D",
948949
"CONV_2D_DW",
949950
"CONV_TRANSPOSE_2D",
950951
"POOL_1D",
@@ -986,7 +987,7 @@ static const char * GGML_OP_NAME[GGML_OP_COUNT] = {
986987
"GLU",
987988
};
988989

989-
static_assert(GGML_OP_COUNT == 85, "GGML_OP_COUNT != 85");
990+
static_assert(GGML_OP_COUNT == 86, "GGML_OP_COUNT != 86");
990991

991992
static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = {
992993
"none",
@@ -1044,6 +1045,7 @@ static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = {
10441045
"conv_transpose_1d(x)",
10451046
"im2col(x)",
10461047
"im2col_back(x)",
1048+
"conv_2d(x)",
10471049
"conv_2d_dw(x)",
10481050
"conv_transpose_2d(x)",
10491051
"pool_1d(x)",
@@ -1085,7 +1087,7 @@ static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = {
10851087
"glu(x)",
10861088
};
10871089

1088-
static_assert(GGML_OP_COUNT == 85, "GGML_OP_COUNT != 85");
1090+
static_assert(GGML_OP_COUNT == 86, "GGML_OP_COUNT != 86");
10891091

10901092
static_assert(GGML_OP_POOL_COUNT == 2, "GGML_OP_POOL_COUNT != 2");
10911093

@@ -4291,6 +4293,44 @@ struct ggml_tensor * ggml_conv_2d_dw_direct(
42914293
return result;
42924294
}
42934295

4296+
// ggml_conv_2d_direct
4297+
4298+
struct ggml_tensor * ggml_conv_2d_direct(
4299+
struct ggml_context * ctx,
4300+
struct ggml_tensor * a, // convolution kernel [KW, KH, IC, OC]
4301+
struct ggml_tensor * b, // input data [W, H, C, N]
4302+
int s0, // stride dimension 0
4303+
int s1, // stride dimension 1
4304+
int p0, // padding dimension 0
4305+
int p1, // padding dimension 1
4306+
int d0, // dilation dimension 0
4307+
int d1) {// dilation dimension 1
4308+
4309+
GGML_ASSERT(a->ne[2] == b->ne[2]);
4310+
//GGML_ASSERT(a->type == b->type);
4311+
4312+
int64_t ne[4];
4313+
ne[0] = ggml_calc_conv_output_size(b->ne[0], a->ne[0], s0, p0, d0);
4314+
ne[1] = ggml_calc_conv_output_size(b->ne[1], a->ne[1], s1, p1, d1);
4315+
ne[2] = a->ne[3];
4316+
ne[3] = b->ne[3];
4317+
4318+
struct ggml_tensor * result = ggml_new_tensor(ctx, b->type, 4, ne);
4319+
4320+
ggml_set_op_params_i32(result, 0, s0);
4321+
ggml_set_op_params_i32(result, 1, s1);
4322+
ggml_set_op_params_i32(result, 2, p0);
4323+
ggml_set_op_params_i32(result, 3, p1);
4324+
ggml_set_op_params_i32(result, 4, d0);
4325+
ggml_set_op_params_i32(result, 5, d1);
4326+
4327+
result->op = GGML_OP_CONV_2D;
4328+
result->src[0] = a;
4329+
result->src[1] = b;
4330+
4331+
return result;
4332+
}
4333+
42944334
// ggml_conv_transpose_2d_p0
42954335

42964336
static int64_t ggml_calc_conv_transpose_output_size(int64_t ins, int64_t ks, int s, int p) {

0 commit comments

Comments
 (0)