Diego Devesa commited on
Commit
ba7a5f8
·
1 Parent(s): 3d4b079

ggml : add more generic custom op, remove deprecated custom ops (ggml/1183)

Browse files

* ggml : add more generic ggml_custom op

* ggml : remove deprecated custom ops

ggml/include/ggml.h CHANGED
@@ -507,17 +507,12 @@ extern "C" {
507
 
508
  GGML_OP_UNARY,
509
 
510
- GGML_OP_MAP_UNARY,
511
- GGML_OP_MAP_BINARY,
512
-
513
- GGML_OP_MAP_CUSTOM1_F32,
514
- GGML_OP_MAP_CUSTOM2_F32,
515
- GGML_OP_MAP_CUSTOM3_F32,
516
-
517
  GGML_OP_MAP_CUSTOM1,
518
  GGML_OP_MAP_CUSTOM2,
519
  GGML_OP_MAP_CUSTOM3,
520
 
 
 
521
  GGML_OP_CROSS_ENTROPY_LOSS,
522
  GGML_OP_CROSS_ENTROPY_LOSS_BACK,
523
  GGML_OP_OPT_STEP_ADAMW,
@@ -1916,83 +1911,6 @@ extern "C" {
1916
 
1917
  // custom operators
1918
 
1919
- typedef void (*ggml_unary_op_f32_t) (const int, float *, const float *);
1920
- typedef void (*ggml_binary_op_f32_t)(const int, float *, const float *, const float *);
1921
-
1922
- typedef void (*ggml_custom1_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *);
1923
- typedef void (*ggml_custom2_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
1924
- typedef void (*ggml_custom3_op_f32_t)(struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *, const struct ggml_tensor *);
1925
-
1926
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_f32(
1927
- struct ggml_context * ctx,
1928
- struct ggml_tensor * a,
1929
- ggml_unary_op_f32_t fun),
1930
- "use ggml_map_custom1 instead");
1931
-
1932
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_unary_inplace_f32(
1933
- struct ggml_context * ctx,
1934
- struct ggml_tensor * a,
1935
- ggml_unary_op_f32_t fun),
1936
- "use ggml_map_custom1_inplace instead");
1937
-
1938
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_f32(
1939
- struct ggml_context * ctx,
1940
- struct ggml_tensor * a,
1941
- struct ggml_tensor * b,
1942
- ggml_binary_op_f32_t fun),
1943
- "use ggml_map_custom2 instead");
1944
-
1945
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_binary_inplace_f32(
1946
- struct ggml_context * ctx,
1947
- struct ggml_tensor * a,
1948
- struct ggml_tensor * b,
1949
- ggml_binary_op_f32_t fun),
1950
- "use ggml_map_custom2_inplace instead");
1951
-
1952
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_f32(
1953
- struct ggml_context * ctx,
1954
- struct ggml_tensor * a,
1955
- ggml_custom1_op_f32_t fun),
1956
- "use ggml_map_custom1 instead");
1957
-
1958
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom1_inplace_f32(
1959
- struct ggml_context * ctx,
1960
- struct ggml_tensor * a,
1961
- ggml_custom1_op_f32_t fun),
1962
- "use ggml_map_custom1_inplace instead");
1963
-
1964
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_f32(
1965
- struct ggml_context * ctx,
1966
- struct ggml_tensor * a,
1967
- struct ggml_tensor * b,
1968
- ggml_custom2_op_f32_t fun),
1969
- "use ggml_map_custom2 instead");
1970
-
1971
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom2_inplace_f32(
1972
- struct ggml_context * ctx,
1973
- struct ggml_tensor * a,
1974
- struct ggml_tensor * b,
1975
- ggml_custom2_op_f32_t fun),
1976
- "use ggml_map_custom2_inplace instead");
1977
-
1978
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_f32(
1979
- struct ggml_context * ctx,
1980
- struct ggml_tensor * a,
1981
- struct ggml_tensor * b,
1982
- struct ggml_tensor * c,
1983
- ggml_custom3_op_f32_t fun),
1984
- "use ggml_map_custom3 instead");
1985
-
1986
- GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_map_custom3_inplace_f32(
1987
- struct ggml_context * ctx,
1988
- struct ggml_tensor * a,
1989
- struct ggml_tensor * b,
1990
- struct ggml_tensor * c,
1991
- ggml_custom3_op_f32_t fun),
1992
- "use ggml_map_custom3_inplace instead");
1993
-
1994
- // custom operators v2
1995
-
1996
  typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
1997
  typedef void (*ggml_custom2_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata);
1998
  typedef void (*ggml_custom3_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata);
@@ -2048,6 +1966,30 @@ extern "C" {
2048
  int n_tasks,
2049
  void * userdata);
2050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2051
  // loss function
2052
 
2053
  GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
 
507
 
508
  GGML_OP_UNARY,
509
 
 
 
 
 
 
 
 
510
  GGML_OP_MAP_CUSTOM1,
