libggml-python 0.3.3__cp314-cp314-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
libggml/include/ggml.h ADDED
@@ -0,0 +1,2405 @@
1
+ #pragma once
2
+
3
+ //
4
+ // GGML Tensor Library
5
+ //
6
+ // This documentation is still a work in progress.
7
+ // If you wish some specific topics to be covered, feel free to drop a comment:
8
+ //
9
+ // https://github.com/ggerganov/whisper.cpp/issues/40
10
+ //
11
+ // ## Overview
12
+ //
13
+ // This library implements:
14
+ //
15
+ // - a set of tensor operations
16
+ // - automatic differentiation
17
+ // - basic optimization algorithms
18
+ //
19
+ // The aim of this library is to provide a minimalistic approach for various machine learning tasks. This includes,
20
+ // but is not limited to, the following:
21
+ //
22
+ // - linear regression
23
+ // - support vector machines
24
+ // - neural networks
25
+ //
26
+ // The library allows the user to define a certain function using the available tensor operations. This function
27
+ // definition is represented internally via a computation graph. Each tensor operation in the function definition
28
+ // corresponds to a node in the graph. Having the computation graph defined, the user can choose to compute the
29
+ // function's value and/or its gradient with respect to the input variables. Optionally, the function can be optimized
30
+ // using one of the available optimization algorithms.
31
+ //
32
+ // For example, here we define the function: f(x) = a*x^2 + b
33
+ //
34
+ // {
35
+ // struct ggml_init_params params = {
36
+ // .mem_size = 16*1024*1024,
37
+ // .mem_buffer = NULL,
38
+ // };
39
+ //
40
+ // // memory allocation happens here
41
+ // struct ggml_context * ctx = ggml_init(params);
42
+ //
43
+ // struct ggml_tensor * x = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
44
+ //
45
+ // ggml_set_param(ctx, x); // x is an input variable
46
+ //
47
+ // struct ggml_tensor * a = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
48
+ // struct ggml_tensor * b = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, 1);
49
+ // struct ggml_tensor * x2 = ggml_mul(ctx, x, x);
50
+ // struct ggml_tensor * f = ggml_add(ctx, ggml_mul(ctx, a, x2), b);
51
+ //
52
+ // ...
53
+ // }
54
+ //
55
+ // Notice that the function definition above does not involve any actual computation. The computation is performed only
56
+ // when the user explicitly requests it. For example, to compute the function's value at x = 2.0:
57
+ //
58
+ // {
59
+ // ...
60
+ //
61
+ // struct ggml_cgraph * gf = ggml_new_graph(ctx);
62
+ // ggml_build_forward_expand(gf, f);
63
+ //
64
+ // // set the input variable and parameter values
65
+ // ggml_set_f32(x, 2.0f);
66
+ // ggml_set_f32(a, 3.0f);
67
+ // ggml_set_f32(b, 4.0f);
68
+ //
69
+ // ggml_graph_compute_with_ctx(ctx, &gf, n_threads);
70
+ //
71
+ // printf("f = %f\n", ggml_get_f32_1d(f, 0));
72
+ //
73
+ // ...
74
+ // }
75
+ //
76
+ // The actual computation is performed in the ggml_graph_compute() function.
77
+ //
78
+ // The ggml_new_tensor_...() functions create new tensors. They are allocated in the memory buffer provided to the
79
+ // ggml_init() function. You have to be careful not to exceed the memory buffer size. Therefore, you have to know
80
+ // in advance how much memory you need for your computation. Alternatively, you can allocate a large enough memory
81
+ // and after defining the computation graph, call the ggml_used_mem() function to find out how much memory was
82
+ // actually needed.
83
+ //
84
+ // The ggml_set_param() function marks a tensor as an input variable. This is used by the automatic
85
+ // differentiation and optimization algorithms.
86
+ //
87
+ // The described approach allows to define the function graph once and then compute its forward or backward graphs
88
+ // multiple times. All computations will use the same memory buffer allocated in the ggml_init() function. This way
89
+ // the user can avoid the memory allocation overhead at runtime.
90
+ //
91
+ // The library supports multi-dimensional tensors - up to 4 dimensions. The FP16 and FP32 data types are first class
92
+ // citizens, but in theory the library can be extended to support FP8 and integer data types.
93
+ //
94
+ // Each tensor operation produces a new tensor. Initially the library was envisioned to support only the use of unary
95
+ // and binary operations. Most of the available operations fall into one of these two categories. With time, it became
96
+ // clear that the library needs to support more complex operations. The way to support these operations is not clear
97
+ // yet, but a few examples are demonstrated in the following operations:
98
+ //
99
+ // - ggml_permute()
100
+ // - ggml_conv_1d_1s()
101
+ // - ggml_conv_1d_2s()
102
+ //
103
+ // For each tensor operator, the library implements a forward and backward computation function. The forward function
104
+ // computes the output tensor value given the input tensor values. The backward function computes the adjoint of the
105
+ // input tensors given the adjoint of the output tensor. For a detailed explanation of what this means, take a
106
+ // calculus class, or watch the following video:
107
+ //
108
+ // What is Automatic Differentiation?
109
+ // https://www.youtube.com/watch?v=wG_nF1awSSY
110
+ //
111
+ //
112
+ // ## Tensor data (struct ggml_tensor)
113
+ //
114
+ // The tensors are stored in memory via the ggml_tensor struct. The structure provides information about the size of
115
+ // the tensor, the data type, and the memory buffer where the tensor data is stored. Additionally, it contains
116
+ // pointers to the "source" tensors - i.e. the tensors that were used to compute the current tensor. For example:
117
+ //
118
+ // {
119
+ // struct ggml_tensor * c = ggml_add(ctx, a, b);
120
+ //
121
+ // assert(c->src[0] == a);
122
+ // assert(c->src[1] == b);
123
+ // }
124
+ //
125
+ // The multi-dimensional tensors are stored in row-major order. The ggml_tensor struct contains fields for the
126
+ // number of elements in each dimension ("ne") as well as the number of bytes ("nb", a.k.a. stride). This allows
127
+ // to store tensors that are not contiguous in memory, which is useful for operations such as transposition and
128
+ // permutation. All tensor operations have to take the stride into account and not assume that the tensor is
129
+ // contiguous in memory.
130
+ //
131
+ // The data of the tensor is accessed via the "data" pointer. For example:
132
+ //
133
+ // {
134
+ // const int nx = 2;
135
+ // const int ny = 3;
136
+ //
137
+ // struct ggml_tensor * a = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, nx, ny);
138
+ //
139
+ // for (int y = 0; y < ny; y++) {
140
+ // for (int x = 0; x < nx; x++) {
141
+ // *(float *) ((char *) a->data + y*a->nb[1] + x*a->nb[0]) = x + y;
142
+ // }
143
+ // }
144
+ //
145
+ // ...
146
+ // }
147
+ //
148
+ // Alternatively, there are helper functions, such as ggml_get_f32_1d() and ggml_set_f32_1d() that can be used.
149
+ //
150
+ // ## The matrix multiplication operator (ggml_mul_mat)
151
+ //
152
+ // TODO
153
+ //
154
+ //
155
+ // ## Multi-threading
156
+ //
157
+ // TODO
158
+ //
159
+ //
160
+ // ## Overview of ggml.c
161
+ //
162
+ // TODO
163
+ //
164
+ //
165
+ // ## SIMD optimizations
166
+ //
167
+ // TODO
168
+ //
169
+ //
170
+ // ## Debugging ggml
171
+ //
172
+ // TODO
173
+ //
174
+ //
175
+
176
+ #ifdef GGML_SHARED
177
+ # if defined(_WIN32) && !defined(__MINGW32__)
178
+ # ifdef GGML_BUILD
179
+ # define GGML_API __declspec(dllexport) extern
180
+ # else
181
+ # define GGML_API __declspec(dllimport) extern
182
+ # endif
183
+ # else
184
+ # define GGML_API __attribute__ ((visibility ("default"))) extern
185
+ # endif
186
+ #else
187
+ # define GGML_API extern
188
+ #endif
189
+
190
+ // TODO: support for clang
191
+ #ifdef __GNUC__
192
+ # define GGML_DEPRECATED(func, hint) func __attribute__((deprecated(hint)))
193
+ #elif defined(_MSC_VER)
194
+ # define GGML_DEPRECATED(func, hint) __declspec(deprecated(hint)) func
195
+ #else
196
+ # define GGML_DEPRECATED(func, hint) func
197
+ #endif
198
+
199
+ #ifndef __GNUC__
200
+ # define GGML_ATTRIBUTE_FORMAT(...)
201
+ #elif defined(__MINGW32__) && !defined(__clang__)
202
+ # define GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
203
+ #else
204
+ # define GGML_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
205
+ #endif
206
+
207
+ #include <stdbool.h>
208
+ #include <stddef.h>
209
+ #include <stdint.h>
210
+ #include <stdio.h>
211
+
212
+ #define GGML_FILE_MAGIC 0x67676d6c // "ggml"
213
+ #define GGML_FILE_VERSION 2
214
+
215
+ #define GGML_QNT_VERSION 2 // bump this on quantization format changes
216
+ #define GGML_QNT_VERSION_FACTOR 1000 // do not change this
217
+
218
+ #define GGML_MAX_DIMS 4
219
+ #define GGML_MAX_PARAMS 2048
220
+ #define GGML_MAX_SRC 10
221
+ #define GGML_MAX_N_THREADS 512
222
+ #define GGML_MAX_OP_PARAMS 64
223
+
224
+ #ifndef GGML_MAX_NAME
225
+ # define GGML_MAX_NAME 64
226
+ #endif
227
+
228
+ #define GGML_DEFAULT_N_THREADS 4
229
+ #define GGML_DEFAULT_GRAPH_SIZE 2048
230
+
231
+ #if UINTPTR_MAX == 0xFFFFFFFF
232
+ #define GGML_MEM_ALIGN 4
233
+ #else
234
+ #define GGML_MEM_ALIGN 16
235
+ #endif
236
+
237
+ #define GGML_EXIT_SUCCESS 0
238
+ #define GGML_EXIT_ABORTED 1
239
+
240
+ #define GGML_ROPE_TYPE_NEOX 2
241
+ #define GGML_ROPE_TYPE_MROPE 8
242
+ #define GGML_ROPE_TYPE_VISION 24
243
+
244
+ #define GGML_UNUSED(x) (void)(x)
245
+
246
+ #define GGML_PAD(x, n) (((x) + (n) - 1) & ~((n) - 1))
247
+
248
+ #ifndef NDEBUG
249
+ # define GGML_UNREACHABLE() do { fprintf(stderr, "statement should be unreachable\n"); abort(); } while(0)
250
+ #elif defined(__GNUC__)
251
+ # define GGML_UNREACHABLE() __builtin_unreachable()
252
+ #elif defined(_MSC_VER)
253
+ # define GGML_UNREACHABLE() __assume(0)
254
+ #else
255
+ # define GGML_UNREACHABLE() ((void) 0)
256
+ #endif
257
+
258
+ #ifdef __cplusplus
259
+ # define GGML_NORETURN [[noreturn]]
260
+ #elif defined(_MSC_VER)
261
+ # define GGML_NORETURN __declspec(noreturn)
262
+ #else
263
+ # define GGML_NORETURN _Noreturn
264
+ #endif
265
+
266
+ #define GGML_ABORT(...) ggml_abort(__FILE__, __LINE__, __VA_ARGS__)
267
+ #define GGML_ASSERT(x) if (!(x)) GGML_ABORT("GGML_ASSERT(%s) failed", #x)
268
+
269
+ // used to copy the number of elements and stride in bytes of tensors into local variables.
270
+ // main purpose is to reduce code duplication and improve readability.
271
+ //
272
+ // example:
273
+ //
274
+ // GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne);
275
+ // GGML_TENSOR_LOCALS(size_t, nb1, src1, nb);
276
+ //
277
+ #define GGML_TENSOR_LOCALS_1(type, prefix, pointer, array) \
278
+ const type prefix##0 = (pointer)->array[0]; \
279
+ GGML_UNUSED(prefix##0);
280
+ #define GGML_TENSOR_LOCALS_2(type, prefix, pointer, array) \
281
+ GGML_TENSOR_LOCALS_1 (type, prefix, pointer, array) \
282
+ const type prefix##1 = (pointer)->array[1]; \
283
+ GGML_UNUSED(prefix##1);
284
+ #define GGML_TENSOR_LOCALS_3(type, prefix, pointer, array) \
285
+ GGML_TENSOR_LOCALS_2 (type, prefix, pointer, array) \
286
+ const type prefix##2 = (pointer)->array[2]; \
287
+ GGML_UNUSED(prefix##2);
288
+ #define GGML_TENSOR_LOCALS(type, prefix, pointer, array) \
289
+ GGML_TENSOR_LOCALS_3 (type, prefix, pointer, array) \
290
+ const type prefix##3 = (pointer)->array[3]; \
291
+ GGML_UNUSED(prefix##3);
292
+
293
+ #define GGML_TENSOR_UNARY_OP_LOCALS \
294
+ GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
295
+ GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
296
+ GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
297
+ GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
298
+
299
+ #define GGML_TENSOR_BINARY_OP_LOCALS \
300
+ GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
301
+ GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
302
+ GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
303
+ GGML_TENSOR_LOCALS(size_t, nb1, src1, nb) \
304
+ GGML_TENSOR_LOCALS(int64_t, ne, dst, ne) \
305
+ GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
306
+
307
+ #define GGML_TENSOR_BINARY_OP_LOCALS01 \
308
+ GGML_TENSOR_LOCALS(int64_t, ne0, src0, ne) \
309
+ GGML_TENSOR_LOCALS(size_t, nb0, src0, nb) \
310
+ GGML_TENSOR_LOCALS(int64_t, ne1, src1, ne) \
311
+ GGML_TENSOR_LOCALS(size_t, nb1, src1, nb)
312
+
313
+ #ifdef __cplusplus
314
+ extern "C" {
315
+ #endif
316
+
317
+ // Function type used in fatal error callbacks
318
+ typedef void (*ggml_abort_callback_t)(const char * error_message);
319
+
320
+ // Set the abort callback (passing null will restore original abort functionality: printing a message to stdout)
321
+ // Returns the old callback for chaining
322
+ GGML_API ggml_abort_callback_t ggml_set_abort_callback(ggml_abort_callback_t callback);
323
+
324
+ GGML_NORETURN GGML_ATTRIBUTE_FORMAT(3, 4)
325
+ GGML_API void ggml_abort(const char * file, int line, const char * fmt, ...);
326
+
327
+ enum ggml_status {
328
+ GGML_STATUS_ALLOC_FAILED = -2,
329
+ GGML_STATUS_FAILED = -1,
330
+ GGML_STATUS_SUCCESS = 0,
331
+ GGML_STATUS_ABORTED = 1,
332
+ };
333
+
334
+ // get ggml_status name string
335
+ GGML_API const char * ggml_status_to_string(enum ggml_status status);
336
+
337
+ // ieee 754-2008 half-precision float16
338
+ // todo: make this not an integral type
339
+ typedef uint16_t ggml_fp16_t;
340
+ GGML_API float ggml_fp16_to_fp32(ggml_fp16_t);
341
+ GGML_API ggml_fp16_t ggml_fp32_to_fp16(float);
342
+ GGML_API void ggml_fp16_to_fp32_row(const ggml_fp16_t *, float *, int64_t);
343
+ GGML_API void ggml_fp32_to_fp16_row(const float *, ggml_fp16_t *, int64_t);
344
+
345
+ // google brain half-precision bfloat16
346
+ typedef struct { uint16_t bits; } ggml_bf16_t;
347
+ GGML_API ggml_bf16_t ggml_fp32_to_bf16(float);
348
+ GGML_API float ggml_bf16_to_fp32(ggml_bf16_t); // consider just doing << 16
349
+ GGML_API void ggml_bf16_to_fp32_row(const ggml_bf16_t *, float *, int64_t);
350
+ GGML_API void ggml_fp32_to_bf16_row_ref(const float *, ggml_bf16_t *, int64_t);
351
+ GGML_API void ggml_fp32_to_bf16_row(const float *, ggml_bf16_t *, int64_t);
352
+
353
+ struct ggml_object;
354
+ struct ggml_context;
355
+ struct ggml_cgraph;
356
+
357
+ // NOTE: always add types at the end of the enum to keep backward compatibility
358
+ enum ggml_type {
359
+ GGML_TYPE_F32 = 0,
360
+ GGML_TYPE_F16 = 1,
361
+ GGML_TYPE_Q4_0 = 2,
362
+ GGML_TYPE_Q4_1 = 3,
363
+ // GGML_TYPE_Q4_2 = 4, support has been removed
364
+ // GGML_TYPE_Q4_3 = 5, support has been removed
365
+ GGML_TYPE_Q5_0 = 6,
366
+ GGML_TYPE_Q5_1 = 7,
367
+ GGML_TYPE_Q8_0 = 8,
368
+ GGML_TYPE_Q8_1 = 9,
369
+ GGML_TYPE_Q2_K = 10,
370
+ GGML_TYPE_Q3_K = 11,
371
+ GGML_TYPE_Q4_K = 12,
372
+ GGML_TYPE_Q5_K = 13,
373
+ GGML_TYPE_Q6_K = 14,
374
+ GGML_TYPE_Q8_K = 15,
375
+ GGML_TYPE_IQ2_XXS = 16,
376
+ GGML_TYPE_IQ2_XS = 17,
377
+ GGML_TYPE_IQ3_XXS = 18,
378
+ GGML_TYPE_IQ1_S = 19,
379
+ GGML_TYPE_IQ4_NL = 20,
380
+ GGML_TYPE_IQ3_S = 21,
381
+ GGML_TYPE_IQ2_S = 22,
382
+ GGML_TYPE_IQ4_XS = 23,
383
+ GGML_TYPE_I8 = 24,
384
+ GGML_TYPE_I16 = 25,
385
+ GGML_TYPE_I32 = 26,
386
+ GGML_TYPE_I64 = 27,
387
+ GGML_TYPE_F64 = 28,
388
+ GGML_TYPE_IQ1_M = 29,
389
+ GGML_TYPE_BF16 = 30,
390
+ // GGML_TYPE_Q4_0_4_4 = 31, support has been removed from gguf files
391
+ // GGML_TYPE_Q4_0_4_8 = 32,
392
+ // GGML_TYPE_Q4_0_8_8 = 33,
393
+ GGML_TYPE_TQ1_0 = 34,
394
+ GGML_TYPE_TQ2_0 = 35,
395
+ // GGML_TYPE_IQ4_NL_4_4 = 36,
396
+ // GGML_TYPE_IQ4_NL_4_8 = 37,
397
+ // GGML_TYPE_IQ4_NL_8_8 = 38,
398
+ GGML_TYPE_COUNT = 39,
399
+ };
400
+
401
+ // precision
402
+ enum ggml_prec {
403
+ GGML_PREC_DEFAULT = 0, // stored as ggml_tensor.op_params, 0 by default
404
+ GGML_PREC_F32 = 10,
405
+ };
406
+
407
+ // model file types
408
+ enum ggml_ftype {
409
+ GGML_FTYPE_UNKNOWN = -1,
410
+ GGML_FTYPE_ALL_F32 = 0,
411
+ GGML_FTYPE_MOSTLY_F16 = 1, // except 1d tensors
412
+ GGML_FTYPE_MOSTLY_Q4_0 = 2, // except 1d tensors
413
+ GGML_FTYPE_MOSTLY_Q4_1 = 3, // except 1d tensors
414
+ GGML_FTYPE_MOSTLY_Q4_1_SOME_F16 = 4, // tok_embeddings.weight and output.weight are F16
415
+ GGML_FTYPE_MOSTLY_Q8_0 = 7, // except 1d tensors
416
+ GGML_FTYPE_MOSTLY_Q5_0 = 8, // except 1d tensors
417
+ GGML_FTYPE_MOSTLY_Q5_1 = 9, // except 1d tensors
418
+ GGML_FTYPE_MOSTLY_Q2_K = 10, // except 1d tensors
419
+ GGML_FTYPE_MOSTLY_Q3_K = 11, // except 1d tensors
420
+ GGML_FTYPE_MOSTLY_Q4_K = 12, // except 1d tensors
421
+ GGML_FTYPE_MOSTLY_Q5_K = 13, // except 1d tensors
422
+ GGML_FTYPE_MOSTLY_Q6_K = 14, // except 1d tensors
423
+ GGML_FTYPE_MOSTLY_IQ2_XXS = 15, // except 1d tensors
424
+ GGML_FTYPE_MOSTLY_IQ2_XS = 16, // except 1d tensors
425
+ GGML_FTYPE_MOSTLY_IQ3_XXS = 17, // except 1d tensors
426
+ GGML_FTYPE_MOSTLY_IQ1_S = 18, // except 1d tensors
427
+ GGML_FTYPE_MOSTLY_IQ4_NL = 19, // except 1d tensors
428
+ GGML_FTYPE_MOSTLY_IQ3_S = 20, // except 1d tensors
429
+ GGML_FTYPE_MOSTLY_IQ2_S = 21, // except 1d tensors
430
+ GGML_FTYPE_MOSTLY_IQ4_XS = 22, // except 1d tensors
431
+ GGML_FTYPE_MOSTLY_IQ1_M = 23, // except 1d tensors
432
+ GGML_FTYPE_MOSTLY_BF16 = 24, // except 1d tensors
433
+ };
434
+
435
+ // available tensor operations:
436
+ enum ggml_op {
437
+ GGML_OP_NONE = 0,
438
+
439
+ GGML_OP_DUP,
440
+ GGML_OP_ADD,
441
+ GGML_OP_ADD1,
442
+ GGML_OP_ACC,
443
+ GGML_OP_SUB,
444
+ GGML_OP_MUL,
445
+ GGML_OP_DIV,
446
+ GGML_OP_SQR,
447
+ GGML_OP_SQRT,
448
+ GGML_OP_LOG,
449
+ GGML_OP_SIN,
450
+ GGML_OP_COS,
451
+ GGML_OP_SUM,
452
+ GGML_OP_SUM_ROWS,
453
+ GGML_OP_MEAN,
454
+ GGML_OP_ARGMAX,
455
+ GGML_OP_COUNT_EQUAL,
456
+ GGML_OP_REPEAT,
457
+ GGML_OP_REPEAT_BACK,
458
+ GGML_OP_CONCAT,
459
+ GGML_OP_SILU_BACK,
460
+ GGML_OP_NORM, // normalize
461
+ GGML_OP_RMS_NORM,
462
+ GGML_OP_RMS_NORM_BACK,
463
+ GGML_OP_GROUP_NORM,
464
+ GGML_OP_L2_NORM,
465
+
466
+ GGML_OP_MUL_MAT,
467
+ GGML_OP_MUL_MAT_ID,
468
+ GGML_OP_OUT_PROD,
469
+
470
+ GGML_OP_SCALE,
471
+ GGML_OP_SET,
472
+ GGML_OP_CPY,
473
+ GGML_OP_CONT,
474
+ GGML_OP_RESHAPE,
475
+ GGML_OP_VIEW,
476
+ GGML_OP_PERMUTE,
477
+ GGML_OP_TRANSPOSE,
478
+ GGML_OP_GET_ROWS,
479
+ GGML_OP_GET_ROWS_BACK,
480
+ GGML_OP_SET_ROWS,
481
+ GGML_OP_DIAG,
482
+ GGML_OP_DIAG_MASK_INF,
483
+ GGML_OP_DIAG_MASK_ZERO,
484
+ GGML_OP_SOFT_MAX,
485
+ GGML_OP_SOFT_MAX_BACK,
486
+ GGML_OP_ROPE,
487
+ GGML_OP_ROPE_BACK,
488
+ GGML_OP_CLAMP,
489
+ GGML_OP_CONV_TRANSPOSE_1D,
490
+ GGML_OP_IM2COL,
491
+ GGML_OP_IM2COL_BACK,
492
+ GGML_OP_CONV_2D,
493
+ GGML_OP_CONV_2D_DW,
494
+ GGML_OP_CONV_TRANSPOSE_2D,
495
+ GGML_OP_POOL_1D,
496
+ GGML_OP_POOL_2D,
497
+ GGML_OP_POOL_2D_BACK,
498
+ GGML_OP_UPSCALE,
499
+ GGML_OP_PAD,
500
+ GGML_OP_PAD_REFLECT_1D,
501
+ GGML_OP_ROLL,
502
+ GGML_OP_ARANGE,
503
+ GGML_OP_TIMESTEP_EMBEDDING,
504
+ GGML_OP_ARGSORT,
505
+ GGML_OP_LEAKY_RELU,
506
+
507
+ GGML_OP_FLASH_ATTN_EXT,
508
+ GGML_OP_FLASH_ATTN_BACK,
509
+ GGML_OP_SSM_CONV,
510
+ GGML_OP_SSM_SCAN,
511
+ GGML_OP_WIN_PART,
512
+ GGML_OP_WIN_UNPART,
513
+ GGML_OP_GET_REL_POS,
514
+ GGML_OP_ADD_REL_POS,
515
+ GGML_OP_RWKV_WKV6,
516
+ GGML_OP_GATED_LINEAR_ATTN,
517
+ GGML_OP_RWKV_WKV7,
518
+
519
+ GGML_OP_UNARY,
520
+
521
+ GGML_OP_MAP_CUSTOM1,
522
+ GGML_OP_MAP_CUSTOM2,
523
+ GGML_OP_MAP_CUSTOM3,
524
+
525
+ GGML_OP_CUSTOM,
526
+
527
+ GGML_OP_CROSS_ENTROPY_LOSS,
528
+ GGML_OP_CROSS_ENTROPY_LOSS_BACK,
529
+ GGML_OP_OPT_STEP_ADAMW,
530
+
531
+ GGML_OP_GLU,
532
+
533
+ GGML_OP_COUNT,
534
+ };
535
+
536
+ enum ggml_unary_op {
537
+ GGML_UNARY_OP_ABS,
538
+ GGML_UNARY_OP_SGN,
539
+ GGML_UNARY_OP_NEG,
540
+ GGML_UNARY_OP_STEP,
541
+ GGML_UNARY_OP_TANH,
542
+ GGML_UNARY_OP_ELU,
543
+ GGML_UNARY_OP_RELU,
544
+ GGML_UNARY_OP_SIGMOID,
545
+ GGML_UNARY_OP_GELU,
546
+ GGML_UNARY_OP_GELU_QUICK,
547
+ GGML_UNARY_OP_SILU,
548
+ GGML_UNARY_OP_HARDSWISH,
549
+ GGML_UNARY_OP_HARDSIGMOID,
550
+ GGML_UNARY_OP_EXP,
551
+ GGML_UNARY_OP_GELU_ERF,
552
+
553
+ GGML_UNARY_OP_COUNT,
554
+ };
555
+
556
+ enum ggml_glu_op {
557
+ GGML_GLU_OP_REGLU,
558
+ GGML_GLU_OP_GEGLU,
559
+ GGML_GLU_OP_SWIGLU,
560
+ GGML_GLU_OP_GEGLU_ERF,
561
+ GGML_GLU_OP_GEGLU_QUICK,
562
+
563
+ GGML_GLU_OP_COUNT,
564
+ };
565
+
566
+ enum ggml_object_type {
567
+ GGML_OBJECT_TYPE_TENSOR,
568
+ GGML_OBJECT_TYPE_GRAPH,
569
+ GGML_OBJECT_TYPE_WORK_BUFFER
570
+ };
571
+
572
+ enum ggml_log_level {
573
+ GGML_LOG_LEVEL_NONE = 0,
574
+ GGML_LOG_LEVEL_DEBUG = 1,
575
+ GGML_LOG_LEVEL_INFO = 2,
576
+ GGML_LOG_LEVEL_WARN = 3,
577
+ GGML_LOG_LEVEL_ERROR = 4,
578
+ GGML_LOG_LEVEL_CONT = 5, // continue previous log
579
+ };
580
+
581
+ // this tensor...
582
+ enum ggml_tensor_flag {
583
+ GGML_TENSOR_FLAG_INPUT = 1, // ...is an input for the GGML compute graph
584
+ GGML_TENSOR_FLAG_OUTPUT = 2, // ...is an output for the GGML compute graph
585
+ GGML_TENSOR_FLAG_PARAM = 4, // ...contains trainable parameters
586
+ GGML_TENSOR_FLAG_LOSS = 8, // ...defines loss for numerical optimization (multiple loss tensors add up)
587
+ };
588
+
589
+ struct ggml_init_params {
590
+ // memory pool
591
+ size_t mem_size; // bytes
592
+ void * mem_buffer; // if NULL, memory will be allocated internally
593
+ bool no_alloc; // don't allocate memory for the tensor data
594
+ };
595
+
596
+ // n-dimensional tensor
597
+ struct ggml_tensor {
598
+ enum ggml_type type;
599
+
600
+ struct ggml_backend_buffer * buffer;
601
+
602
+ int64_t ne[GGML_MAX_DIMS]; // number of elements
603
+ size_t nb[GGML_MAX_DIMS]; // stride in bytes:
604
+ // nb[0] = ggml_type_size(type)
605
+ // nb[1] = nb[0] * (ne[0] / ggml_blck_size(type)) + padding
606
+ // nb[i] = nb[i-1] * ne[i-1]
607
+
608
+ // compute data
609
+ enum ggml_op op;
610
+
611
+ // op params - allocated as int32_t for alignment
612
+ int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)];
613
+
614
+ int32_t flags;
615
+
616
+ struct ggml_tensor * src[GGML_MAX_SRC];
617
+
618
+ // source tensor and offset for views
619
+ struct ggml_tensor * view_src;
620
+ size_t view_offs;
621
+
622
+ void * data;
623
+
624
+ char name[GGML_MAX_NAME];
625
+
626
+ void * extra; // extra things e.g. for ggml-cuda.cu
627
+
628
+ char padding[8];
629
+ };
630
+
631
+ static const size_t GGML_TENSOR_SIZE = sizeof(struct ggml_tensor);
632
+
633
+ // Abort callback
634
+ // If not NULL, called before ggml computation
635
+ // If it returns true, the computation is aborted
636
+ typedef bool (*ggml_abort_callback)(void * data);
637
+
638
+
639
+ //
640
+ // GUID
641
+ //
642
+
643
+ // GUID types
644
+ typedef uint8_t ggml_guid[16];
645
+ typedef ggml_guid * ggml_guid_t;
646
+
647
+ GGML_API bool ggml_guid_matches(ggml_guid_t guid_a, ggml_guid_t guid_b);
648
+
649
+ // misc
650
+
651
+ GGML_API const char * ggml_version(void);
652
+ GGML_API const char * ggml_commit(void);
653
+
654
+ GGML_API void ggml_time_init(void); // call this once at the beginning of the program
655
+ GGML_API int64_t ggml_time_ms(void);
656
+ GGML_API int64_t ggml_time_us(void);
657
+ GGML_API int64_t ggml_cycles(void);
658
+ GGML_API int64_t ggml_cycles_per_ms(void);
659
+
660
+ // accepts a UTF-8 path, even on Windows
661
+ GGML_API FILE * ggml_fopen(const char * fname, const char * mode);
662
+
663
+ GGML_API void ggml_print_object (const struct ggml_object * obj);
664
+ GGML_API void ggml_print_objects(const struct ggml_context * ctx);
665
+
666
+ GGML_API int64_t ggml_nelements (const struct ggml_tensor * tensor);
667
+ GGML_API int64_t ggml_nrows (const struct ggml_tensor * tensor);
668
+ GGML_API size_t ggml_nbytes (const struct ggml_tensor * tensor);
669
+ GGML_API size_t ggml_nbytes_pad(const struct ggml_tensor * tensor); // same as ggml_nbytes() but padded to GGML_MEM_ALIGN
670
+
671
+ GGML_API int64_t ggml_blck_size(enum ggml_type type);
672
+ GGML_API size_t ggml_type_size(enum ggml_type type); // size in bytes for all elements in a block
673
+ GGML_API size_t ggml_row_size (enum ggml_type type, int64_t ne); // size in bytes for all elements in a row
674
+
675
+ GGML_DEPRECATED(
676
+ GGML_API double ggml_type_sizef(enum ggml_type type), // ggml_type_size()/ggml_blck_size() as float
677
+ "use ggml_row_size() instead");
678
+
679
+ GGML_API const char * ggml_type_name(enum ggml_type type);
680
+ GGML_API const char * ggml_op_name (enum ggml_op op);
681
+ GGML_API const char * ggml_op_symbol(enum ggml_op op);
682
+
683
+ GGML_API const char * ggml_unary_op_name(enum ggml_unary_op op);
684
+ GGML_API const char * ggml_glu_op_name(enum ggml_glu_op op);
685
+ GGML_API const char * ggml_op_desc(const struct ggml_tensor * t); // unary or op name
686
+
687
+ GGML_API size_t ggml_element_size(const struct ggml_tensor * tensor);
688
+
689
+ GGML_API bool ggml_is_quantized(enum ggml_type type);
690
+
691
+ // TODO: temporary until model loading of ggml examples is refactored
692
+ GGML_API enum ggml_type ggml_ftype_to_ggml_type(enum ggml_ftype ftype);
693
+
694
+ GGML_API bool ggml_is_transposed(const struct ggml_tensor * tensor);
695
+ GGML_API bool ggml_is_permuted (const struct ggml_tensor * tensor);
696
+ GGML_API bool ggml_is_empty (const struct ggml_tensor * tensor);
697
+ GGML_API bool ggml_is_scalar (const struct ggml_tensor * tensor);
698
+ GGML_API bool ggml_is_vector (const struct ggml_tensor * tensor);
699
+ GGML_API bool ggml_is_matrix (const struct ggml_tensor * tensor);
700
+ GGML_API bool ggml_is_3d (const struct ggml_tensor * tensor);
701
+ GGML_API int ggml_n_dims (const struct ggml_tensor * tensor); // returns 1 for scalars
702
+
703
+ // returns whether the tensor elements can be iterated over with a flattened index (no gaps, no permutation)
704
+ GGML_API bool ggml_is_contiguous (const struct ggml_tensor * tensor);
705
+ GGML_API bool ggml_is_contiguous_0(const struct ggml_tensor * tensor); // same as ggml_is_contiguous()
706
+ GGML_API bool ggml_is_contiguous_1(const struct ggml_tensor * tensor); // contiguous for dims >= 1
707
+ GGML_API bool ggml_is_contiguous_2(const struct ggml_tensor * tensor); // contiguous for dims >= 2
708
+
709
+ // returns whether the tensor elements are allocated as one contiguous block of memory (no gaps, but permutation ok)
710
+ GGML_API bool ggml_is_contiguously_allocated(const struct ggml_tensor * tensor);
711
+
712
+ // true for tensor that is stored in memory as CxWxHxN and has been permuted to WxHxCxN
713
+ GGML_API bool ggml_is_contiguous_channels(const struct ggml_tensor * tensor);
714
+
715
+ // true if the elements in dimension 0 are contiguous, or there is just 1 block of elements
716
+ GGML_API bool ggml_is_contiguous_rows(const struct ggml_tensor * tensor);
717
+
718
+ GGML_API bool ggml_are_same_shape (const struct ggml_tensor * t0, const struct ggml_tensor * t1);
719
+ GGML_API bool ggml_are_same_stride(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
720
+
721
+ GGML_API bool ggml_can_repeat(const struct ggml_tensor * t0, const struct ggml_tensor * t1);
722
+
723
+ // use this to compute the memory overhead of a tensor
724
+ GGML_API size_t ggml_tensor_overhead(void);
725
+
726
+ GGML_API bool ggml_validate_row_data(enum ggml_type type, const void * data, size_t nbytes);
727
+
728
+ // main
729
+
730
+ GGML_API struct ggml_context * ggml_init (struct ggml_init_params params);
731
+ GGML_API void ggml_reset(struct ggml_context * ctx);
732
+ GGML_API void ggml_free (struct ggml_context * ctx);
733
+
734
+ GGML_API size_t ggml_used_mem(const struct ggml_context * ctx);
735
+
736
+ GGML_API bool ggml_get_no_alloc(struct ggml_context * ctx);
737
+ GGML_API void ggml_set_no_alloc(struct ggml_context * ctx, bool no_alloc);
738
+
739
+ GGML_API void * ggml_get_mem_buffer (const struct ggml_context * ctx);
740
+ GGML_API size_t ggml_get_mem_size (const struct ggml_context * ctx);
741
+ GGML_API size_t ggml_get_max_tensor_size(const struct ggml_context * ctx);
742
+
743
+ GGML_API struct ggml_tensor * ggml_new_tensor(
744
+ struct ggml_context * ctx,
745
+ enum ggml_type type,
746
+ int n_dims,
747
+ const int64_t *ne);
748
+
749
+ GGML_API struct ggml_tensor * ggml_new_tensor_1d(
750
+ struct ggml_context * ctx,
751
+ enum ggml_type type,
752
+ int64_t ne0);
753
+
754
+ GGML_API struct ggml_tensor * ggml_new_tensor_2d(
755
+ struct ggml_context * ctx,
756
+ enum ggml_type type,
757
+ int64_t ne0,
758
+ int64_t ne1);
759
+
760
+ GGML_API struct ggml_tensor * ggml_new_tensor_3d(
761
+ struct ggml_context * ctx,
762
+ enum ggml_type type,
763
+ int64_t ne0,
764
+ int64_t ne1,
765
+ int64_t ne2);
766
+
767
+ GGML_API struct ggml_tensor * ggml_new_tensor_4d(
768
+ struct ggml_context * ctx,
769
+ enum ggml_type type,
770
+ int64_t ne0,
771
+ int64_t ne1,
772
+ int64_t ne2,
773
+ int64_t ne3);
774
+
775
+ GGML_API void * ggml_new_buffer(struct ggml_context * ctx, size_t nbytes);
776
+
777
+ GGML_API struct ggml_tensor * ggml_dup_tensor (struct ggml_context * ctx, const struct ggml_tensor * src);
778
+ GGML_API struct ggml_tensor * ggml_view_tensor(struct ggml_context * ctx, struct ggml_tensor * src);
779
+
780
+ // Context tensor enumeration and lookup
781
+ GGML_API struct ggml_tensor * ggml_get_first_tensor(const struct ggml_context * ctx);
782
+ GGML_API struct ggml_tensor * ggml_get_next_tensor (const struct ggml_context * ctx, struct ggml_tensor * tensor);
783
+ GGML_API struct ggml_tensor * ggml_get_tensor(struct ggml_context * ctx, const char * name);
784
+
785
+ // Converts a flat index into coordinates
786
+ GGML_API void ggml_unravel_index(const struct ggml_tensor * tensor, int64_t i, int64_t * i0, int64_t * i1, int64_t * i2, int64_t * i3);
787
+
788
+ GGML_API enum ggml_unary_op ggml_get_unary_op(const struct ggml_tensor * tensor);
789
+ GGML_API enum ggml_glu_op ggml_get_glu_op(const struct ggml_tensor * tensor);
790
+
791
+ GGML_API void * ggml_get_data (const struct ggml_tensor * tensor);
792
+ GGML_API float * ggml_get_data_f32(const struct ggml_tensor * tensor);
793
+
794
+ GGML_API const char * ggml_get_name (const struct ggml_tensor * tensor);
795
+ GGML_API struct ggml_tensor * ggml_set_name ( struct ggml_tensor * tensor, const char * name);
796
+ GGML_ATTRIBUTE_FORMAT(2, 3)
797
+ GGML_API struct ggml_tensor * ggml_format_name( struct ggml_tensor * tensor, const char * fmt, ...);
798
+
799
+ // Tensor flags
800
+ GGML_API void ggml_set_input(struct ggml_tensor * tensor);
801
+ GGML_API void ggml_set_output(struct ggml_tensor * tensor);
802
+ GGML_API void ggml_set_param(struct ggml_tensor * tensor);
803
+ GGML_API void ggml_set_loss(struct ggml_tensor * tensor);
804
+
805
+ //
806
+ // operations on tensors with backpropagation
807
+ //
808
+
809
+ GGML_API struct ggml_tensor * ggml_dup(
810
+ struct ggml_context * ctx,
811
+ struct ggml_tensor * a);
812
+
813
+ // in-place, returns view(a)
814
+ GGML_API struct ggml_tensor * ggml_dup_inplace(
815
+ struct ggml_context * ctx,
816
+ struct ggml_tensor * a);
817
+
818
+ GGML_API struct ggml_tensor * ggml_add(
819
+ struct ggml_context * ctx,
820
+ struct ggml_tensor * a,
821
+ struct ggml_tensor * b);
822
+
823
+ GGML_API struct ggml_tensor * ggml_add_inplace(
824
+ struct ggml_context * ctx,
825
+ struct ggml_tensor * a,
826
+ struct ggml_tensor * b);
827
+
828
+ GGML_API struct ggml_tensor * ggml_add_cast(
829
+ struct ggml_context * ctx,
830
+ struct ggml_tensor * a,
831
+ struct ggml_tensor * b,
832
+ enum ggml_type type);
833
+
834
+ GGML_API struct ggml_tensor * ggml_add1(
835
+ struct ggml_context * ctx,
836
+ struct ggml_tensor * a,
837
+ struct ggml_tensor * b);
838
+
839
+ GGML_API struct ggml_tensor * ggml_add1_inplace(
840
+ struct ggml_context * ctx,
841
+ struct ggml_tensor * a,
842
+ struct ggml_tensor * b);
843
+
844
+ // dst = a
845
+ // view(dst, nb1, nb2, nb3, offset) += b
846
+ // return dst
847
+ GGML_API struct ggml_tensor * ggml_acc(
848
+ struct ggml_context * ctx,
849
+ struct ggml_tensor * a,
850
+ struct ggml_tensor * b,
851
+ size_t nb1,
852
+ size_t nb2,
853
+ size_t nb3,
854
+ size_t offset);
855
+
856
+ GGML_API struct ggml_tensor * ggml_acc_inplace(
857
+ struct ggml_context * ctx,
858
+ struct ggml_tensor * a,
859
+ struct ggml_tensor * b,
860
+ size_t nb1,
861
+ size_t nb2,
862
+ size_t nb3,
863
+ size_t offset);
864
+
865
+ GGML_API struct ggml_tensor * ggml_sub(
866
+ struct ggml_context * ctx,
867
+ struct ggml_tensor * a,
868
+ struct ggml_tensor * b);
869
+
870
+ GGML_API struct ggml_tensor * ggml_sub_inplace(
871
+ struct ggml_context * ctx,
872
+ struct ggml_tensor * a,
873
+ struct ggml_tensor * b);
874
+
875
+ GGML_API struct ggml_tensor * ggml_mul(
876
+ struct ggml_context * ctx,
877
+ struct ggml_tensor * a,
878
+ struct ggml_tensor * b);
879
+
880
+ GGML_API struct ggml_tensor * ggml_mul_inplace(
881
+ struct ggml_context * ctx,
882
+ struct ggml_tensor * a,
883
+ struct ggml_tensor * b);
884
+
885
+ GGML_API struct ggml_tensor * ggml_div(
886
+ struct ggml_context * ctx,
887
+ struct ggml_tensor * a,
888
+ struct ggml_tensor * b);
889
+
890
+ GGML_API struct ggml_tensor * ggml_div_inplace(
891
+ struct ggml_context * ctx,
892
+ struct ggml_tensor * a,
893
+ struct ggml_tensor * b);
894
+
895
+ GGML_API struct ggml_tensor * ggml_sqr(
896
+ struct ggml_context * ctx,
897
+ struct ggml_tensor * a);
898
+
899
+ GGML_API struct ggml_tensor * ggml_sqr_inplace(
900
+ struct ggml_context * ctx,
901
+ struct ggml_tensor * a);
902
+
903
+ GGML_API struct ggml_tensor * ggml_sqrt(
904
+ struct ggml_context * ctx,
905
+ struct ggml_tensor * a);
906
+
907
+ GGML_API struct ggml_tensor * ggml_sqrt_inplace(
908
+ struct ggml_context * ctx,
909
+ struct ggml_tensor * a);
910
+
911
+ GGML_API struct ggml_tensor * ggml_log(
912
+ struct ggml_context * ctx,
913
+ struct ggml_tensor * a);
914
+
915
+ GGML_API struct ggml_tensor * ggml_log_inplace(
916
+ struct ggml_context * ctx,
917
+ struct ggml_tensor * a);
918
+
919
+ GGML_API struct ggml_tensor * ggml_sin(
920
+ struct ggml_context * ctx,
921
+ struct ggml_tensor * a);
922
+
923
+ GGML_API struct ggml_tensor * ggml_sin_inplace(
924
+ struct ggml_context * ctx,
925
+ struct ggml_tensor * a);
926
+
927
+ GGML_API struct ggml_tensor * ggml_cos(
928
+ struct ggml_context * ctx,
929
+ struct ggml_tensor * a);
930
+
931
+ GGML_API struct ggml_tensor * ggml_cos_inplace(
932
+ struct ggml_context * ctx,
933
+ struct ggml_tensor * a);
934
+
935
+ // return scalar
936
+ GGML_API struct ggml_tensor * ggml_sum(
937
+ struct ggml_context * ctx,
938
+ struct ggml_tensor * a);
939
+
940
+ // sums along rows, with input shape [a,b,c,d] return shape [1,b,c,d]
941
+ GGML_API struct ggml_tensor * ggml_sum_rows(
942
+ struct ggml_context * ctx,
943
+ struct ggml_tensor * a);
944
+
945
+ // mean along rows
946
+ GGML_API struct ggml_tensor * ggml_mean(
947
+ struct ggml_context * ctx,
948
+ struct ggml_tensor * a);
949
+
950
+ // argmax along rows
951
+ GGML_API struct ggml_tensor * ggml_argmax(
952
+ struct ggml_context * ctx,
953
+ struct ggml_tensor * a);
954
+
955
+ // count number of equal elements in a and b
956
+ GGML_API struct ggml_tensor * ggml_count_equal(
957
+ struct ggml_context * ctx,
958
+ struct ggml_tensor * a,
959
+ struct ggml_tensor * b);
960
+
961
+ // if a is the same shape as b, and a is not parameter, return a
962
+ // otherwise, return a new tensor: repeat(a) to fit in b
963
+ GGML_API struct ggml_tensor * ggml_repeat(
964
+ struct ggml_context * ctx,
965
+ struct ggml_tensor * a,
966
+ struct ggml_tensor * b);
967
+
968
+ // repeat a to the specified shape
969
+ GGML_API struct ggml_tensor * ggml_repeat_4d(
970
+ struct ggml_context * ctx,
971
+ struct ggml_tensor * a,
972
+ int64_t ne0,
973
+ int64_t ne1,
974
+ int64_t ne2,
975
+ int64_t ne3);
976
+
977
+ // sums repetitions in a into shape of b
978
+ GGML_API struct ggml_tensor * ggml_repeat_back(
979
+ struct ggml_context * ctx,
980
+ struct ggml_tensor * a,
981
+ struct ggml_tensor * b); // sum up values that are adjacent in dims > 0 instead of repeated with same stride
982
+
983
+ // concat a and b along dim
984
+ // used in stable-diffusion
985
+ GGML_API struct ggml_tensor * ggml_concat(
986
+ struct ggml_context * ctx,
987
+ struct ggml_tensor * a,
988
+ struct ggml_tensor * b,
989
+ int dim);
990
+
991
+ GGML_API struct ggml_tensor * ggml_abs(
992
+ struct ggml_context * ctx,
993
+ struct ggml_tensor * a);
994
+
995
+ GGML_API struct ggml_tensor * ggml_abs_inplace(
996
+ struct ggml_context * ctx,
997
+ struct ggml_tensor * a);
998
+
999
+ GGML_API struct ggml_tensor * ggml_sgn(
1000
+ struct ggml_context * ctx,
1001
+ struct ggml_tensor * a);
1002
+
1003
+ GGML_API struct ggml_tensor * ggml_sgn_inplace(
1004
+ struct ggml_context * ctx,
1005
+ struct ggml_tensor * a);
1006
+
1007
+ GGML_API struct ggml_tensor * ggml_neg(
1008
+ struct ggml_context * ctx,
1009
+ struct ggml_tensor * a);
1010
+
1011
+ GGML_API struct ggml_tensor * ggml_neg_inplace(
1012
+ struct ggml_context * ctx,
1013
+ struct ggml_tensor * a);
1014
+
1015
+ GGML_API struct ggml_tensor * ggml_step(
1016
+ struct ggml_context * ctx,
1017
+ struct ggml_tensor * a);
1018
+
1019
+ GGML_API struct ggml_tensor * ggml_step_inplace(
1020
+ struct ggml_context * ctx,
1021
+ struct ggml_tensor * a);
1022
+
1023
+ GGML_API struct ggml_tensor * ggml_tanh(
1024
+ struct ggml_context * ctx,
1025
+ struct ggml_tensor * a);
1026
+
1027
+ GGML_API struct ggml_tensor * ggml_tanh_inplace(
1028
+ struct ggml_context * ctx,
1029
+ struct ggml_tensor * a);
1030
+
1031
+ GGML_API struct ggml_tensor * ggml_elu(
1032
+ struct ggml_context * ctx,
1033
+ struct ggml_tensor * a);
1034
+
1035
+ GGML_API struct ggml_tensor * ggml_elu_inplace(
1036
+ struct ggml_context * ctx,
1037
+ struct ggml_tensor * a);
1038
+
1039
+ GGML_API struct ggml_tensor * ggml_relu(
1040
+ struct ggml_context * ctx,
1041
+ struct ggml_tensor * a);
1042
+
1043
+ GGML_API struct ggml_tensor * ggml_leaky_relu(
1044
+ struct ggml_context * ctx,
1045
+ struct ggml_tensor * a, float negative_slope, bool inplace);
1046
+
1047
+ GGML_API struct ggml_tensor * ggml_relu_inplace(
1048
+ struct ggml_context * ctx,
1049
+ struct ggml_tensor * a);
1050
+
1051
+ GGML_API struct ggml_tensor * ggml_sigmoid(
1052
+ struct ggml_context * ctx,
1053
+ struct ggml_tensor * a);
1054
+
1055
+ GGML_API struct ggml_tensor * ggml_sigmoid_inplace(
1056
+ struct ggml_context * ctx,
1057
+ struct ggml_tensor * a);
1058
+
1059
+ GGML_API struct ggml_tensor * ggml_gelu(
1060
+ struct ggml_context * ctx,
1061
+ struct ggml_tensor * a);
1062
+
1063
+ GGML_API struct ggml_tensor * ggml_gelu_inplace(
1064
+ struct ggml_context * ctx,
1065
+ struct ggml_tensor * a);
1066
+
1067
+ // GELU using erf (error function) when possible
1068
+ // some backends may fallback to approximation based on Abramowitz and Stegun formula
1069
+ GGML_API struct ggml_tensor * ggml_gelu_erf(
1070
+ struct ggml_context * ctx,
1071
+ struct ggml_tensor * a);
1072
+
1073
+ GGML_API struct ggml_tensor * ggml_gelu_erf_inplace(
1074
+ struct ggml_context * ctx,
1075
+ struct ggml_tensor * a);
1076
+
1077
+ GGML_API struct ggml_tensor * ggml_gelu_quick(
1078
+ struct ggml_context * ctx,
1079
+ struct ggml_tensor * a);
1080
+
1081
+ GGML_API struct ggml_tensor * ggml_gelu_quick_inplace(
1082
+ struct ggml_context * ctx,
1083
+ struct ggml_tensor * a);
1084
+
1085
+ GGML_API struct ggml_tensor * ggml_silu(
1086
+ struct ggml_context * ctx,
1087
+ struct ggml_tensor * a);
1088
+
1089
+ GGML_API struct ggml_tensor * ggml_silu_inplace(
1090
+ struct ggml_context * ctx,
1091
+ struct ggml_tensor * a);
1092
+
1093
+ // a - x
1094
+ // b - dy
1095
+ GGML_API struct ggml_tensor * ggml_silu_back(
1096
+ struct ggml_context * ctx,
1097
+ struct ggml_tensor * a,
1098
+ struct ggml_tensor * b);
1099
+
1100
+ // hardswish(x) = x * relu6(x + 3) / 6
1101
+ GGML_API struct ggml_tensor * ggml_hardswish(
1102
+ struct ggml_context * ctx,
1103
+ struct ggml_tensor * a);
1104
+
1105
+ // hardsigmoid(x) = relu6(x + 3) / 6
1106
+ GGML_API struct ggml_tensor * ggml_hardsigmoid(
1107
+ struct ggml_context * ctx,
1108
+ struct ggml_tensor * a);
1109
+
1110
+ GGML_API struct ggml_tensor * ggml_exp(
1111
+ struct ggml_context * ctx,
1112
+ struct ggml_tensor * a);
1113
+
1114
+ GGML_API struct ggml_tensor * ggml_exp_inplace(
1115
+ struct ggml_context * ctx,
1116
+ struct ggml_tensor * a);
1117
+
1118
+ // gated linear unit ops
1119
+ // A: n columns, r rows,
1120
+ // result is n / 2 columns, r rows,
1121
+ // expects gate in second half of row, unless swapped is true
1122
+ GGML_API struct ggml_tensor * ggml_glu(
1123
+ struct ggml_context * ctx,
1124
+ struct ggml_tensor * a,
1125
+ enum ggml_glu_op op,
1126
+ bool swapped);
1127
+
1128
+ GGML_API struct ggml_tensor * ggml_reglu(
1129
+ struct ggml_context * ctx,
1130
+ struct ggml_tensor * a);
1131
+
1132
+ GGML_API struct ggml_tensor * ggml_reglu_swapped(
1133
+ struct ggml_context * ctx,
1134
+ struct ggml_tensor * a);
1135
+
1136
+ GGML_API struct ggml_tensor * ggml_geglu(
1137
+ struct ggml_context * ctx,
1138
+ struct ggml_tensor * a);
1139
+
1140
+ GGML_API struct ggml_tensor * ggml_geglu_swapped(
1141
+ struct ggml_context * ctx,
1142
+ struct ggml_tensor * a);
1143
+
1144
+ GGML_API struct ggml_tensor * ggml_swiglu(
1145
+ struct ggml_context * ctx,
1146
+ struct ggml_tensor * a);
1147
+
1148
+ GGML_API struct ggml_tensor * ggml_swiglu_swapped(
1149
+ struct ggml_context * ctx,
1150
+ struct ggml_tensor * a);
1151
+
1152
+ GGML_API struct ggml_tensor * ggml_geglu_erf(
1153
+ struct ggml_context * ctx,
1154
+ struct ggml_tensor * a);
1155
+
1156
+ GGML_API struct ggml_tensor * ggml_geglu_erf_swapped(
1157
+ struct ggml_context * ctx,
1158
+ struct ggml_tensor * a);
1159
+
1160
+ GGML_API struct ggml_tensor * ggml_geglu_quick(
1161
+ struct ggml_context * ctx,
1162
+ struct ggml_tensor * a);
1163
+
1164
+ GGML_API struct ggml_tensor * ggml_geglu_quick_swapped(
1165
+ struct ggml_context * ctx,
1166
+ struct ggml_tensor * a);
1167
+
1168
+ // A: n columns, r rows,
1169
+ // B: n columns, r rows,
1170
+ GGML_API struct ggml_tensor * ggml_glu_split(
1171
+ struct ggml_context * ctx,
1172
+ struct ggml_tensor * a,
1173
+ struct ggml_tensor * b,
1174
+ enum ggml_glu_op op);
1175
+
1176
+ GGML_API struct ggml_tensor * ggml_reglu_split(
1177
+ struct ggml_context * ctx,
1178
+ struct ggml_tensor * a,
1179
+ struct ggml_tensor * b);
1180
+
1181
+ GGML_API struct ggml_tensor * ggml_geglu_split(
1182
+ struct ggml_context * ctx,
1183
+ struct ggml_tensor * a,
1184
+ struct ggml_tensor * b);
1185
+
1186
+ GGML_API struct ggml_tensor * ggml_swiglu_split(
1187
+ struct ggml_context * ctx,
1188
+ struct ggml_tensor * a,
1189
+ struct ggml_tensor * b);
1190
+
1191
+ GGML_API struct ggml_tensor * ggml_geglu_erf_split(
1192
+ struct ggml_context * ctx,
1193
+ struct ggml_tensor * a,
1194
+ struct ggml_tensor * b);
1195
+
1196
+ GGML_API struct ggml_tensor * ggml_geglu_quick_split(
1197
+ struct ggml_context * ctx,
1198
+ struct ggml_tensor * a,
1199
+ struct ggml_tensor * b);
1200
+
1201
+ // normalize along rows
1202
+ GGML_API struct ggml_tensor * ggml_norm(
1203
+ struct ggml_context * ctx,
1204
+ struct ggml_tensor * a,
1205
+ float eps);
1206
+
1207
+ GGML_API struct ggml_tensor * ggml_norm_inplace(
1208
+ struct ggml_context * ctx,
1209
+ struct ggml_tensor * a,
1210
+ float eps);
1211
+
1212
+ GGML_API struct ggml_tensor * ggml_rms_norm(
1213
+ struct ggml_context * ctx,
1214
+ struct ggml_tensor * a,
1215
+ float eps);
1216
+
1217
+ GGML_API struct ggml_tensor * ggml_rms_norm_inplace(
1218
+ struct ggml_context * ctx,
1219
+ struct ggml_tensor * a,
1220
+ float eps);
1221
+
1222
+ // group normalize along ne0*ne1*n_groups
1223
+ // used in stable-diffusion
1224
+ GGML_API struct ggml_tensor * ggml_group_norm(
1225
+ struct ggml_context * ctx,
1226
+ struct ggml_tensor * a,
1227
+ int n_groups,
1228
+ float eps);
1229
+
1230
+ GGML_API struct ggml_tensor * ggml_group_norm_inplace(
1231
+ struct ggml_context * ctx,
1232
+ struct ggml_tensor * a,
1233
+ int n_groups,
1234
+ float eps);
1235
+
1236
+ // l2 normalize along rows
1237
+ // used in rwkv v7
1238
+ GGML_API struct ggml_tensor * ggml_l2_norm(
1239
+ struct ggml_context * ctx,
1240
+ struct ggml_tensor * a,
1241
+ float eps);
1242
+
1243
+ GGML_API struct ggml_tensor * ggml_l2_norm_inplace(
1244
+ struct ggml_context * ctx,
1245
+ struct ggml_tensor * a,
1246
+ float eps);
1247
+
1248
+ // a - x
1249
+ // b - dy
1250
+ GGML_API struct ggml_tensor * ggml_rms_norm_back(
1251
+ struct ggml_context * ctx,
1252
+ struct ggml_tensor * a,
1253
+ struct ggml_tensor * b,
1254
+ float eps);
1255
+
1256
+ // A: k columns, n rows => [ne03, ne02, n, k]
1257
+ // B: k columns, m rows (i.e. we transpose it internally) => [ne03 * x, ne02 * y, m, k]
1258
+ // result is n columns, m rows => [ne03 * x, ne02 * y, m, n]
1259
+ GGML_API struct ggml_tensor * ggml_mul_mat(
1260
+ struct ggml_context * ctx,
1261
+ struct ggml_tensor * a,
1262
+ struct ggml_tensor * b);
1263
+
1264
+ // change the precision of a matrix multiplication
1265
+ // set to GGML_PREC_F32 for higher precision (useful for phi-2)
1266
+ GGML_API void ggml_mul_mat_set_prec(
1267
+ struct ggml_tensor * a,
1268
+ enum ggml_prec prec);
1269
+
1270
+ // indirect matrix multiplication
1271
+ GGML_API struct ggml_tensor * ggml_mul_mat_id(
1272
+ struct ggml_context * ctx,
1273
+ struct ggml_tensor * as,
1274
+ struct ggml_tensor * b,
1275
+ struct ggml_tensor * ids);
1276
+
1277
+ // A: m columns, n rows,
1278
+ // B: p columns, n rows,
1279
+ // result is m columns, p rows
1280
+ GGML_API struct ggml_tensor * ggml_out_prod(
1281
+ struct ggml_context * ctx,
1282
+ struct ggml_tensor * a,
1283
+ struct ggml_tensor * b);
1284
+
1285
+ //
1286
+ // operations on tensors without backpropagation
1287
+ //
1288
+
1289
+ GGML_API struct ggml_tensor * ggml_scale(
1290
+ struct ggml_context * ctx,
1291
+ struct ggml_tensor * a,
1292
+ float s);
1293
+
1294
+ // in-place, returns view(a)
1295
+ GGML_API struct ggml_tensor * ggml_scale_inplace(
1296
+ struct ggml_context * ctx,
1297
+ struct ggml_tensor * a,
1298
+ float s);
1299
+
1300
+ // x = s * a + b
1301
+ GGML_API struct ggml_tensor * ggml_scale_bias(
1302
+ struct ggml_context * ctx,
1303
+ struct ggml_tensor * a,
1304
+ float s,
1305
+ float b);
1306
+
1307
+ GGML_API struct ggml_tensor * ggml_scale_bias_inplace(
1308
+ struct ggml_context * ctx,
1309
+ struct ggml_tensor * a,
1310
+ float s,
1311
+ float b);
1312
+
1313
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1314
+ GGML_API struct ggml_tensor * ggml_set(
1315
+ struct ggml_context * ctx,
1316
+ struct ggml_tensor * a,
1317
+ struct ggml_tensor * b,
1318
+ size_t nb1,
1319
+ size_t nb2,
1320
+ size_t nb3,
1321
+ size_t offset); // in bytes
1322
+
1323
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1324
+ GGML_API struct ggml_tensor * ggml_set_inplace(
1325
+ struct ggml_context * ctx,
1326
+ struct ggml_tensor * a,
1327
+ struct ggml_tensor * b,
1328
+ size_t nb1,
1329
+ size_t nb2,
1330
+ size_t nb3,
1331
+ size_t offset); // in bytes
1332
+
1333
+ GGML_API struct ggml_tensor * ggml_set_1d(
1334
+ struct ggml_context * ctx,
1335
+ struct ggml_tensor * a,
1336
+ struct ggml_tensor * b,
1337
+ size_t offset); // in bytes
1338
+
1339
+ GGML_API struct ggml_tensor * ggml_set_1d_inplace(
1340
+ struct ggml_context * ctx,
1341
+ struct ggml_tensor * a,
1342
+ struct ggml_tensor * b,
1343
+ size_t offset); // in bytes
1344
+
1345
+ // b -> view(a,offset,nb1,nb2,3), return modified a
1346
+ GGML_API struct ggml_tensor * ggml_set_2d(
1347
+ struct ggml_context * ctx,
1348
+ struct ggml_tensor * a,
1349
+ struct ggml_tensor * b,
1350
+ size_t nb1,
1351
+ size_t offset); // in bytes
1352
+
1353
+ // b -> view(a,offset,nb1,nb2,3), return view(a)
1354
+ GGML_API struct ggml_tensor * ggml_set_2d_inplace(
1355
+ struct ggml_context * ctx,
1356
+ struct ggml_tensor * a,
1357
+ struct ggml_tensor * b,
1358
+ size_t nb1,
1359
+ size_t offset); // in bytes
1360
+
1361
+ // a -> b, return view(b)
1362
+ GGML_API struct ggml_tensor * ggml_cpy(
1363
+ struct ggml_context * ctx,
1364
+ struct ggml_tensor * a,
1365
+ struct ggml_tensor * b);
1366
+
1367
+ GGML_API struct ggml_tensor * ggml_cast(
1368
+ struct ggml_context * ctx,
1369
+ struct ggml_tensor * a,
1370
+ enum ggml_type type);
1371
+
1372
+ // make contiguous
1373
+ GGML_API struct ggml_tensor * ggml_cont(
1374
+ struct ggml_context * ctx,
1375
+ struct ggml_tensor * a);
1376
+
1377
+ // make contiguous, with new shape
1378
+ GGML_API struct ggml_tensor * ggml_cont_1d(
1379
+ struct ggml_context * ctx,
1380
+ struct ggml_tensor * a,
1381
+ int64_t ne0);
1382
+
1383
+ GGML_API struct ggml_tensor * ggml_cont_2d(
1384
+ struct ggml_context * ctx,
1385
+ struct ggml_tensor * a,
1386
+ int64_t ne0,
1387
+ int64_t ne1);
1388
+
1389
+ GGML_API struct ggml_tensor * ggml_cont_3d(
1390
+ struct ggml_context * ctx,
1391
+ struct ggml_tensor * a,
1392
+ int64_t ne0,
1393
+ int64_t ne1,
1394
+ int64_t ne2);
1395
+
1396
+ GGML_API struct ggml_tensor * ggml_cont_4d(
1397
+ struct ggml_context * ctx,
1398
+ struct ggml_tensor * a,
1399
+ int64_t ne0,
1400
+ int64_t ne1,
1401
+ int64_t ne2,
1402
+ int64_t ne3);
1403
+
1404
+ // return view(a), b specifies the new shape
1405
+ // TODO: when we start computing gradient, make a copy instead of view
1406
+ GGML_API struct ggml_tensor * ggml_reshape(
1407
+ struct ggml_context * ctx,
1408
+ struct ggml_tensor * a,
1409
+ struct ggml_tensor * b);
1410
+
1411
+ // return view(a)
1412
+ // TODO: when we start computing gradient, make a copy instead of view
1413
+ GGML_API struct ggml_tensor * ggml_reshape_1d(
1414
+ struct ggml_context * ctx,
1415
+ struct ggml_tensor * a,
1416
+ int64_t ne0);
1417
+
1418
+ GGML_API struct ggml_tensor * ggml_reshape_2d(
1419
+ struct ggml_context * ctx,
1420
+ struct ggml_tensor * a,
1421
+ int64_t ne0,
1422
+ int64_t ne1);
1423
+
1424
+ // return view(a)
1425
+ // TODO: when we start computing gradient, make a copy instead of view
1426
+ GGML_API struct ggml_tensor * ggml_reshape_3d(
1427
+ struct ggml_context * ctx,
1428
+ struct ggml_tensor * a,
1429
+ int64_t ne0,
1430
+ int64_t ne1,
1431
+ int64_t ne2);
1432
+
1433
+ GGML_API struct ggml_tensor * ggml_reshape_4d(
1434
+ struct ggml_context * ctx,
1435
+ struct ggml_tensor * a,
1436
+ int64_t ne0,
1437
+ int64_t ne1,
1438
+ int64_t ne2,
1439
+ int64_t ne3);
1440
+
1441
+ // offset in bytes
1442
+ GGML_API struct ggml_tensor * ggml_view_1d(
1443
+ struct ggml_context * ctx,
1444
+ struct ggml_tensor * a,
1445
+ int64_t ne0,
1446
+ size_t offset);
1447
+
1448
+ GGML_API struct ggml_tensor * ggml_view_2d(
1449
+ struct ggml_context * ctx,
1450
+ struct ggml_tensor * a,
1451
+ int64_t ne0,
1452
+ int64_t ne1,
1453
+ size_t nb1, // row stride in bytes
1454
+ size_t offset);
1455
+
1456
+ GGML_API struct ggml_tensor * ggml_view_3d(
1457
+ struct ggml_context * ctx,
1458
+ struct ggml_tensor * a,
1459
+ int64_t ne0,
1460
+ int64_t ne1,
1461
+ int64_t ne2,
1462
+ size_t nb1, // row stride in bytes
1463
+ size_t nb2, // slice stride in bytes
1464
+ size_t offset);
1465
+
1466
+ GGML_API struct ggml_tensor * ggml_view_4d(
1467
+ struct ggml_context * ctx,
1468
+ struct ggml_tensor * a,
1469
+ int64_t ne0,
1470
+ int64_t ne1,
1471
+ int64_t ne2,
1472
+ int64_t ne3,
1473
+ size_t nb1, // row stride in bytes
1474
+ size_t nb2, // slice stride in bytes
1475
+ size_t nb3,
1476
+ size_t offset);
1477
+
1478
+ GGML_API struct ggml_tensor * ggml_permute(
1479
+ struct ggml_context * ctx,
1480
+ struct ggml_tensor * a,
1481
+ int axis0,
1482
+ int axis1,
1483
+ int axis2,
1484
+ int axis3);
1485
+
1486
+ // alias for ggml_permute(ctx, a, 1, 0, 2, 3)
1487
+ GGML_API struct ggml_tensor * ggml_transpose(
1488
+ struct ggml_context * ctx,
1489
+ struct ggml_tensor * a);
1490
+
1491
+ // supports 3D: a->ne[2] == b->ne[1]
1492
+ GGML_API struct ggml_tensor * ggml_get_rows(
1493
+ struct ggml_context * ctx,
1494
+ struct ggml_tensor * a, // data
1495
+ struct ggml_tensor * b); // row indices
1496
+
1497
+ GGML_API struct ggml_tensor * ggml_get_rows_back(
1498
+ struct ggml_context * ctx,
1499
+ struct ggml_tensor * a, // gradients of ggml_get_rows result
1500
+ struct ggml_tensor * b, // row indices
1501
+ struct ggml_tensor * c); // data for ggml_get_rows, only used for its shape
1502
+
1503
+ // a TD [n_embd, ne1, ne2, ne3]
1504
+ // b TS [n_embd, n_rows, ne02, ne03] | ne02 == ne2, ne03 == ne3
1505
+ // c I64 [n_rows, ne11, ne12, 1] | c[i] in [0, ne1)
1506
+ //
1507
+ // undefined behavior if destination rows overlap
1508
+ //
1509
+ // broadcast:
1510
+ // ne2 % ne11 == 0
1511
+ // ne3 % ne12 == 0
1512
+ //
1513
+ // return view(a)
1514
+ GGML_API struct ggml_tensor * ggml_set_rows(
1515
+ struct ggml_context * ctx,
1516
+ struct ggml_tensor * a, // destination
1517
+ struct ggml_tensor * b, // source
1518
+ struct ggml_tensor * c); // row indices
1519
+
1520
+ GGML_API struct ggml_tensor * ggml_diag(
1521
+ struct ggml_context * ctx,
1522
+ struct ggml_tensor * a);
1523
+
1524
+ // set elements above the diagonal to -INF
1525
+ GGML_API struct ggml_tensor * ggml_diag_mask_inf(
1526
+ struct ggml_context * ctx,
1527
+ struct ggml_tensor * a,
1528
+ int n_past);
1529
+
1530
+ // in-place, returns view(a)
1531
+ GGML_API struct ggml_tensor * ggml_diag_mask_inf_inplace(
1532
+ struct ggml_context * ctx,
1533
+ struct ggml_tensor * a,
1534
+ int n_past);
1535
+
1536
+ // set elements above the diagonal to 0
1537
+ GGML_API struct ggml_tensor * ggml_diag_mask_zero(
1538
+ struct ggml_context * ctx,
1539
+ struct ggml_tensor * a,
1540
+ int n_past);
1541
+
1542
+ // in-place, returns view(a)
1543
+ GGML_API struct ggml_tensor * ggml_diag_mask_zero_inplace(
1544
+ struct ggml_context * ctx,
1545
+ struct ggml_tensor * a,
1546
+ int n_past);
1547
+
1548
+ GGML_API struct ggml_tensor * ggml_soft_max(
1549
+ struct ggml_context * ctx,
1550
+ struct ggml_tensor * a);
1551
+
1552
+ // in-place, returns view(a)
1553
+ GGML_API struct ggml_tensor * ggml_soft_max_inplace(
1554
+ struct ggml_context * ctx,
1555
+ struct ggml_tensor * a);
1556
+
1557
+ // a [ne0, ne01, ne02, ne03]
1558
+ // mask [ne0, ne11, ne12, ne13] | ne11 >= ne01, F16 or F32, optional
1559
+ //
1560
+ // broadcast:
1561
+ // ne02 % ne12 == 0
1562
+ // ne03 % ne13 == 0
1563
+ //
1564
+ // fused soft_max(a*scale + mask*(ALiBi slope))
1565
+ // max_bias = 0.0f for no ALiBi
1566
+ GGML_API struct ggml_tensor * ggml_soft_max_ext(
1567
+ struct ggml_context * ctx,
1568
+ struct ggml_tensor * a,
1569
+ struct ggml_tensor * mask,
1570
+ float scale,
1571
+ float max_bias);
1572
+
1573
+ GGML_API struct ggml_tensor * ggml_soft_max_ext_back(
1574
+ struct ggml_context * ctx,
1575
+ struct ggml_tensor * a,
1576
+ struct ggml_tensor * b,
1577
+ float scale,
1578
+ float max_bias);
1579
+
1580
+ // in-place, returns view(a)
1581
+ GGML_API struct ggml_tensor * ggml_soft_max_ext_back_inplace(
1582
+ struct ggml_context * ctx,
1583
+ struct ggml_tensor * a,
1584
+ struct ggml_tensor * b,
1585
+ float scale,
1586
+ float max_bias);
1587
+
1588
+ // rotary position embedding
1589
+ // if (mode & 1) - skip n_past elements (NOT SUPPORTED)
1590
+ // if (mode & GGML_ROPE_TYPE_NEOX) - GPT-NeoX style
1591
+ //
1592
+ // b is an int32 vector with size a->ne[2], it contains the positions
1593
+ GGML_API struct ggml_tensor * ggml_rope(
1594
+ struct ggml_context * ctx,
1595
+ struct ggml_tensor * a,
1596
+ struct ggml_tensor * b,
1597
+ int n_dims,
1598
+ int mode);
1599
+
1600
+ // in-place, returns view(a)
1601
+ GGML_API struct ggml_tensor * ggml_rope_inplace(
1602
+ struct ggml_context * ctx,
1603
+ struct ggml_tensor * a,
1604
+ struct ggml_tensor * b,
1605
+ int n_dims,
1606
+ int mode);
1607
+
1608
+ // custom RoPE
1609
+ // c is freq factors (e.g. phi3-128k), (optional)
1610
+ GGML_API struct ggml_tensor * ggml_rope_ext(
1611
+ struct ggml_context * ctx,
1612
+ struct ggml_tensor * a,
1613
+ struct ggml_tensor * b,
1614
+ struct ggml_tensor * c,
1615
+ int n_dims,
1616
+ int mode,
1617
+ int n_ctx_orig,
1618
+ float freq_base,
1619
+ float freq_scale,
1620
+ float ext_factor,
1621
+ float attn_factor,
1622
+ float beta_fast,
1623
+ float beta_slow);
1624
+
1625
+ GGML_API struct ggml_tensor * ggml_rope_multi(
1626
+ struct ggml_context * ctx,
1627
+ struct ggml_tensor * a,
1628
+ struct ggml_tensor * b,
1629
+ struct ggml_tensor * c,
1630
+ int n_dims,
1631
+ int sections[4],
1632
+ int mode,
1633
+ int n_ctx_orig,
1634
+ float freq_base,
1635
+ float freq_scale,
1636
+ float ext_factor,
1637
+ float attn_factor,
1638
+ float beta_fast,
1639
+ float beta_slow);
1640
+
1641
+ // in-place, returns view(a)
1642
+ GGML_API struct ggml_tensor * ggml_rope_ext_inplace(
1643
+ struct ggml_context * ctx,
1644
+ struct ggml_tensor * a,
1645
+ struct ggml_tensor * b,
1646
+ struct ggml_tensor * c,
1647
+ int n_dims,
1648
+ int mode,
1649
+ int n_ctx_orig,
1650
+ float freq_base,
1651
+ float freq_scale,
1652
+ float ext_factor,
1653
+ float attn_factor,
1654
+ float beta_fast,
1655
+ float beta_slow);
1656
+
1657
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom(
1658
+ struct ggml_context * ctx,
1659
+ struct ggml_tensor * a,
1660
+ struct ggml_tensor * b,
1661
+ int n_dims,
1662
+ int mode,
1663
+ int n_ctx_orig,
1664
+ float freq_base,
1665
+ float freq_scale,
1666
+ float ext_factor,
1667
+ float attn_factor,
1668
+ float beta_fast,
1669
+ float beta_slow),
1670
+ "use ggml_rope_ext instead");
1671
+
1672
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_rope_custom_inplace(
1673
+ struct ggml_context * ctx,
1674
+ struct ggml_tensor * a,
1675
+ struct ggml_tensor * b,
1676
+ int n_dims,
1677
+ int mode,
1678
+ int n_ctx_orig,
1679
+ float freq_base,
1680
+ float freq_scale,
1681
+ float ext_factor,
1682
+ float attn_factor,
1683
+ float beta_fast,
1684
+ float beta_slow),
1685
+ "use ggml_rope_ext_inplace instead");
1686
+
1687
+ // compute correction dims for YaRN RoPE scaling
1688
+ GGML_API void ggml_rope_yarn_corr_dims(
1689
+ int n_dims, int n_ctx_orig, float freq_base, float beta_fast, float beta_slow, float dims[2]);
1690
+
1691
+ // rotary position embedding backward, i.e compute dx from dy
1692
+ // a - dy
1693
+ GGML_API struct ggml_tensor * ggml_rope_ext_back(
1694
+ struct ggml_context * ctx,
1695
+ struct ggml_tensor * a, // gradients of ggml_rope result
1696
+ struct ggml_tensor * b, // positions
1697
+ struct ggml_tensor * c, // freq factors
1698
+ int n_dims,
1699
+ int mode,
1700
+ int n_ctx_orig,
1701
+ float freq_base,
1702
+ float freq_scale,
1703
+ float ext_factor,
1704
+ float attn_factor,
1705
+ float beta_fast,
1706
+ float beta_slow);
1707
+
1708
+ GGML_API struct ggml_tensor * ggml_rope_multi_back(
1709
+ struct ggml_context * ctx,
1710
+ struct ggml_tensor * a,
1711
+ struct ggml_tensor * b,
1712
+ struct ggml_tensor * c,
1713
+ int n_dims,
1714
+ int sections[4],
1715
+ int mode,
1716
+ int n_ctx_orig,
1717
+ float freq_base,
1718
+ float freq_scale,
1719
+ float ext_factor,
1720
+ float attn_factor,
1721
+ float beta_fast,
1722
+ float beta_slow);
1723
+
1724
+
1725
+ // clamp
1726
+ // in-place, returns view(a)
1727
+ GGML_API struct ggml_tensor * ggml_clamp(
1728
+ struct ggml_context * ctx,
1729
+ struct ggml_tensor * a,
1730
+ float min,
1731
+ float max);
1732
+
1733
+ // im2col
1734
+ // converts data into a format that effectively results in a convolution when combined with matrix multiplication
1735
+ GGML_API struct ggml_tensor * ggml_im2col(
1736
+ struct ggml_context * ctx,
1737
+ struct ggml_tensor * a, // convolution kernel
1738
+ struct ggml_tensor * b, // data
1739
+ int s0, // stride dimension 0
1740
+ int s1, // stride dimension 1
1741
+ int p0, // padding dimension 0
1742
+ int p1, // padding dimension 1
1743
+ int d0, // dilation dimension 0
1744
+ int d1, // dilation dimension 1
1745
+ bool is_2D,
1746
+ enum ggml_type dst_type);
1747
+
1748
+ GGML_API struct ggml_tensor * ggml_im2col_back(
1749
+ struct ggml_context * ctx,
1750
+ struct ggml_tensor * a, // convolution kernel
1751
+ struct ggml_tensor * b, // gradient of im2col output
1752
+ int64_t * ne, // shape of im2col input
1753
+ int s0, // stride dimension 0
1754
+ int s1, // stride dimension 1
1755
+ int p0, // padding dimension 0
1756
+ int p1, // padding dimension 1
1757
+ int d0, // dilation dimension 0
1758
+ int d1, // dilation dimension 1
1759
+ bool is_2D);
1760
+
1761
+ GGML_API struct ggml_tensor * ggml_conv_1d(
1762
+ struct ggml_context * ctx,
1763
+ struct ggml_tensor * a, // convolution kernel
1764
+ struct ggml_tensor * b, // data
1765
+ int s0, // stride
1766
+ int p0, // padding
1767
+ int d0); // dilation
1768
+
1769
+ // conv_1d with padding = half
1770
+ // alias for ggml_conv_1d(a, b, s, a->ne[0]/2, d)
1771
+ GGML_API struct ggml_tensor* ggml_conv_1d_ph(
1772
+ struct ggml_context * ctx,
1773
+ struct ggml_tensor * a, // convolution kernel
1774
+ struct ggml_tensor * b, // data
1775
+ int s, // stride
1776
+ int d); // dilation
1777
+
1778
+ // depthwise
1779
+ // TODO: this is very likely wrong for some cases! - needs more testing
1780
+ GGML_API struct ggml_tensor * ggml_conv_1d_dw(
1781
+ struct ggml_context * ctx,
1782
+ struct ggml_tensor * a, // convolution kernel
1783
+ struct ggml_tensor * b, // data
1784
+ int s0, // stride
1785
+ int p0, // padding
1786
+ int d0); // dilation
1787
+
1788
+ GGML_API struct ggml_tensor * ggml_conv_1d_dw_ph(
1789
+ struct ggml_context * ctx,
1790
+ struct ggml_tensor * a, // convolution kernel
1791
+ struct ggml_tensor * b, // data
1792
+ int s0, // stride
1793
+ int d0); // dilation
1794
+
1795
+ GGML_API struct ggml_tensor * ggml_conv_transpose_1d(
1796
+ struct ggml_context * ctx,
1797
+ struct ggml_tensor * a, // convolution kernel
1798
+ struct ggml_tensor * b, // data
1799
+ int s0, // stride
1800
+ int p0, // padding
1801
+ int d0); // dilation
1802
+
1803
+ GGML_API struct ggml_tensor * ggml_conv_2d(
1804
+ struct ggml_context * ctx,
1805
+ struct ggml_tensor * a, // convolution kernel
1806
+ struct ggml_tensor * b, // data
1807
+ int s0, // stride dimension 0
1808
+ int s1, // stride dimension 1
1809
+ int p0, // padding dimension 0
1810
+ int p1, // padding dimension 1
1811
+ int d0, // dilation dimension 0
1812
+ int d1); // dilation dimension 1
1813
+
1814
+ // kernel size is a->ne[0] x a->ne[1]
1815
+ // stride is equal to kernel size
1816
+ // padding is zero
1817
+ // example:
1818
+ // a: 16 16 3 768
1819
+ // b: 1024 1024 3 1
1820
+ // res: 64 64 768 1
1821
+ // used in sam
1822
+ GGML_API struct ggml_tensor * ggml_conv_2d_sk_p0(
1823
+ struct ggml_context * ctx,
1824
+ struct ggml_tensor * a,
1825
+ struct ggml_tensor * b);
1826
+
1827
+ // kernel size is a->ne[0] x a->ne[1]
1828
+ // stride is 1
1829
+ // padding is half
1830
+ // example:
1831
+ // a: 3 3 256 256
1832
+ // b: 64 64 256 1
1833
+ // res: 64 64 256 1
1834
+ // used in sam
1835
+ GGML_API struct ggml_tensor * ggml_conv_2d_s1_ph(
1836
+ struct ggml_context * ctx,
1837
+ struct ggml_tensor * a,
1838
+ struct ggml_tensor * b);
1839
+
1840
+ // depthwise (via im2col and mul_mat)
1841
+ GGML_API struct ggml_tensor * ggml_conv_2d_dw(
1842
+ struct ggml_context * ctx,
1843
+ struct ggml_tensor * a, // convolution kernel
1844
+ struct ggml_tensor * b, // data
1845
+ int s0, // stride dimension 0
1846
+ int s1, // stride dimension 1
1847
+ int p0, // padding dimension 0
1848
+ int p1, // padding dimension 1
1849
+ int d0, // dilation dimension 0
1850
+ int d1); // dilation dimension 1
1851
+
1852
+ // Depthwise 2D convolution
1853
+ // may be faster than ggml_conv_2d_dw, but not available in all backends
1854
+ // a: KW KH 1 C convolution kernel
1855
+ // b: W H C N input data
1856
+ // res: W_out H_out C N
1857
+ GGML_API struct ggml_tensor * ggml_conv_2d_dw_direct(
1858
+ struct ggml_context * ctx,
1859
+ struct ggml_tensor * a,
1860
+ struct ggml_tensor * b,
1861
+ int stride0,
1862
+ int stride1,
1863
+ int pad0,
1864
+ int pad1,
1865
+ int dilation0,
1866
+ int dilation1);
1867
+
1868
+ GGML_API struct ggml_tensor * ggml_conv_transpose_2d_p0(
1869
+ struct ggml_context * ctx,
1870
+ struct ggml_tensor * a,
1871
+ struct ggml_tensor * b,
1872
+ int stride);
1873
+
1874
+ GGML_API struct ggml_tensor * ggml_conv_2d_direct(
1875
+ struct ggml_context * ctx,
1876
+ struct ggml_tensor * a, // convolution kernel [KW, KH, IC, OC]
1877
+ struct ggml_tensor * b, // input data [W, H, C, N]
1878
+ int s0, // stride dimension 0
1879
+ int s1, // stride dimension 1
1880
+ int p0, // padding dimension 0
1881
+ int p1, // padding dimension 1
1882
+ int d0, // dilation dimension 0
1883
+ int d1); // dilation dimension 1
1884
+
1885
+ enum ggml_op_pool {
1886
+ GGML_OP_POOL_MAX,
1887
+ GGML_OP_POOL_AVG,
1888
+ GGML_OP_POOL_COUNT,
1889
+ };
1890
+
1891
+ GGML_API struct ggml_tensor * ggml_pool_1d(
1892
+ struct ggml_context * ctx,
1893
+ struct ggml_tensor * a,
1894
+ enum ggml_op_pool op,
1895
+ int k0, // kernel size
1896
+ int s0, // stride
1897
+ int p0); // padding
1898
+
1899
+ // the result will have 2*p0 padding for the first dimension
1900
+ // and 2*p1 padding for the second dimension
1901
+ GGML_API struct ggml_tensor * ggml_pool_2d(
1902
+ struct ggml_context * ctx,
1903
+ struct ggml_tensor * a,
1904
+ enum ggml_op_pool op,
1905
+ int k0,
1906
+ int k1,
1907
+ int s0,
1908
+ int s1,
1909
+ float p0,
1910
+ float p1);
1911
+
1912
+ GGML_API struct ggml_tensor * ggml_pool_2d_back(
1913
+ struct ggml_context * ctx,
1914
+ struct ggml_tensor * a,
1915
+ struct ggml_tensor * af, // "a"/input used in forward pass
1916
+ enum ggml_op_pool op,
1917
+ int k0,
1918
+ int k1,
1919
+ int s0,
1920
+ int s1,
1921
+ float p0,
1922
+ float p1);
1923
+
1924
+ enum ggml_scale_mode {
1925
+ GGML_SCALE_MODE_NEAREST = 0,
1926
+ GGML_SCALE_MODE_BILINEAR = 1,
1927
+
1928
+ GGML_SCALE_MODE_COUNT
1929
+ };
1930
+
1931
+ enum ggml_scale_flag {
1932
+ GGML_SCALE_FLAG_ALIGN_CORNERS = (1 << 8)
1933
+ };
1934
+
1935
+ // interpolate
1936
+ // multiplies ne0 and ne1 by scale factor
1937
+ GGML_API struct ggml_tensor * ggml_upscale(
1938
+ struct ggml_context * ctx,
1939
+ struct ggml_tensor * a,
1940
+ int scale_factor,
1941
+ enum ggml_scale_mode mode);
1942
+
1943
+ // interpolate
1944
+ // interpolate scale to specified dimensions
1945
+ GGML_DEPRECATED(GGML_API struct ggml_tensor * ggml_upscale_ext(
1946
+ struct ggml_context * ctx,
1947
+ struct ggml_tensor * a,
1948
+ int ne0,
1949
+ int ne1,
1950
+ int ne2,
1951
+ int ne3,
1952
+ enum ggml_scale_mode mode),
1953
+ "use ggml_interpolate instead");
1954
+
1955
+ // Up- or downsamples the input to the specified size.
1956
+ // 2D scale modes (eg. bilinear) are applied to the first two dimensions.
1957
+ GGML_API struct ggml_tensor * ggml_interpolate(
1958
+ struct ggml_context * ctx,
1959
+ struct ggml_tensor * a,
1960
+ int64_t ne0,
1961
+ int64_t ne1,
1962
+ int64_t ne2,
1963
+ int64_t ne3,
1964
+ uint32_t mode); // ggml_scale_mode [ | ggml_scale_flag...]
1965
+
1966
+ // pad each dimension with zeros: [x, ..., x] -> [x, ..., x, 0, ..., 0]
1967
+ GGML_API struct ggml_tensor * ggml_pad(
1968
+ struct ggml_context * ctx,
1969
+ struct ggml_tensor * a,
1970
+ int p0,
1971
+ int p1,
1972
+ int p2,
1973
+ int p3);
1974
+
1975
+ // pad each dimension with reflection: [a, b, c, d] -> [b, a, b, c, d, c]
1976
+ GGML_API struct ggml_tensor * ggml_pad_reflect_1d(
1977
+ struct ggml_context * ctx,
1978
+ struct ggml_tensor * a,
1979
+ int p0,
1980
+ int p1);
1981
+
1982
+ // Move tensor elements by an offset given for each dimension. Elements that
1983
+ // are shifted beyond the last position are wrapped around to the beginning.
1984
+ GGML_API struct ggml_tensor * ggml_roll(
1985
+ struct ggml_context * ctx,
1986
+ struct ggml_tensor * a,
1987
+ int shift0,
1988
+ int shift1,
1989
+ int shift2,
1990
+ int shift3);
1991
+
1992
+
1993
+ // Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
1994
+ // timesteps: [N,]
1995
+ // return: [N, dim]
1996
+ GGML_API struct ggml_tensor * ggml_timestep_embedding(
1997
+ struct ggml_context * ctx,
1998
+ struct ggml_tensor * timesteps,
1999
+ int dim,
2000
+ int max_period);
2001
+
2002
+ // sort rows
2003
+ enum ggml_sort_order {
2004
+ GGML_SORT_ORDER_ASC,
2005
+ GGML_SORT_ORDER_DESC,
2006
+ };
2007
+
2008
+ GGML_API struct ggml_tensor * ggml_argsort(
2009
+ struct ggml_context * ctx,
2010
+ struct ggml_tensor * a,
2011
+ enum ggml_sort_order order);
2012
+
2013
+ GGML_API struct ggml_tensor * ggml_arange(
2014
+ struct ggml_context * ctx,
2015
+ float start,
2016
+ float stop,
2017
+ float step);
2018
+
2019
+ // top k elements per row
2020
+ GGML_API struct ggml_tensor * ggml_top_k(
2021
+ struct ggml_context * ctx,
2022
+ struct ggml_tensor * a,
2023
+ int k);
2024
+
2025
+ #define GGML_KQ_MASK_PAD 64
2026
+
2027
+ // q: [n_embd_k, n_batch, n_head, ne3 ]
2028
+ // k: [n_embd_k, n_kv, n_head_kv, ne3 ]
2029
+ // v: [n_embd_v, n_kv, n_head_kv, ne3 ] !! not transposed !!
2030
+ // mask: [n_kv, n_batch_pad, ne32, ne33] !! n_batch_pad = GGML_PAD(n_batch, GGML_KQ_MASK_PAD) !!
2031
+ // res: [n_embd_v, n_head, n_batch, ne3 ] !! permuted !!
2032
+ //
2033
+ // broadcast:
2034
+ // n_head % n_head_kv == 0
2035
+ // n_head % ne32 == 0
2036
+ // ne3 % ne33 == 0
2037
+ //
2038
+ GGML_API struct ggml_tensor * ggml_flash_attn_ext(
2039
+ struct ggml_context * ctx,
2040
+ struct ggml_tensor * q,
2041
+ struct ggml_tensor * k,
2042
+ struct ggml_tensor * v,
2043
+ struct ggml_tensor * mask,
2044
+ float scale,
2045
+ float max_bias,
2046
+ float logit_softcap);
2047
+
2048
+ GGML_API void ggml_flash_attn_ext_set_prec(
2049
+ struct ggml_tensor * a,
2050
+ enum ggml_prec prec);
2051
+
2052
+ GGML_API enum ggml_prec ggml_flash_attn_ext_get_prec(
2053
+ const struct ggml_tensor * a);
2054
+
2055
+ // TODO: needs to be adapted to ggml_flash_attn_ext
2056
+ GGML_API struct ggml_tensor * ggml_flash_attn_back(
2057
+ struct ggml_context * ctx,
2058
+ struct ggml_tensor * q,
2059
+ struct ggml_tensor * k,
2060
+ struct ggml_tensor * v,
2061
+ struct ggml_tensor * d,
2062
+ bool masked);
2063
+
2064
+ GGML_API struct ggml_tensor * ggml_ssm_conv(
2065
+ struct ggml_context * ctx,
2066
+ struct ggml_tensor * sx,
2067
+ struct ggml_tensor * c);
2068
+
2069
+ GGML_API struct ggml_tensor * ggml_ssm_scan(
2070
+ struct ggml_context * ctx,
2071
+ struct ggml_tensor * s,
2072
+ struct ggml_tensor * x,
2073
+ struct ggml_tensor * dt,
2074
+ struct ggml_tensor * A,
2075
+ struct ggml_tensor * B,
2076
+ struct ggml_tensor * C,
2077
+ struct ggml_tensor * ids);
2078
+
2079
+ // partition into non-overlapping windows with padding if needed
2080
+ // example:
2081
+ // a: 768 64 64 1
2082
+ // w: 14
2083
+ // res: 768 14 14 25
2084
+ // used in sam
2085
+ GGML_API struct ggml_tensor * ggml_win_part(
2086
+ struct ggml_context * ctx,
2087
+ struct ggml_tensor * a,
2088
+ int w);
2089
+
2090
+ // reverse of ggml_win_part
2091
+ // used in sam
2092
+ GGML_API struct ggml_tensor * ggml_win_unpart(
2093
+ struct ggml_context * ctx,
2094
+ struct ggml_tensor * a,
2095
+ int w0,
2096
+ int h0,
2097
+ int w);
2098
+
2099
+ GGML_API struct ggml_tensor * ggml_unary(
2100
+ struct ggml_context * ctx,
2101
+ struct ggml_tensor * a,
2102
+ enum ggml_unary_op op);
2103
+
2104
+ GGML_API struct ggml_tensor * ggml_unary_inplace(
2105
+ struct ggml_context * ctx,
2106
+ struct ggml_tensor * a,
2107
+ enum ggml_unary_op op);
2108
+
2109
+ // used in sam
2110
+ GGML_API struct ggml_tensor * ggml_get_rel_pos(
2111
+ struct ggml_context * ctx,
2112
+ struct ggml_tensor * a,
2113
+ int qh,
2114
+ int kh);
2115
+
2116
+ // used in sam
2117
+ GGML_API struct ggml_tensor * ggml_add_rel_pos(
2118
+ struct ggml_context * ctx,
2119
+ struct ggml_tensor * a,
2120
+ struct ggml_tensor * pw,
2121
+ struct ggml_tensor * ph);
2122
+
2123
+ GGML_API struct ggml_tensor * ggml_add_rel_pos_inplace(
2124
+ struct ggml_context * ctx,
2125
+ struct ggml_tensor * a,
2126
+ struct ggml_tensor * pw,
2127
+ struct ggml_tensor * ph);
2128
+
2129
+ GGML_API struct ggml_tensor * ggml_rwkv_wkv6(
2130
+ struct ggml_context * ctx,
2131
+ struct ggml_tensor * k,
2132
+ struct ggml_tensor * v,
2133
+ struct ggml_tensor * r,
2134
+ struct ggml_tensor * tf,
2135
+ struct ggml_tensor * td,
2136
+ struct ggml_tensor * state);
2137
+
2138
+ GGML_API struct ggml_tensor * ggml_gated_linear_attn(
2139
+ struct ggml_context * ctx,
2140
+ struct ggml_tensor * k,
2141
+ struct ggml_tensor * v,
2142
+ struct ggml_tensor * q,
2143
+ struct ggml_tensor * g,
2144
+ struct ggml_tensor * state,
2145
+ float scale);
2146
+
2147
+ GGML_API struct ggml_tensor * ggml_rwkv_wkv7(
2148
+ struct ggml_context * ctx,
2149
+ struct ggml_tensor * r,
2150
+ struct ggml_tensor * w,
2151
+ struct ggml_tensor * k,
2152
+ struct ggml_tensor * v,
2153
+ struct ggml_tensor * a,
2154
+ struct ggml_tensor * b,
2155
+ struct ggml_tensor * state);
2156
+
2157
+ // custom operators
2158
+
2159
+ typedef void (*ggml_custom1_op_t)(struct ggml_tensor * dst , const struct ggml_tensor * a, int ith, int nth, void * userdata);
2160
+ 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);
2161
+ 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);
2162
+
2163
+ #define GGML_N_TASKS_MAX (-1)
2164
+ // n_tasks == GGML_N_TASKS_MAX means to use max number of tasks
2165
+
2166
+ GGML_API struct ggml_tensor * ggml_map_custom1(
2167
+ struct ggml_context * ctx,
2168
+ struct ggml_tensor * a,
2169
+ ggml_custom1_op_t fun,
2170
+ int n_tasks,
2171
+ void * userdata);
2172
+
2173
+ GGML_API struct ggml_tensor * ggml_map_custom1_inplace(
2174
+ struct ggml_context * ctx,
2175
+ struct ggml_tensor * a,
2176
+ ggml_custom1_op_t fun,
2177
+ int n_tasks,
2178
+ void * userdata);
2179
+
2180
+ GGML_API struct ggml_tensor * ggml_map_custom2(
2181
+ struct ggml_context * ctx,
2182
+ struct ggml_tensor * a,
2183
+ struct ggml_tensor * b,
2184
+ ggml_custom2_op_t fun,
2185
+ int n_tasks,
2186
+ void * userdata);
2187
+
2188
+ GGML_API struct ggml_tensor * ggml_map_custom2_inplace(
2189
+ struct ggml_context * ctx,
2190
+ struct ggml_tensor * a,
2191
+ struct ggml_tensor * b,
2192
+ ggml_custom2_op_t fun,
2193
+ int n_tasks,
2194
+ void * userdata);
2195
+
2196
+ GGML_API struct ggml_tensor * ggml_map_custom3(
2197
+ struct ggml_context * ctx,
2198
+ struct ggml_tensor * a,
2199
+ struct ggml_tensor * b,
2200
+ struct ggml_tensor * c,
2201
+ ggml_custom3_op_t fun,
2202
+ int n_tasks,
2203
+ void * userdata);
2204
+
2205
+ GGML_API struct ggml_tensor * ggml_map_custom3_inplace(
2206
+ struct ggml_context * ctx,
2207
+ struct ggml_tensor * a,
2208
+ struct ggml_tensor * b,
2209
+ struct ggml_tensor * c,
2210
+ ggml_custom3_op_t fun,
2211
+ int n_tasks,
2212
+ void * userdata);
2213
+
2214
+ typedef void (*ggml_custom_op_t)(struct ggml_tensor * dst , int ith, int nth, void * userdata);
2215
+
2216
+ GGML_API struct ggml_tensor * ggml_custom_4d(
2217
+ struct ggml_context * ctx,
2218
+ enum ggml_type type,
2219
+ int64_t ne0,
2220
+ int64_t ne1,
2221
+ int64_t ne2,
2222
+ int64_t ne3,
2223
+ struct ggml_tensor ** args,
2224
+ int n_args,
2225
+ ggml_custom_op_t fun,
2226
+ int n_tasks,
2227
+ void * userdata);
2228
+
2229
+ GGML_API struct ggml_tensor * ggml_custom_inplace(
2230
+ struct ggml_context * ctx,
2231
+ struct ggml_tensor * a,
2232
+ struct ggml_tensor ** args,
2233
+ int n_args,
2234
+ ggml_custom_op_t fun,
2235
+ int n_tasks,
2236
+ void * userdata);
2237
+
2238
+ // loss function
2239
+
2240
+ GGML_API struct ggml_tensor * ggml_cross_entropy_loss(
2241
+ struct ggml_context * ctx,
2242
+ struct ggml_tensor * a, // logits
2243
+ struct ggml_tensor * b); // labels
2244
+
2245
+ GGML_API struct ggml_tensor * ggml_cross_entropy_loss_back(
2246
+ struct ggml_context * ctx,
2247
+ struct ggml_tensor * a, // logits
2248
+ struct ggml_tensor * b, // labels
2249
+ struct ggml_tensor * c); // gradients of cross_entropy_loss result
2250
+
2251
+ // AdamW optimizer step
2252
+ // Paper: https://arxiv.org/pdf/1711.05101v3.pdf
2253
+ // PyTorch: https://pytorch.org/docs/stable/generated/torch.optim.AdamW.html
2254
+ GGML_API struct ggml_tensor * ggml_opt_step_adamw(
2255
+ struct ggml_context * ctx,
2256
+ struct ggml_tensor * a,
2257
+ struct ggml_tensor * grad,
2258
+ struct ggml_tensor * m,
2259
+ struct ggml_tensor * v,
2260
+ struct ggml_tensor * adamw_params); // parameters such a the learning rate
2261
+
2262
+ //
2263
+ // automatic differentiation
2264
+ //
2265
+
2266
+ GGML_API void ggml_build_forward_expand(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
2267
+ GGML_API void ggml_build_backward_expand(
2268
+ struct ggml_context * ctx, // context for gradient computation
2269
+ struct ggml_cgraph * cgraph,
2270
+ struct ggml_tensor ** grad_accs);
2271
+
2272
+ // graph allocation in a context
2273
+ GGML_API struct ggml_cgraph * ggml_new_graph (struct ggml_context * ctx); // size = GGML_DEFAULT_GRAPH_SIZE, grads = false
2274
+ GGML_API struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads);
2275
+ GGML_API struct ggml_cgraph * ggml_graph_dup (struct ggml_context * ctx, struct ggml_cgraph * cgraph, bool force_grads);
2276
+ GGML_API void ggml_graph_cpy (struct ggml_cgraph * src, struct ggml_cgraph * dst);
2277
+ GGML_API void ggml_graph_reset (struct ggml_cgraph * cgraph); // set regular grads + optimizer momenta to 0, set loss grad to 1
2278
+ GGML_API void ggml_graph_clear (struct ggml_cgraph * cgraph);
2279
+
2280
+ GGML_API int ggml_graph_size (struct ggml_cgraph * cgraph);
2281
+ GGML_API struct ggml_tensor * ggml_graph_node (struct ggml_cgraph * cgraph, int i); // if i < 0, returns nodes[n_nodes + i]
2282
+ GGML_API struct ggml_tensor ** ggml_graph_nodes (struct ggml_cgraph * cgraph);
2283
+ GGML_API int ggml_graph_n_nodes(struct ggml_cgraph * cgraph);
2284
+
2285
+ GGML_API void ggml_graph_add_node(struct ggml_cgraph * cgraph, struct ggml_tensor * tensor);
2286
+
2287
+ GGML_API size_t ggml_graph_overhead(void);
2288
+ GGML_API size_t ggml_graph_overhead_custom(size_t size, bool grads);
2289
+
2290
+ GGML_API struct ggml_tensor * ggml_graph_get_tensor (const struct ggml_cgraph * cgraph, const char * name);
2291
+ GGML_API struct ggml_tensor * ggml_graph_get_grad (const struct ggml_cgraph * cgraph, const struct ggml_tensor * node);
2292
+ GGML_API struct ggml_tensor * ggml_graph_get_grad_acc(const struct ggml_cgraph * cgraph, const struct ggml_tensor * node);
2293
+
2294
+ // print info and performance information for the graph
2295
+ GGML_API void ggml_graph_print(const struct ggml_cgraph * cgraph);
2296
+
2297
+ // dump the graph into a file using the dot format
2298
+ GGML_API void ggml_graph_dump_dot(const struct ggml_cgraph * gb, const struct ggml_cgraph * gf, const char * filename);
2299
+
2300
+ // TODO these functions were sandwiched in the old optimization interface, is there a better place for them?
2301
+ typedef void (*ggml_log_callback)(enum ggml_log_level level, const char * text, void * user_data);
2302
+
2303
+ // Set callback for all future logging events.
2304
+ // If this is not called, or NULL is supplied, everything is output on stderr.
2305
+ GGML_API void ggml_log_set(ggml_log_callback log_callback, void * user_data);
2306
+
2307
+ GGML_API struct ggml_tensor * ggml_set_zero(struct ggml_tensor * tensor);
2308
+
2309
+ //
2310
+ // quantization
2311
+ //
2312
+
2313
+ // - ggml_quantize_init can be called multiple times with the same type
2314
+ // it will only initialize the quantization tables for the first call or after ggml_quantize_free
2315
+ // automatically called by ggml_quantize_chunk for convenience
2316
+ //
2317
+ // - ggml_quantize_free will free any memory allocated by ggml_quantize_init
2318
+ // call this at the end of the program to avoid memory leaks
2319
+ //
2320
+ // note: these are thread-safe
2321
+ //
2322
+ GGML_API void ggml_quantize_init(enum ggml_type type);
2323
+ GGML_API void ggml_quantize_free(void);
2324
+
2325
+ // some quantization type cannot be used without an importance matrix
2326
+ GGML_API bool ggml_quantize_requires_imatrix(enum ggml_type type);
2327
+
2328
+ // calls ggml_quantize_init internally (i.e. can allocate memory)
2329
+ GGML_API size_t ggml_quantize_chunk(
2330
+ enum ggml_type type,
2331
+ const float * src,
2332
+ void * dst,
2333
+ int64_t start,
2334
+ int64_t nrows,
2335
+ int64_t n_per_row,
2336
+ const float * imatrix);
2337
+
2338
+ #ifdef __cplusplus
2339
+ // restrict not standard in C++
2340
+ # if defined(__GNUC__)
2341
+ # define GGML_RESTRICT __restrict__
2342
+ # elif defined(__clang__)
2343
+ # define GGML_RESTRICT __restrict
2344
+ # elif defined(_MSC_VER)
2345
+ # define GGML_RESTRICT __restrict
2346
+ # else
2347
+ # define GGML_RESTRICT
2348
+ # endif
2349
+ #else
2350
+ # if defined (_MSC_VER) && (__STDC_VERSION__ < 201112L)
2351
+ # define GGML_RESTRICT __restrict
2352
+ # else
2353
+ # define GGML_RESTRICT restrict
2354
+ # endif
2355
+ #endif
2356
+ typedef void (*ggml_to_float_t) (const void * GGML_RESTRICT x, float * GGML_RESTRICT y, int64_t k);
2357
+ typedef void (*ggml_from_float_t)(const float * GGML_RESTRICT x, void * GGML_RESTRICT y, int64_t k);
2358
+
2359
+ struct ggml_type_traits {
2360
+ const char * type_name;
2361
+ int64_t blck_size;
2362
+ int64_t blck_size_interleave; // interleave elements in blocks
2363
+ size_t type_size;
2364
+ bool is_quantized;
2365
+ ggml_to_float_t to_float;
2366
+ ggml_from_float_t from_float_ref;
2367
+ };
2368
+
2369
+ GGML_API const struct ggml_type_traits * ggml_get_type_traits(enum ggml_type type);
2370
+
2371
+ // ggml threadpool
2372
+ // TODO: currently, only a few functions are in the base ggml API, while the rest are in the CPU backend
2373
+ // the goal should be to create an API that other backends can use move everything to the ggml base
2374
+
2375
+ // scheduling priorities
2376
+ enum ggml_sched_priority {
2377
+ GGML_SCHED_PRIO_LOW = -1,
2378
+ GGML_SCHED_PRIO_NORMAL,
2379
+ GGML_SCHED_PRIO_MEDIUM,
2380
+ GGML_SCHED_PRIO_HIGH,
2381
+ GGML_SCHED_PRIO_REALTIME
2382
+ };
2383
+
2384
+ // threadpool params
2385
+ // Use ggml_threadpool_params_default() or ggml_threadpool_params_init() to populate the defaults
2386
+ struct ggml_threadpool_params {
2387
+ bool cpumask[GGML_MAX_N_THREADS]; // mask of cpu cores (all-zeros means use default affinity settings)
2388
+ int n_threads; // number of threads
2389
+ enum ggml_sched_priority prio; // thread priority
2390
+ uint32_t poll; // polling level (0 - no polling, 100 - aggressive polling)
2391
+ bool strict_cpu; // strict cpu placement
2392
+ bool paused; // start in paused state
2393
+ };
2394
+
2395
+ struct ggml_threadpool; // forward declaration, see ggml.c
2396
+
2397
+ typedef struct ggml_threadpool * ggml_threadpool_t;
2398
+
2399
+ GGML_API struct ggml_threadpool_params ggml_threadpool_params_default(int n_threads);
2400
+ GGML_API void ggml_threadpool_params_init (struct ggml_threadpool_params * p, int n_threads);
2401
+ GGML_API bool ggml_threadpool_params_match (const struct ggml_threadpool_params * p0, const struct ggml_threadpool_params * p1);
2402
+
2403
+ #ifdef __cplusplus
2404
+ }
2405
+ #endif