511
  GGML_OP_MAP_CUSTOM2,
512
  GGML_OP_MAP_CUSTOM3,
513
 
514
+ GGML_OP_CUSTOM,
515
+
516
  GGML_OP_CROSS_ENTROPY_LOSS,
517
  GGML_OP_CROSS_ENTROPY_LOSS_BACK,
518
  GGML_OP_OPT_STEP_ADAMW,
 
1911
 
1912
  // custom operators
1913
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1914
  typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
1915
  typedef void (*ggml_custom2_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, int ith, int nth, void * userdata);
1916
  typedef void (*ggml_custom3_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, const struct ggml_tensor * b, const struct ggml_tensor * c, int ith, int nth, void * userdata);
 
1966
  int n_tasks,
1967
  void * userdata);
1968
 
1969
+ typedef void (*ggml_custom_op_t)(struct ggml_tensor * dst , int ith, int nth, void * userdata);
1970
+
1971
+ GGML_API struct ggml_tensor * ggml_custom_4d(
1972
+ struct ggml_context * ctx,
1973
+ enum ggml_type type,
1974
+ int64_t ne0,
1975
+ int64_t ne1,
1976
+ int64_t ne2,
1977
+ int64_t ne3,
1978
+ struct ggml_tensor ** args,
1979
+ int n_args,
1980
+ ggml_custom_op_t fun,
1981
+ int n_tasks,
1982
+ void * userdata);
1983
+
1984
+ GGML_API struct ggml_tensor * ggml_custom_inplace(
1985
+ struct ggml_context * ctx,
1986
+ struct ggml_tensor * a,
1987
+ struct ggml_tensor ** args,
1988
+ int n_args,
1989
+ ggml_custom_op_t fun,
1990
+ int n_tasks,
1991
+ void * userdata);
1992
+
1993
  // loss function
1994
 
1995
  GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
ggml/src/ggml-cpu/ggml-cpu.c CHANGED
@@ -2027,41 +2027,6 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
2027
  {
2028
  ggml_compute_forward_rwkv_wkv7(params, tensor);
2029
  } break;
2030
- case GGML_OP_MAP_UNARY:
2031
- {
2032
- ggml_unary_op_f32_t fun;
2033
- memcpy(&fun, tensor->op_params, sizeof(fun));
2034
- ggml_compute_forward_map_unary(params, tensor, fun);
2035
- }
2036
- break;
2037
- case GGML_OP_MAP_BINARY:
2038
- {
2039
- ggml_binary_op_f32_t fun;
2040
- memcpy(&fun, tensor->op_params, sizeof(fun));
2041
- ggml_compute_forward_map_binary(params, tensor, fun);
2042
- }
2043
- break;
2044
- case GGML_OP_MAP_CUSTOM1_F32:
2045
- {
2046
- ggml_custom1_op_f32_t fun;
2047
- memcpy(&fun, tensor->op_params, sizeof(fun));
2048
- ggml_compute_forward_map_custom1_f32(params, tensor, fun);
2049
- }
2050
- break;
2051
- case GGML_OP_MAP_CUSTOM2_F32:
2052
- {
2053
- ggml_custom2_op_f32_t fun;
2054
- memcpy(&fun, tensor->op_params, sizeof(fun));
2055
- ggml_compute_forward_map_custom2_f32(params, tensor, fun);
2056
- }
2057
- break;
2058
- case GGML_OP_MAP_CUSTOM3_F32:
2059
- {
2060
- ggml_custom3_op_f32_t fun;
2061
- memcpy(&fun, tensor->op_params, sizeof(fun));
2062
- ggml_compute_forward_map_custom3_f32(params, tensor, fun);
2063
- }
2064
- break;
2065
  case GGML_OP_MAP_CUSTOM1:
2066
  {
2067
  ggml_compute_forward_map_custom1(params, tensor);
@@ -2077,6 +2042,11 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
2077
  ggml_compute_forward_map_custom3(params, tensor);
2078
  }
2079
  break;
 
 
 
 
 
2080
  case GGML_OP_CROSS_ENTROPY_LOSS:
2081
  {
2082
  ggml_compute_forward_cross_entropy_loss(params, tensor);
@@ -2328,11 +2298,6 @@ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
2328
  case GGML_OP_WIN_PART:
2329
  case GGML_OP_WIN_UNPART:
2330
  case GGML_OP_GET_REL_POS:
2331
- case GGML_OP_MAP_UNARY:
2332
- case GGML_OP_MAP_BINARY:
2333
- case GGML_OP_MAP_CUSTOM1_F32:
2334
- case GGML_OP_MAP_CUSTOM2_F32:
2335
- case GGML_OP_MAP_CUSTOM3_F32:
2336
  {
2337
  n_tasks = 1;
2338
  } break;
@@ -2366,6 +2331,16 @@ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
2366
  n_tasks = MIN(p.n_tasks, n_threads);
2367
  }
2368
  } break;
 
 
 
 
 
 
 
 
 
 
2369
  case GGML_OP_CROSS_ENTROPY_LOSS:
2370
  case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
2371
  case GGML_OP_OPT_STEP_ADAMW:
 
2027
  {
2028
  ggml_compute_forward_rwkv_wkv7(params, tensor);
2029
  } break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2030
  case GGML_OP_MAP_CUSTOM1:
2031
  {
2032
  ggml_compute_forward_map_custom1(params, tensor);
 
2042
  ggml_compute_forward_map_custom3(params, tensor);
2043
  }
2044
  break;
2045
+ case GGML_OP_CUSTOM:
2046
+ {
2047
+ ggml_compute_forward_custom(params, tensor);
2048
+ }
2049
+ break;
2050
  case GGML_OP_CROSS_ENTROPY_LOSS:
2051
  {
2052
  ggml_compute_forward_cross_entropy_loss(params, tensor);
 
2298
  case GGML_OP_WIN_PART:
2299
  case GGML_OP_WIN_UNPART:
2300
  case GGML_OP_GET_REL_POS:
 
 
 
 
 
2301
  {
2302
  n_tasks = 1;
2303
  } break;
 
2331
  n_tasks = MIN(p.n_tasks, n_threads);
2332
  }
2333
  } break;
2334
+ case GGML_OP_CUSTOM:
2335
+ {
2336
+ struct ggml_custom_op_params p;
2337
+ memcpy(&p, node->op_params, sizeof(p));
2338
+ if (p.n_tasks == GGML_N_TASKS_MAX) {
2339
+ n_tasks = n_threads;
2340
+ } else {
2341
+ n_tasks = MIN(p.n_tasks, n_threads);
2342
+ }
2343
+ } break;
2344
  case GGML_OP_CROSS_ENTROPY_LOSS:
2345
  case GGML_OP_CROSS_ENTROPY_LOSS_BACK:
2346
  case GGML_OP_OPT_STEP_ADAMW:
ggml/src/ggml-cpu/ops.cpp CHANGED
@@ -8264,152 +8264,6 @@ void ggml_compute_forward_rwkv_wkv7(
8264
  }
8265
  }
8266
 
8267
- // ggml_compute_forward_map_unary
8268
-
8269
- static void ggml_compute_forward_map_unary_f32(
8270
- const ggml_compute_params * params,
8271
- ggml_tensor * dst,
8272
- const ggml_unary_op_f32_t fun) {
8273
-
8274
- const ggml_tensor * src0 = dst->src[0];
8275
-
8276
- if (params->ith != 0) {
8277
- return;
8278
- }
8279
-
8280
- assert(ggml_is_contiguous_1(src0));
8281
- assert(ggml_is_contiguous_1(dst));
8282
- assert(ggml_are_same_shape(src0, dst));
8283
-
8284
- const int n = ggml_nrows(src0);
8285
- const int nc = src0->ne[0];
8286
-
8287
- for (int i = 0; i < n; i++) {
8288
- fun(nc,
8289
- (float *) ((char *) dst->data + i*( dst->nb[1])),
8290
- (float *) ((char *) src0->data + i*(src0->nb[1])));
8291
- }
8292
- }
8293
-
8294
- void ggml_compute_forward_map_unary(
8295
- const ggml_compute_params * params,
8296
- ggml_tensor * dst,
8297
- const ggml_unary_op_f32_t fun) {
8298
-
8299
- const ggml_tensor * src0 = dst->src[0];
8300
-
8301
- switch (src0->type) {
8302
- case GGML_TYPE_F32:
8303
- {
8304
- ggml_compute_forward_map_unary_f32(params, dst, fun);
8305
- } break;
8306
- default:
8307
- {
8308
- GGML_ABORT("fatal error");
8309
- }
8310
- }
8311
- }
8312
-
8313
- // ggml_compute_forward_map_binary
8314
-
8315
- static void ggml_compute_forward_map_binary_f32(
8316
- const ggml_compute_params * params,
8317
- ggml_tensor * dst,
8318
- const ggml_binary_op_f32_t fun) {
8319
-
8320
- const ggml_tensor * src0 = dst->src[0];
8321
- const ggml_tensor * src1 = dst->src[1];
8322
-
8323
- if (params->ith != 0) {
8324
- return;
8325
- }
8326
-
8327
- assert(ggml_is_contiguous_1(src0));
8328
- assert(ggml_is_contiguous_1(src1));
8329
- assert(ggml_is_contiguous_1(dst));
8330
- assert(ggml_are_same_shape(src0, src1) && ggml_are_same_shape(src0, dst));
8331
-
8332
- const int n = ggml_nrows(src0);
8333
- const int nc = src0->ne[0];
8334
-
8335
- for (int i = 0; i < n; i++) {
8336
- fun(nc,
8337
- (float *) ((char *) dst->data + i*( dst->nb[1])),
8338
- (float *) ((char *) src0->data + i*(src0->nb[1])),
8339
- (float *) ((char *) src1->data + i*(src1->nb[1])));
8340
- }
8341
- }
8342
-
8343
- void ggml_compute_forward_map_binary(
8344
- const ggml_compute_params * params,
8345
- ggml_tensor * dst,
8346
- const ggml_binary_op_f32_t fun) {
8347
-
8348
- const ggml_tensor * src0 = dst->src[0];
8349
-
8350
- switch (src0->type) {
8351
- case GGML_TYPE_F32:
8352
- {
8353
- ggml_compute_forward_map_binary_f32(params, dst, fun);
8354
- } break;
8355
- default:
8356
- {
8357
- GGML_ABORT("fatal error");
8358
- }
8359
- }
8360
- }
8361
-
8362
- // ggml_compute_forward_map_custom1
8363
-
8364
- void ggml_compute_forward_map_custom1_f32(
8365
- const ggml_compute_params * params,
8366
- ggml_tensor * dst,
8367
- const ggml_custom1_op_f32_t fun) {
8368
-
8369
- const ggml_tensor * a = dst->src[0];
8370
-
8371
- if (params->ith != 0) {
8372
- return;
8373
- }
8374
-
8375
- fun(dst, a);
8376
- }
8377
-
8378
- // ggml_compute_forward_map_custom2
8379
-
8380
- void ggml_compute_forward_map_custom2_f32(
8381
- const ggml_compute_params * params,
8382
- ggml_tensor * dst,
8383
- const ggml_custom2_op_f32_t fun) {
8384
-
8385
- const ggml_tensor * a = dst->src[0];
8386
- const ggml_tensor * b = dst->src[1];
8387
-
8388
- if (params->ith != 0) {
8389
- return;
8390
- }
8391
-
8392
- fun(dst, a, b);
8393
- }
8394
-
8395
- // ggml_compute_forward_map_custom3
8396
-
8397
- void ggml_compute_forward_map_custom3_f32(
8398
- const ggml_compute_params * params,
8399
- ggml_tensor * dst,
8400
- const ggml_custom3_op_f32_t fun) {
8401
-
8402
- const ggml_tensor * a = dst->src[0];
8403
- const ggml_tensor * b = dst->src[1];
8404
- const ggml_tensor * c = dst->src[1];
8405
-
8406
- if (params->ith != 0) {
8407
- return;
8408
- }
8409
-
8410
- fun(dst, a, b, c);
8411
- }
8412
-
8413
  // ggml_compute_forward_map_custom1
8414
 
8415
  void ggml_compute_forward_map_custom1(
@@ -8455,6 +8309,18 @@ void ggml_compute_forward_map_custom3(
8455
  p.fun(dst, a, b, c, params->ith, params->nth, p.userdata);
8456
  }
8457
 
 
 
 
 
 
 
 
 
 
 
 
 
8458
  // ggml_compute_forward_cross_entropy_loss
8459
 
8460
  static void ggml_compute_forward_cross_entropy_loss_f32(
 
8264
  }
8265
  }
8266
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8267
  // ggml_compute_forward_map_custom1
8268
 
8269
  void ggml_compute_forward_map_custom1(
 
8309
  p.fun(dst, a, b, c, params->ith, params->nth, p.userdata);
8310
  }
8311
 
8312
+ // ggml_compute_forward_custom
8313
+
8314
+ void ggml_compute_forward_custom(
8315
+ const struct ggml_compute_params * params,
8316
+ struct ggml_tensor * dst) {
8317
+
8318
+ struct ggml_custom_op_params p;
8319
+ memcpy(&p, dst->op_params, sizeof(p));
8320
+
8321
+ p.fun(dst, params->ith, params->nth, p.userdata);
8322
+ }
8323
+
8324
  // ggml_compute_forward_cross_entropy_loss
8325
 
8326
  static void ggml_compute_forward_cross_entropy_loss_f32(
ggml/src/ggml-cpu/ops.h CHANGED
@@ -96,29 +96,10 @@ void ggml_compute_forward_add_rel_pos(const struct ggml_compute_params * params,
96
  void ggml_compute_forward_rwkv_wkv6(const struct ggml_compute_params * params, struct ggml_tensor * dst);
97
  void ggml_compute_forward_rwkv_wkv7(const struct ggml_compute_params * params, struct ggml_tensor * dst);
98
  void ggml_compute_forward_gla(const struct ggml_compute_params * params, struct ggml_tensor * dst);
99
- void ggml_compute_forward_map_unary(
100
- const struct ggml_compute_params * params,
101
- struct ggml_tensor * dst,
102
- const ggml_unary_op_f32_t fun);
103
- void ggml_compute_forward_map_binary(
104
- const struct ggml_compute_params * params,
105
- struct ggml_tensor * dst,
106
- const ggml_binary_op_f32_t fun);
107
- void ggml_compute_forward_map_custom1_f32(
108
- const struct ggml_compute_params * params,
109
- struct ggml_tensor * dst,
110
- const ggml_custom1_op_f32_t fun);
111
- void ggml_compute_forward_map_custom2_f32(
112
- const struct ggml_compute_params * params,
113
- struct ggml_tensor * dst,
114
- const ggml_custom2_op_f32_t fun);
115
- void ggml_compute_forward_map_custom3_f32(
116
- const struct ggml_compute_params * params,
117
- struct ggml_tensor * dst,
118
- const ggml_custom3_op_f32_t fun);
119
  void ggml_compute_forward_map_custom1(const struct ggml_compute_params * params, struct ggml_tensor * dst);
120
  void ggml_compute_forward_map_custom2(const struct ggml_compute_params * params, struct ggml_tensor * dst);
121
  void ggml_compute_forward_map_custom3(const struct ggml_compute_params * params, struct ggml_tensor * dst);
 
122
  void ggml_compute_forward_cross_entropy_loss(const struct ggml_compute_params * params, struct ggml_tensor * dst);
123
  void ggml_compute_forward_cross_entropy_loss_back(const struct ggml_compute_params * params, struct ggml_tensor * dst);
124
  void ggml_compute_forward_opt_step_adamw(const struct ggml_compute_params * params, struct ggml_tensor * dst);
 
96
  void ggml_compute_forward_rwkv_wkv6(const struct ggml_compute_params * params, struct ggml_tensor * dst);
97
  void ggml_compute_forward_rwkv_wkv7(const struct ggml_compute_params * params, struct ggml_tensor * dst);
98
  void ggml_compute_forward_gla(const struct ggml_compute_params * params, struct ggml_tensor * dst);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  void ggml_compute_forward_map_custom1(const struct ggml_compute_params * params, struct ggml_tensor * dst);
100
  void ggml_compute_forward_map_custom2(const struct ggml_compute_params * params, struct ggml_tensor * dst);
101
  void ggml_compute_forward_map_custom3(const struct ggml_compute_params * params, struct ggml_tensor * dst);
102
+ void ggml_compute_forward_custom(const struct ggml_compute_params * params, struct ggml_tensor * dst);
103
  void ggml_compute_forward_cross_entropy_loss(const struct ggml_compute_params * params, struct ggml_tensor * dst);
104
  void ggml_compute_forward_cross_entropy_loss_back(const struct ggml_compute_params * params, struct ggml_tensor * dst);
105
  void ggml_compute_forward_opt_step_adamw(const struct ggml_compute_params * params, struct ggml_tensor * dst);
ggml/src/ggml-impl.h CHANGED
@@ -140,8 +140,14 @@ struct ggml_map_custom2_op_params {
140
 
141
  struct ggml_map_custom3_op_params {
142
  ggml_custom3_op_t fun;
143
- int n_tasks;
144
- void * userdata;
 
 
 
 
 
 
145
  };
146
 
147
  // bitset
 
140
 
141
  struct ggml_map_custom3_op_params {
142
  ggml_custom3_op_t fun;
143
+ int n_tasks;
144
+ void * userdata;
145
+ };
146
+
147
+ struct ggml_custom_op_params {
148
+ ggml_custom_op_t fun;
149
+ int n_tasks;
150
+ void * userdata;
151
  };
152
 
153
  // bitset
ggml/src/ggml.c CHANGED
@@ -982,23 +982,18 @@ static const char * GGML_OP_NAME[GGML_OP_COUNT] = {
982
 
983
  "UNARY",
984
 
985
- "MAP_UNARY",
986
- "MAP_BINARY",
987
-
988
- "MAP_CUSTOM1_F32",
989
- "MAP_CUSTOM2_F32",
990
- "MAP_CUSTOM3_F32",
991
-
992
  "MAP_CUSTOM1",
993
  "MAP_CUSTOM2",
994
  "MAP_CUSTOM3",
995
 
 
 
996
  "CROSS_ENTROPY_LOSS",
997
  "CROSS_ENTROPY_LOSS_BACK",
998
  "OPT_STEP_ADAMW",
999
  };
1000
 
1001
- static_assert(GGML_OP_COUNT == 85, "GGML_OP_COUNT != 85");
1002
 
1003
  static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = {
1004
  "none",
@@ -1081,23 +1076,18 @@ static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = {
1081
 
1082
  "unary(x)",
1083
 
1084
- "f(x)",
1085
- "f(x,y)",
1086
-
1087
- "custom_f32(x)",
1088
- "custom_f32(x,y)",
1089
- "custom_f32(x,y,z)",
1090
 
1091
  "custom(x)",
1092
- "custom(x,y)",
1093
- "custom(x,y,z)",
1094
 
1095
  "cross_entropy_loss(x,y)",
1096
  "cross_entropy_loss_back(x,y)",
1097
  "adamw(x)",
1098
  };
1099
 
1100
- static_assert(GGML_OP_COUNT == 85, "GGML_OP_COUNT != 85");
1101
 
1102
  static_assert(GGML_OP_POOL_COUNT == 2, "GGML_OP_POOL_COUNT != 2");
1103
 
@@ -4842,179 +4832,6 @@ struct ggml_tensor * ggml_unary_inplace(
4842
  return ggml_unary_impl(ctx, a, op, true);
4843
  }
4844
 
4845
- // ggml_map_unary
4846
-
4847
- static struct ggml_tensor * ggml_map_unary_impl_f32(
4848
- struct ggml_context * ctx,
4849
- struct ggml_tensor * a,
4850
- const ggml_unary_op_f32_t fun,
4851
- bool inplace) {
4852
- struct ggml_tensor * result = inplace ? ggml_view_tensor(ctx, a) : ggml_dup_tensor(ctx, a);
4853
-
4854
- ggml_set_op_params(result, (const void *) &fun, sizeof(fun));
4855
-
4856
- result->op = GGML_OP_MAP_UNARY;
4857
- result->src[0] = a;
4858
-
4859
- return result;
4860
- }
4861
-
4862
- struct ggml_tensor * ggml_map_unary_f32(
4863
- struct ggml_context * ctx,
4864
- struct ggml_tensor * a,
4865
- const ggml_unary_op_f32_t fun) {
4866
- return ggml_map_unary_impl_f32(ctx, a, fun, false);
4867
- }
4868
-
4869
- struct ggml_tensor * ggml_map_unary_inplace_f32(
4870
- struct ggml_context * ctx,
4871
- struct ggml_tensor * a,
4872
- const ggml_unary_op_f32_t fun) {
4873
- return ggml_map_unary_impl_f32(ctx, a, fun, true);
4874
- }
4875
-
4876
- // ggml_map_binary
4877
-
4878
- static struct ggml_tensor * ggml_map_binary_impl_f32(
4879
- struct ggml_context * ctx,
4880
- struct ggml_tensor * a,
4881
- struct ggml_tensor * b,
4882
- const ggml_binary_op_f32_t fun,
4883
- bool inplace) {
4884
- GGML_ASSERT(ggml_are_same_shape(a, b));
4885
-
4886
- struct ggml_tensor * result = inplace ? ggml_view_tensor(ctx, a) : ggml_dup_tensor(ctx, a);
4887
-
4888
- ggml_set_op_params(result, (const void *) &fun, sizeof(fun));
4889
-
4890
- result->op = GGML_OP_MAP_BINARY;
4891
- result->src[0] = a;
4892
- result->src[1] = b;
4893
-
4894
- return result;
4895
- }
4896
-
4897
- struct ggml_tensor * ggml_map_binary_f32(
4898
- struct ggml_context * ctx,
4899
- struct ggml_tensor * a,
4900
- struct ggml_tensor * b,
4901
- const ggml_binary_op_f32_t fun) {
4902
- return ggml_map_binary_impl_f32(ctx, a, b, fun, false);
4903
- }
4904
-
4905
- struct ggml_tensor * ggml_map_binary_inplace_f32(
4906
- struct ggml_context * ctx,
4907
- struct ggml_tensor * a,
4908
- struct ggml_tensor * b,
4909
- const ggml_binary_op_f32_t fun) {
4910
- return ggml_map_binary_impl_f32(ctx, a, b, fun, true);
4911
- }
4912
-
4913
- // ggml_map_custom1_f32
4914
-
4915
- static struct ggml_tensor * ggml_map_custom1_impl_f32(
4916
- struct ggml_context * ctx,
4917
- struct ggml_tensor * a,
4918
- const ggml_custom1_op_f32_t fun,
4919
- bool inplace) {
4920
- struct ggml_tensor * result = inplace ? ggml_view_tensor(ctx, a) : ggml_dup_tensor(ctx, a);
4921
-
4922
- ggml_set_op_params(result, (const void *) &fun, sizeof(fun));
4923
-
4924
- result->op = GGML_OP_MAP_CUSTOM1_F32;
4925
- result->src[0] = a;
4926
-
4927
- return result;
4928
- }
4929
-
4930
- struct ggml_tensor * ggml_map_custom1_f32(
4931
- struct ggml_context * ctx,
4932
- struct ggml_tensor * a,
4933
- const ggml_custom1_op_f32_t fun) {
4934
- return ggml_map_custom1_impl_f32(ctx, a, fun, false);
4935
- }
4936
-
4937
- struct ggml_tensor * ggml_map_custom1_inplace_f32(
4938
- struct ggml_context * ctx,
4939
- struct ggml_tensor * a,
4940
- const ggml_custom1_op_f32_t fun) {
4941
- return ggml_map_custom1_impl_f32(ctx, a, fun, true);
4942
- }
4943
-
4944
- // ggml_map_custom2_f32
4945
-
4946
- static struct ggml_tensor * ggml_map_custom2_impl_f32(
4947
- struct ggml_context * ctx,
4948
- struct ggml_tensor * a,
4949
- struct ggml_tensor * b,
4950
- const ggml_custom2_op_f32_t fun,
4951
- bool inplace) {
4952
- struct ggml_tensor * result = inplace ? ggml_view_tensor(ctx, a) : ggml_dup_tensor(ctx, a);
4953
-
4954
- ggml_set_op_params(result, (const void *) &fun, sizeof(fun));
4955
-
4956
- result->op = GGML_OP_MAP_CUSTOM2_F32;
4957
- result->src[0] = a;
4958
- result->src[1] = b;
4959
-
4960
- return result;
4961
- }
4962
-
4963
- struct ggml_tensor * ggml_map_custom2_f32(
4964
- struct ggml_context * ctx,
4965
- struct ggml_tensor * a,
4966
- struct ggml_tensor * b,
4967
- const ggml_custom2_op_f32_t fun) {
4968
- return ggml_map_custom2_impl_f32(ctx, a, b, fun, false);
4969
- }
4970
-
4971
- struct ggml_tensor * ggml_map_custom2_inplace_f32(
4972
- struct ggml_context * ctx,
4973
- struct ggml_tensor * a,
4974
- struct ggml_tensor * b,
4975
- const ggml_custom2_op_f32_t fun) {
4976
- return ggml_map_custom2_impl_f32(ctx, a, b, fun, true);
4977
- }
4978
-
4979
- // ggml_map_custom3_f32
4980
-
4981
- static struct ggml_tensor * ggml_map_custom3_impl_f32(
4982
- struct ggml_context * ctx,
4983
- struct ggml_tensor * a,
4984
- struct ggml_tensor * b,
4985
- struct ggml_tensor * c,
4986
- const ggml_custom3_op_f32_t fun,
4987
- bool inplace) {
4988
- struct ggml_tensor * result = inplace ? ggml_view_tensor(ctx, a) : ggml_dup_tensor(ctx, a);
4989
-
4990
- ggml_set_op_params(result, (const void *) &fun, sizeof(fun));
4991
-
4992
- result->op = GGML_OP_MAP_CUSTOM3_F32;
4993
- result->src[0] = a;
4994
- result->src[1] = b;
4995
- result->src[2] = c;
4996
-
4997
- return result;
4998
- }
4999
-
5000
- struct ggml_tensor * ggml_map_custom3_f32(
5001
- struct ggml_context * ctx,
5002
- struct ggml_tensor * a,
5003
- struct ggml_tensor * b,
5004
- struct ggml_tensor * c,
5005
- const ggml_custom3_op_f32_t fun) {
5006
- return ggml_map_custom3_impl_f32(ctx, a, b, c, fun, false);
5007
- }
5008
-
5009
- struct ggml_tensor * ggml_map_custom3_inplace_f32(
5010
- struct ggml_context * ctx,
5011
- struct ggml_tensor * a,
5012
- struct ggml_tensor * b,
5013
- struct ggml_tensor * c,
5014
- const ggml_custom3_op_f32_t fun) {
5015
- return ggml_map_custom3_impl_f32(ctx, a, b, c, fun, true);
5016
- }
5017
-
5018
  // ggml_map_custom1
5019
 
5020
  static struct ggml_tensor * ggml_map_custom1_impl(
@@ -5033,7 +4850,7 @@ static struct ggml_tensor * ggml_map_custom1_impl(
5033
  /*.n_tasks =*/ n_tasks,
5034
  /*.userdata =*/ userdata
5035
  };
5036
- ggml_set_op_params(result, (const void *) &params, sizeof(params));
5037
 
5038
  result->op = GGML_OP_MAP_CUSTOM1;
5039
  result->src[0] = a;
@@ -5078,7 +4895,7 @@ static struct ggml_tensor * ggml_map_custom2_impl(
5078
  /*.n_tasks =*/ n_tasks,
5079
  /*.userdata =*/ userdata
5080
  };
5081
- ggml_set_op_params(result, (const void *) &params, sizeof(params));
5082
 
5083
  result->op = GGML_OP_MAP_CUSTOM2;
5084
  result->src[0] = a;
@@ -5127,7 +4944,7 @@ static struct ggml_tensor * ggml_map_custom3_impl(
5127
  /*.n_tasks =*/ n_tasks,
5128
  /*.userdata =*/ userdata
5129
  };
5130
- ggml_set_op_params(result, (const void *) &params, sizeof(params));
5131
 
5132
  result->op = GGML_OP_MAP_CUSTOM3;
5133
  result->src[0] = a;
@@ -5159,6 +4976,66 @@ struct ggml_tensor * ggml_map_custom3_inplace(
5159
  return ggml_map_custom3_impl(ctx, a, b, c, fun, n_tasks, userdata, true);
5160
  }
5161
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5162
  // ggml_cross_entropy_loss
5163
 
5164
  struct ggml_tensor * ggml_cross_entropy_loss(
 
982
 
983
  "UNARY",
984
 
 
 
 
 
 
 
 
985
  "MAP_CUSTOM1",
986
  "MAP_CUSTOM2",
987
  "MAP_CUSTOM3",
988
 
989
+ "CUSTOM",
990
+
991
  "CROSS_ENTROPY_LOSS",
992
  "CROSS_ENTROPY_LOSS_BACK",
993
  "OPT_STEP_ADAMW",
994
  };
995
 
996
+ static_assert(GGML_OP_COUNT == 81, "GGML_OP_COUNT != 81");
997
 
998
  static const char * GGML_OP_SYMBOL[GGML_OP_COUNT] = {
999
  "none",
 
1076
 
1077
  "unary(x)",
1078
 
1079
+ "map_custom(x)",
1080
+ "map_custom(x,y)",
1081
+ "map_custom(x,y,z)",
 
 
 
1082
 
1083
  "custom(x)",
 
 
1084
 
1085
  "cross_entropy_loss(x,y)",
1086
  "cross_entropy_loss_back(x,y)",
1087
  "adamw(x)",
1088
  };
1089
 
1090
+ static_assert(GGML_OP_COUNT == 81, "GGML_OP_COUNT != 81");
1091
 
1092
  static_assert(GGML_OP_POOL_COUNT == 2, "GGML_OP_POOL_COUNT != 2");
1093
 
 
4832
  return ggml_unary_impl(ctx, a, op, true);
4833
  }
4834
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4835
  // ggml_map_custom1
4836
 
4837
  static struct ggml_tensor * ggml_map_custom1_impl(
 
4850
  /*.n_tasks =*/ n_tasks,
4851
  /*.userdata =*/ userdata
4852
  };
4853
+ ggml_set_op_params(result, &params, sizeof(params));
4854
 
4855
  result->op = GGML_OP_MAP_CUSTOM1;
4856
  result->src[0] = a;
 
4895
  /*.n_tasks =*/ n_tasks,
4896
  /*.userdata =*/ userdata
4897
  };
4898
+ ggml_set_op_params(result, &params, sizeof(params));
4899
 
4900
  result->op = GGML_OP_MAP_CUSTOM2;
4901
  result->src[0] = a;
 
4944
  /*.n_tasks =*/ n_tasks,
4945
  /*.userdata =*/ userdata
4946
  };
4947
+ ggml_set_op_params(result, &params, sizeof(params));
4948
 
4949
  result->op = GGML_OP_MAP_CUSTOM3;
4950
  result->src[0] = a;
 
4976
  return ggml_map_custom3_impl(ctx, a, b, c, fun, n_tasks, userdata, true);
4977
  }
4978
 
4979
+ struct ggml_tensor * ggml_custom_4d(
4980
+ struct ggml_context * ctx,
4981
+ enum ggml_type type,
4982
+ int64_t ne0,
4983
+ int64_t ne1,
4984
+ int64_t ne2,
4985
+ int64_t ne3,
4986
+ struct ggml_tensor ** args,
4987
+ int n_args,
4988
+ ggml_custom_op_t fun,
4989
+ int n_tasks,
4990
+ void * userdata) {
4991
+
4992
+ GGML_ASSERT(n_args < GGML_MAX_SRC);
4993
+
4994
+ struct ggml_tensor * result = ggml_new_tensor_4d(ctx, type, ne0, ne1, ne2, ne3);
4995
+
4996
+ struct ggml_custom_op_params params = {
4997
+ /*.fun =*/ fun,
4998
+ /*.n_tasks =*/ n_tasks,
4999
+ /*.userdata =*/ userdata
5000
+ };
5001
+ ggml_set_op_params(result, &params, sizeof(params));
5002
+
5003
+ result->op = GGML_OP_CUSTOM;
5004
+ for (int i = 0; i < n_args; i++) {
5005
+ result->src[i] = args[i];
5006
+ }
5007
+
5008
+ return result;
5009
+ }
5010
+
5011
+ struct ggml_tensor * ggml_custom_inplace(
5012
+ struct ggml_context * ctx,
5013
+ struct ggml_tensor * a,
5014
+ struct ggml_tensor ** args,
5015
+ int n_args,
5016
+ ggml_custom_op_t fun,
5017
+ int n_tasks,
5018
+ void * userdata) {
5019
+
5020
+ GGML_ASSERT(n_args < GGML_MAX_SRC - 1);
5021
+
5022
+ struct ggml_tensor * result = ggml_view_tensor(ctx, a);
5023
+
5024
+ struct ggml_custom_op_params params = {
5025
+ /*.fun =*/ fun,
5026
+ /*.n_tasks =*/ n_tasks,
5027
+ /*.userdata =*/ userdata
5028
+ };
5029
+ ggml_set_op_params(result, &params, sizeof(params));
5030
+
5031
+ result->op = GGML_OP_CUSTOM;
5032
+ result->src[0] = a;
5033
+ for (int i = 0; i < n_args; i++) {
5034
+ result->src[i + 1] = args[i];
5035
+ }
5036
+
5037
+ return result;
5038
+ }
5039
  // ggml_cross_entropy_loss
5040
 
5041
  struct ggml_tensor * ggml_cross_entropy_loss(